Jump to content
  • Advertisement
Sign in to follow this  
blaze02

Optimizing stdext::hash_map

This topic is 4022 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I had this idea about optimizing our app. The first thing it does is load 40,000+ variables into a hash_map. The idea is to remember how much space is needed to load everything, and reserve that space before attempting to load all the variables. If I was working with a vector, this would be easy using .size to get the amount of memory and .reserve to allocate it. But with a hash_map, it is proving much harder. Is it possible to reserve space in a hash_map? Is it possible to determine how much space a hash_map currently has allocated? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
There is a hash_map::size() function, which just returns the number of elements the map contains, but I suspect that's different from how much space the map has allocated.

I guess if it's a major concern, you could always write a custom allocator for it...

Edit: On second thought, hash_map::get_allocator().allocate() may work. I've never bothered with that stuff before, so I don't know exactly how it works, but it's worth a shot.

Share this post


Link to post
Share on other sites
I think that stl style allocators are broken for associative types.
Quote:
Quoted from: Scott Meyers Effective STL
most standard containers never ask their associated allocated for memory. Never.

Although I believe it says that types which do not have nodes (ie: non-associative containers) use allocators.

I don't know if that applies to hash_map as well since it is not standard, but I wouldn't bet on stl allocators to help you.

*edit* As for reserving space, I am certain that you are out of luck on that one.

Look here

Share this post


Link to post
Share on other sites
Hash maps usually grow at some threshold, so I would assume the amount of space to reserve would be something like:
map.size() * 1.25 //threshold = 80%

But that won't help if we can't tell it to reserve to a specific size.

As for the reserving the space, the container must be smart enough to optimize large inserts. It looks like the loading algorithm needs to load the hash_map entries into a vector with reserved size, then inserted into the hash_map all at once.

Quote:
Quoted from: Scott Meyers Effective STL
most standard containers never ask their associated allocated for memory. Never.
Where are the boost evangelists when you need them?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!