-
Notifications
You must be signed in to change notification settings - Fork 211
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
parity-util-mem: optimize MallocSizeOf for flat collections #398
Conversation
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 did not think of doing it this way, but it LGTM.
A possible variation would be to do :
fn constant_size() -> Option<usize>
, which would allow other optimizations if the collection got a efficient method to return its size (usually).
I've thought about this as well, but unless |
Implemented this because why not :) |
just thought it could be an associated constant (locking trait object use case, but I am not sure if using mallocsizeof as trait object could be useful). |
Is there any benefit of doing that? Both cases are optimized at compile time in release mode, so if there is no benefit, I'd prefer to keep as is and keep the ability to create |
Not that much, gives stronger guaranties, so we are sure there will be no bad implementation of the method. |
not sure I follow, could you elaborate how can no bad impl be guaranteed with the assoc const? |
The associated constant strictly needs to be a constant. |
I see, that's a contrived example though, an easier way to shoot oneself in the foot is to use |
During investigation of slowness in
MemoryDB::size_of
, I've noticed that we iterate over collections when calculating size even if values havesize_of
of 0, e.g. forHashMap<u32, u8>
.To avoid iterations in this case, a new static method
fn size_of_is_zero()
is added toMallocSizeOf
.I've considered another approach with a marker trait, but it didn't work due to the lack of specialization.
I've noticed that the compiler is smart enough to optimize this for
Vec
's already, but not for other collections: https://rust.godbolt.org/z/2jNo4c.