-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Use BTreeMap instead of a sorted Vec #5877
Use BTreeMap instead of a sorted Vec #5877
Conversation
Turns out this saves some binary size and makes the code simpler. Signed-off-by: Daniel Egger <daniel@eggers-club.de>
Thanks! Improvement is improvement. BTreeMaps can be expensive code wise as well and i have wondered about a custom sort algorithm optimized for binary size. |
The main problem as I see it is the monomorphisation, through the use of generics. Even after disabling the suggestions feature, I still see uses of various sort functions:
but I've no idea where they're called since the only callers outside of the suggestion code are in BTW: The use of clap 4.5.24 reduced the size of my application from 5.814MB to 5.797MB. 🥳 |
I’ve used https://crates.io/crates/tiny_sort for this in some projects and it worked well. More precisely, I’ve only audited and used the unstable sort, which is sufficient when there are no duplicate keys. The stable sort seems to have more and more subtle unsafe code, so I can’t vouch for it. |
@hanna-kruppe The issue here is: I've no idea where those still existing sort functions are called from. The visible sorts are now gone (and if necessary we can switch back to using sort if that's beneficial with a dedicated sorting algorithm). Do you have any idea how to get a calltree showing where those sorts are being dragged in? I've tried putting breakpoints on them but I couldn't get them to be called at all and all the additional data rustc can output also doesn't show them... |
I’ve mostly done this sort of thing for webassembly modules with the help of twiggy (not quite a general call graph but often good enough especially when for functions only called from one place). I guess reverse engineering tools like binary ninja make this pretty easy to walk the static call graph if you’re used to them (I’m not). For a short amount of time, grepping for symbols in objdump output may be tolerable. (e.g., pick one of the |
You could also try building with |
@hanna-kruppe Fantastic advise! Thanks. 😃 Unfortunately it doesn't help with the sorts...
|
This is cargo-bloat output? You can try if |
@hanna-kruppe Yes, it's cargo-bloat. I'm aware of
|
I took the |
While looking into the still troublesome binary size I was investigating the noticeable amount of monomorphised sort functions. Looking at the few callers I noticed that they're creating custom
Vec
s and then sorting them with a custom lambda functions to sort them.As it turns out, it's a win to insert the entries into a
BTreeMap
instead of creating aVec
and manually sorting it.E.g. for the
stdio-fixture
binary, we're going from:to: