electronicarts / EASTL

EASTL stands for Electronic Arts Standard Template Library. It is an extensive and robust implementation that has an emphasis on high performance.
BSD 3-Clause "New" or "Revised" License
8.09k stars 929 forks source link

[Question] Should eastl::unordered_set.reserve not check if a change is required? #521

Open ScottDennison opened 1 year ago

ScottDennison commented 1 year ago

eastl::unordered_set is implemented behind the scenes as a eastl::hashtable

The various eastl colleciton classes support the method reserve to reserve enough space to hold data that is intended to be inserted, to prevent repeated allocations. In most cases, a check is made to see if any allocations would actually need to be done, and if not, the method exits without making any changes.

eastl::hashtable on the otherhand blindly always calls rehash, which calls DoRehash, which always does allocations, copies and frees, even if the amount of buckets that were calculated as being needed is the same as before. It also will happily accept reducing the amount of buckets if reserve was passed in a size that was less than the current size.

I herefore raise:

  1. Why does reserve() not bail out if the size provided is less than or equal to the current size.
  2. Why does reserve() also not bail out if, while the size provided is greater than the current size, there is no change required to the number of buckets, and therefore no reallocations required.

I have opened this as a question as opposed to providing a pull request, as I am primarily a Java developer and therefore don't consider myself knowledgeable with EASTL enough to know if there is a valid reason for this behaviour to exist.