Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 19 Oct 2007
Offline Last Active Mar 29 2015 08:05 AM

Posts I've Made

In Topic: My Custom Memory Pool Seems Too Slow. Need Advice.

19 April 2014 - 09:10 AM

After the clarification about the template issue, I tried to remove the map and decided to use the template instead. It removes the needs to find which pool I have to allocate (due to map), which increases the performance slightly (up to 0.1 sec) compare to the one that uses map and without template. If I reserve the capacity (by the total number of the test), it is reduced to 0.23 sec! The number of classes that would need this would probably at hundreds or less, unless doing such template that way is very expensive (I don't know).


EDIT: As Waterlimon suggested, I've changed the benchmark against the Car class to be quite a bit the same as Entity. The Car class now inherits Vehicle, where it has to pass a string through its constructor and save it to a name variable. Apparently this is the culprit. Now the Car increases up to 0.5x seconds!

Now everything looks quite normal, as a matter of fact, better!


Thanks a bunch guys!! you guys are very helpful. laugh.png

In Topic: My Custom Memory Pool Seems Too Slow. Need Advice.

19 April 2014 - 08:02 AM



You might want to consider a less intrusive option. Your classes probably should not know where and how they are allocated.


Any idea of the usage? I don't seem to understand the less intrusive option should be. Is it because it has to inherit Object with template?



The key issue is that MemoryPool<Foo> is a different type than MemoryPool<Bar>. This means that static members are not shared between the two different pools.


Oh my I didn't know that at all. Well that clears up what Waterlimon suggested then, that memoryPool probably won't need to be a template. laugh.png


I guess I have to learn more on how this template works. Thanks ApochPiQ! biggrin.png

In Topic: My Custom Memory Pool Seems Too Slow. Need Advice.

19 April 2014 - 07:32 AM


I have used the Very Sleepy profiler (not part of visual studio) to do my profiling. I dont think visual studio express edition has a profiler.


I understand what you intend to do with the map, but to me it seems as if you would have 3 maps: 1 for A, 1 for B, 1 for C, because the MemoryPool class itself is templated. (so you have MemoryPool<A> with its map, MemoryPool<B> with another map etc.)


Looking at the code it seems as if the MemoryPool class itself does not need to be a template. Only some of the functions (the allocation ones) need to be. This would probably fix it.


I'll check on that profiler, thanks!


That's the funny thing though, the memoryPool in Object is static, so I am assuming it is only one. What I thought is (here's the dangerous part), the memoryPool attached to Object would adapt to whichever who wants to use it (A, B, or C). So again, since memoryPool is only one as static, I assume that the map in memoryPool is shared and dynamically adapt whoever is using it whether it's A, B, or C. But I can't really tell if this is correct or not (anyone please clarify).


Hmm I thought about that too, I'll give in another shot.

In Topic: My Custom Memory Pool Seems Too Slow. Need Advice.

19 April 2014 - 07:17 AM

In your memory pool's alloc function, specificPool is copied by value from the map on each allocation. I suspect this is a bug and what you really want is to have specificPool be a pointer to a vector in the map. Is that correct?


I guess you're right, I can optimize that part. I'll let you know. EDIT: Tried it! it reduces by 0.3 sec! Thanks. (happy.png )



Do I understand this correctly:

-Entity is Object<Entity>

-Instances of Object<Entity> have MemoryPool<Entity>

-Only objects of type Entity can be allocated into that specific pool


I dont see how the map works here, wont every instantiation of MemoryPool only allow a single type to be allocated anyways, which would mean that there will only every be a single key in the map? Im not very used to template code and static members so i might have misundestood something.

Anyways, I do have suggestions, assuming there is nothing fundamentally wrong in how the pool works:

-Would it be possible to use the hash_code of the type_info struct instead of the name for the map key? This way you wouldnt need to use std::string but could have an int instead which might be faster.

-Reserve some capacity into the vectors when you create them so they dont need to reallocate all the time

-You could try using an unorderer_set instead of a map, access is about O(1) I think. If the map access is a bottleneck.

-In your test code, make sure that the objects you compare are actually the same. In the code you posted, car is probably a single byte, while your Entity might even allocate memory because of the vector contained.


And of course, use a profiler to figure out what it is you are doing that is slowing down your pool.


The idea of the map is to avoid that specific situation. Say I have A, B, C class that inherits Object, I assume that Object would be generic on each of them, like Object<A>, Object<B>, Object<C>. So the map contains vectors of pointers for each A, B, and C when required. Say we're allocating memory for A, if the map is not yet have vectors of pointers for A objects, then create such vector. Then A would have its own pool. Whenever Pool for A is filled, when allocating new As, it will automatically reuse the pointers from the vector inside map, which has they key for A.


1. Hmm I haven't check on hash_code yet, maybe it would. Yes! probably string is the case.

2. Will try!

3. I'll try that, yeah probably the map. Thanks for pointing it out!

4. You're right. I was probably got terrified already with the number. I'll post out with comparable items. Still,1.8 sec for such test is somehow unexpected, but that's why I open this thread to make sure if it's okay or not okay.


EDIT: Using all the first 3 suggestions (including ApochPiQ suggestion), it drops to 0.6 seconds for 100K iterations of allocating the Entity! But I used unordered_map here instead of unordered_set, because I am unable to track which object to pool without a key. I'll find a way for that. And also I hardcoded the capacity reserve for vector according to the test. I have to find out on how this should work in real application. Anyway, without reserving the capacity, I will get 0.95x seconds.


I am so not knowing how to use this IDE (Visual Studio 2013), is it related to that? do you mind telling me about this profiler stuff?

In Topic: Fire from Afar

05 January 2014 - 08:57 PM

Thanks for the feedback and thorough solutions JonasB! wub.png  yeah I think I'm too dependent on the instrument itself regardless of its weaknesses, hoping that it already throws its best sound. I still don't know either I have to get better instruments or playing with frequencies/layering as you suggested, nor still scratching whether my current instruments are already the best that I got. But I think I'll go for the cheaper solution first (playing with freqs/layering! :D). Still learning how to mix stuff better. wacko.png