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

feat: Store file contents statically and use binary search for lookup. #217

Merged
merged 2 commits into from
Aug 23, 2023

Conversation

osiewicz
Copy link
Contributor

@osiewicz osiewicz commented Aug 20, 2023

For large directories, RustEmbed generates large amounts of LLVM IR. This is mostly caused by use of match expression with string literals.

In Rust, literals from match set are compared one by one; it is kind of like a big if-else chain. Instead, we can store a static lookup table (sorted by file name) and run a binary search over that. This should make file lookup more efficient in runtime and improve compile time too. This improves LLVM IR even for small uses of rust-embed; for examples/basic.rs a size of generated IR goes down by 7% from 4397 to 4082 lines. For a crate I'm working on this reduced LLVM IR size from 1.7M to 1.55M - RustEmbed is no longer the largest contributor to IR size.

I'd appreciate response to #216, as I had to mark __rust_embed_new as const to use it in array element initializer. This can probably be circumvented, though I think that this PR as a whole does not require a recent Rust version to work, so unless MSRV is very strict, we should be okay.

For large directories, RustEmbed generates large amounts of LLVM IR. This is mostly caused by use of match expression with string literals.

In Rust, literals from match set are compared one by one; it is kind of like a big if-else chain. Instead, we can store a static lookup table (sorted by file name) and run a binary search over that.
This should make file lookup more efficient in runtime and improve compile time too.
This improves LLVM IR even for small uses of rust-embed; for examples/basic.rs a size of generated IR goes down by 7% from 4397 to 4082 lines.
If 'compression' is enabled, the static data contains function pointers to get contents of embedded file. That is done so as to work around 'include_flate' using lazy_static.
@pyrossh pyrossh merged commit c286171 into pyrossh:master Aug 23, 2023
5 checks passed
@pyrossh
Copy link
Owner

pyrossh commented Aug 23, 2023

Will bump this to v8 as the implementation kinda changes and want to keep all the packages aligned.

@osiewicz
Copy link
Contributor Author

osiewicz commented Aug 23, 2023

Thanks!

@flosse
Copy link
Contributor

flosse commented Sep 12, 2023

Will bump this to v8 as the implementation kinda changes and want to keep all the packages aligned.

what happened to v7.x?

@pyrossh
Copy link
Owner

pyrossh commented Sep 13, 2023

Skipped it to pin all the sub packages to the same version as its getting a bit confusing which dependency matches the main library.

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.

None yet

3 participants