refactor(mercury): avoid path existence checks with once cell#741
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
There was a problem hiding this comment.
Copilot reviewed 1 out of 1 changed files in this pull request and generated no suggestions.
Comments skipped due to low confidence (1)
mercury/src/internal/pack/cache.rs:86
- Using
unwrap()withincall_oncecould cause a panic if directory creation fails. Consider handling the error more gracefully.
self.path_prefixes[hash.as_ref()[0] as usize].call_once(|| { fs::create_dir(&path).unwrap(); });
|
It looks good, reducing file system access and improving performance. While it does add a bit of complexity, I was wondering if it might be a good idea to pre-create these 256 folders during the initialization phase to achieve the best possible performance? |
Thank you for the suggestion. However, I believe lazy initialization with |
Well, you’re right, not all 256 folders will be used, but the overhead of creating them during initialization is insignificant compared to the overall Plus, this approach removes any runtime checks, which significantly boosts performance and avoids potential race conditions. Also, I think there’s no real difference in elegance between initializing 256 folders and 256
For this, I think it could be a good idea. |
|
uh, this pr is merged much earlier than I have expected... I would like to continue working on the idea mentioned above if appropriate. |
Delaying the creation of folders until the drop stage sounds like a good idea, as not all objects need to be saved to a file when dropped—especially for smaller packs. However, modifying the code to add an additional member to each ArcWrapper seems a bit excessive and complex. |
The default behavior is to delete the cached folders after decoding is complete, meaning these 256 folders only exist for an average of a few dozen seconds, or even less, which won't cause any inconvenience to the user.
Creating 256 empty folders should be nothing for modern operating systems, while the benefits it brings are reduced code complexity and fewer runtime checks.
I agree with Hou's perspective. While creating folders at the time of drop may sound elegant, the additional complexity it introduces would reduce code maintainability and increase the likelihood of bugs. In my opinion, less, simpler, and more maintainable code is also an important criterion for elegance. |

Avoids expensive file (stat) operations, no atomicity issues this time.
Time spent on
mercury::internal::pack::cache::Caches::generate_temp_pathreproducibly reduced from ~2.7% to ~1.4% for the testinternal::pack::decode::tests::test_decode_large_file_asyncafter applying this patch.