Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

m(breaking): Simplify internal storage and lookup mechanisms, take 2 #46

Closed
wants to merge 1 commit into from

Conversation

notgull
Copy link
Contributor

@notgull notgull commented Feb 18, 2023

Rather than using slab like in #45, this PR just uses the ID as an index. This leaks memory when fonts are removed.

@@ -88,15 +88,15 @@ pub use ttf_parser::Width as Stretch;
/// as different strings, but does not make any guarantees about format or
/// content of the strings.
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, Debug)]
pub struct ID(u32);
pub struct ID(usize);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can keep it as u32. No one will have more that 4M font faces.

@@ -450,12 +445,11 @@ impl Database {
/// after loading a large directory with fonts.
/// Or a specific face from a font.
pub fn remove_face(&mut self, id: ID) -> bool {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should return bool anymore.

And we should rename it to disable_face.

@@ -714,6 +708,9 @@ pub struct FaceInfo {

/// Indicates that the font face is monospaced.
pub monospaced: bool,

/// Whether or not this font face has been disabled.
pub disabled: bool,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A disabled face should be ignored during querying.

@RazrFalcon
Copy link
Owner

While a simple disabled flag is fine, it will "leak" memory in case of Source::Binary.
Adding Source::None instead seems like a way more complicated change.

Will have to think about it.

I'm trying to think about a use case when someone would be constantly adding and removing fonts to a database. And I can think of one in case of SVG with embedded fonts, in which case removal of a font is necessary....

We need a 0(1) indexing, strong/unique indices and an ability to remove a face... Looks like a generational arena to me. It's better than slab, since there are no ABA problem anymore, but we also loose &[T] and our ID is 16 bytes instead of 4 now.

In the end, fontdb is deceptively simple. It's actually surprisingly hard to have "just a list of fonts".

@notgull
Copy link
Contributor Author

notgull commented Feb 19, 2023

but we also loose &[T] and our ID is 16 bytes instead of 4 now.

We could use slotmap, which is similar to generational-arena but uses an 8-byte key instead of a 16-byte one.

@RazrFalcon
Copy link
Owner

Yes, it seems like slotmap is our own solution. It will break the API a bit and would require MSRV bump as well.

@notgull
Copy link
Contributor Author

notgull commented Feb 19, 2023

Superseded by #47

@notgull notgull closed this Feb 19, 2023
@notgull notgull deleted the id-index branch February 19, 2023 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants