Add method to convert non-shared Collection/SourceCache to shared variants#541
Open
nicoburns wants to merge 3 commits intolinebender:mainfrom
Open
Add method to convert non-shared Collection/SourceCache to shared variants#541nicoburns wants to merge 3 commits intolinebender:mainfrom
Collection/SourceCache to shared variants#541nicoburns wants to merge 3 commits intolinebender:mainfrom
Conversation
5795bf4 to
3bbf724
Compare
3bbf724 to
327b128
Compare
taj-p
reviewed
Mar 10, 2026
fontique/src/collection/mod.rs
Outdated
| pub fn make_shared(&mut self) { | ||
| if self.shared.is_none() { | ||
| self.shared = Some(Arc::new(Shared { | ||
| data: Mutex::new(core::mem::take(&mut self.data)), |
Contributor
There was a problem hiding this comment.
I think there is a bug here. Do we need to increment the self.shared_version too or clone instead of take? Otherwise, local data seems to be lost on subsequent reads.
For example, this example code fails on the assert because all our reads are guarded through sync_shared. Since the version hasn't changed, we don't update our self.data.
use parley::fontique::{Collection, CollectionOptions};
fn main() {
let mut collection = Collection::new(CollectionOptions {
shared: false,
system_fonts: false,
});
let font_dirs: Vec<std::path::PathBuf> = ["/System/Library/Fonts", "/Library/Fonts"]
.iter()
.map(std::path::PathBuf::from)
.filter(|p| p.is_dir())
.collect();
if font_dirs.is_empty() {
eprintln!("No system font directories found — cannot demonstrate the bug.");
std::process::exit(1);
}
collection.load_fonts_from_paths(&font_dirs);
let names_before: Vec<String> = collection.family_names().map(String::from).collect();
collection.make_shared();
let names_after: Vec<String> = collection.family_names().map(String::from).collect();
assert_eq!(names_before.len(), names_after.len());
}
Signed-off-by: Nico Burns <nico@nicoburns.com>
Signed-off-by: Nico Burns <nico@nicoburns.com>
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.
This PR allows a
CollectionorSourceCachethat was constructed as non-shared, to be later converted to be shared. This is useful when acceptingFontContext/Collection/SourceCacheprovided by users (which may or may not be shared - as decided by the user) which you then want to "fan out" into thread-local shared clones.Blitz is using the new
make_sharedmethod onSourceCacheintroduced in this PR. Blitz is not currently using the new correspondingmake_sharedmethod onCollection, but that method is included in this PR anyway for symmetry.