-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Switch to ImDocument in BevyManifest
#18272
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
Conversation
Reduces overhead compared to creating a `DocumentMut` and better communicates intent.
| } | ||
| } | ||
|
|
||
| let key = manifest_path.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: is there a reason the definition of key is up here and not by its usage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Purely because it's a boxed string (so cloning requires allocation), and we take a write lock. Similar to the filesystem operations, allocating involves syscalls and can block, so in general you want to do as little within the locked scope as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved it right above the write lock to satisfy both constraints!
|
Leaving this for @cart to merge for now, mostly because I think it's good for him to see it :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good calls all around!
Objective
When reviewing #18263, I noticed that
BevyManifestinternally stores aDocumentMut, a mutable TOML document, instead of anImDocument, an immutable one. The process of creating aDocumentMutfirst involves creating aImDocumentand then cloning all the referenced spans of text into their own allocations (internally referred to asdespanintoml_edit). As such, using aDocumentMutwithout mutation is strictly additional overhead.In addition, I noticed that the filesystem operations associated with reading a manifest and parsing it were written to be completed while a write-lock was held on
MANIFESTS. This likely doesn't translate into a performance or deadlock issue as the manifest files are generally small and can be read quickly, but it is generally considered a bad practice.Solution
ImDocument<Box<str>>instead ofDocumentMutBevyManifest::sharedto minimise time spent holding open the write-lock onMANIFESTSTesting
Notes
I wasn't able to measure a meaningful performance difference with this PR, so this is purely a code quality change and not one for performance.