Add optional sync feature to allow sharing DocumentsRef.#138
Add optional sync feature to allow sharing DocumentsRef.#138cgranade wants to merge 1 commit intoPaligo:mainfrom
Conversation
|
Thank you! Some feedback:
|
|
Thanks for your comments, I really appreciate it! As for making it a feature or not, I'm happy to refactor my PR without using a feature — that would require replacing Rc by Arc as well as RefCell by RwLock, since RefCell is inherently single-threaded. Using a lock is probably more impactful to performance than using Arc, but I don't have any data to back that hunch up. If it's helpful, I can look at adding a criterion.rs suite or similar to help obtain that data? With respect to BorrowWith, that's only used to offer a uniform API when the sync feature is turned on or off, and can be eliminated if sync isn't exposed as a Cargo feature. Regardless, I'm happy to remove that trait, whatever is most helpful. ♥ |
This draft PR adds a new Cargo feature that, when enabled, uses
ArcandRwLockinstead ofRcandRefCellto allow sharingDocumentsRefacross threads. In particular, this allowsDocumentsRefto be stored as a field in a struct marked with PyO3 as#[pyclass], as that attribute requiresDocumentsRef: Send + Sync.By making synchronization conditioned on a feature, existing single-thread performance can be preserved, while allowing consuming libraries that need synchronization primitives to explicitly opt-in to that functionality.
To enable the new feature, this PR also adds a new trait,
BorrowWith, that differs from thestd::borrow::Borrowtrait by allowing implementers to use custom types as borrow targets as long as those custom types satisfy the appropriate lifetimes. This trait is incomplete, missing bounds to ensure that the target types are appropriate for use as borrowed references and comments indicating the purpose of the trait; it is included regardless so as to solicit initial discussions.See also: #66