Make Documents Send and Sync - #161
Open
KayJay7 wants to merge 2 commits into
Open
Conversation
Instead of storing a xee_interpreter::xml::DocumentsRef, Documents now stores a plain xee_interpreter::xml::Documents. Without the Rc, xee_xpath::documents::Documents becomes send. For this Query::execute_with_context gets replaced by Query::execute_with_builder.
…ync (Documents is also Sync) Removing the RefCells in DocumentOrderAnnotations makes the struct Sync. This makes both xee_interpreter::xml::Documents and xee_xpath::documents::Documents also Sync Now xee_xpath::documents::Documents can be shared across threads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The purpose of this is to allow sharing an
xee_xpath::documents::Documentsobject across threads, so it can be queried and interacted with. This approach has some key differences from #138 that we will discuss.The problem is that
Documentscontains aDocumentsRef, which is neitherSendnorSync. This pr replaces thatDocumentsReffor a plainxee_interpreter::xml::Documents, and changes theDocumentsReffrom anRc<RefCell<xml::Documents>>into a simplerRefCell<&'a mut xml::Documents>. This allows us to "create" aDocumentsRefwhen it's time to pass it to the interpreter, and drop it soon after (which can't be done withRcs), and tracking it's lifetime across the interpreter instead of relying on reference counting.A consequence of this is that when you take a reference to the
xml::Documentsout ofDocumentsit has a lifetime and is borrowchecked (e.g. mutably accessingDocumentswould invalidate the reference), so we had to make a small change to the API. We replacedQuery::execute_with_context getswithQuery::execute_with_builder, this way, the full context (with thexml::Documentsreference) is only built when executing, and the builder doesn't need lifetime tracking.Due to the size of this project, I had the help of AI for the grunt work of the implementation. However, every change has been checked and most of them are just adding lifetimes to signatures.
cargo testpasses all tests, andxee-testrunnerpasses and fails the same tests as before the PR.Difference from #138
In #138, the
DocumentsRefis made shareable between threads by replacingRc<RefCell<xml::Documents>>withArc<RwLock<xml::Documents>>. This leaves the API unchanged because it doesn't affects lifetimes, but has some major drawbacks:Arcis almost free,RwLockis not, I haven't tested but it's sure to have some impact on performance because the interpreter locks and unlocks it repeatedly instead of only at the start and end of execution.DocumentsRefcan be shared among threads, the compiler will allow running queries concurrently as long as you provide theXotand theDynamicContext. But opening and closing the locks immediately only prevent the actual writes to overstep each other, but does not protect the two executions as a whole