Skip to content
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

Feature Request: steal backing array in flat map/set #22

Open
yonik opened this issue Jan 13, 2023 · 4 comments
Open

Feature Request: steal backing array in flat map/set #22

yonik opened this issue Jan 13, 2023 · 4 comments

Comments

@yonik
Copy link

yonik commented Jan 13, 2023

One feature that would greatly decrease peak memory usage in my use-case is to have the ability to "steal" the backing array from the flat-maps (after compacting them into a "normal" array first of course.)

The use-case is currently implemented as follows:

  1. add / update many objects in a flat_hash_set
  2. allocate a new array, iterate over the flat_hash_set, adding all to the array
  3. delete the flat_hash_set
  4. sort the array of objects

Perhaps something along the lines of:

// Removes and returns an array of all entries in the set.  Optimized implementations may re-use an internal array, thus
// lowering memory usage.  Only the first size() entries are defined.  It is the callers responsibility to delete the returned array.
value_type* remove_all_entries()

Oh, and thank you for the inspired lazy_emplace()! It's still more powerful than other map/set implementations of try_emplace since key creation can be deferred or customized as well.

@greg7mdp
Copy link
Owner

greg7mdp commented Jan 13, 2023

Thanks for the kind words!

have the ability to "steal" the backing array from the flat-maps (after compacting them into a "normal" array first of course.)

Should not be too difficult to do. Let me look into it.

@greg7mdp
Copy link
Owner

greg7mdp commented Jan 13, 2023

Hum, there is the issue of the allocator used for allocating the array inside the flat_hash_set. If you steal the buffer, then you also need to use the same allocator to free the memory buffer eventually. I wish there was a way to construct a std::vector from a pointer, size, capacity and optional allocator. I don't want to implement my own gtl::vector.

But it would be nice, we could have a gtl::vector which could be constructed using:

vector::vector(gtl::flat_hash_set&&);

and that move constructor would steal the underlying array (after compaction).

@yonik
Copy link
Author

yonik commented Jan 14, 2023

Hum, there is the issue of the allocator used for allocating the array inside the flat_hash_set

Yeah, I figured it could be a super-low-level expert method where the caller may even be in charge of calling individual destructors (making it possible is better than nothing at all IMO). Either that, or as you suggest, your own little vector-like class.

@greg7mdp
Copy link
Owner

OK, I did the first part, added gtl::vector.

Repository owner deleted a comment Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants