Reduce LfnBuffer size from 24 to 16 bytes on 32-bits sys.#218
Merged
thejpster merged 3 commits intorust-embedded-community:developfrom Jan 23, 2026
Merged
Conversation
edee1b9 to
15a996b
Compare
And from 32 bytes to 24 bytes on 64bits systems. The reduction comes from the observation that the required buffer doesn't need to exceed the maximum bytes required to represent a lfn. A lfn has at most 255 UTF-16 characters. The buffer holds UTF-8 characters. A UTF-8 character takes at most 3 bytes. So, the buffer doesn't need to exceed 255*3 = 765 bytes. Thus, `free` can be stored in a u16. To avoid a breaking change, we accept buffer larger than u16::MAX and just slice it to retain at most u16::MAX bytes. Note that the optimization could go beyond by replacing `inner` with a pointer to the data and storing the length in a u16. `unpaired_surrogate` could also be a `Option<NonZeroU16>` because `0` is not a valid surrogate character. Finally we could encode the `overflow` bool in the msb of `free`. All these optimizations make the code too complex IMO.
Member
|
Thank you for the PR. Looking at this, I think I can just about justify the complexity for the memory saving. But perhaps you could add a function like: That would avoid the need to do so many u16 to usize conversions in the code. Have you found the eight byte saving to be significant in your application? |
Contributor
Author
The gain is not significative at an application level. However, I thought it was a good tradeoff between complexity/gain. If you think it is not worth the complexity, feel free to reject it :) |
thejpster
approved these changes
Jan 23, 2026
Merged
via the queue into
rust-embedded-community:develop
with commit Jan 23, 2026
8d30ebc
7 checks passed
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.
LfnBuffersize from 24 to 16 bytes on 32-bits sys.LfnBuffersize from 32 to 24 bytes on 64-bits sys.The reduction comes from the observation that the required buffer doesn't need to exceed the maximum bytes required to represent a LFN. A LFN has at most 255 UTF-16 characters. The buffer holds UTF-8 characters. A UTF-8 character takes at most 3 bytes. So, the buffer doesn't need to exceed
255*3bytes. Thus,freecan be stored in a u16.To avoid a breaking change, the buffer is sliced in order to retain at most
u16::MAXbytes.Note that the optimization could go beyond, however this could become too complex IMO.