Skip to content

Bind HashMap::reserve() to Dictionary #12509

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

Open
dementive opened this issue May 27, 2025 · 2 comments
Open

Bind HashMap::reserve() to Dictionary #12509

dementive opened this issue May 27, 2025 · 2 comments

Comments

@dementive
Copy link

Describe the project you are working on

Prototyping a game.

Describe the problem or limitation you are having in your project

Currently Dictionary has no way to reserve a know amount of memory, but HashMap does with the reserve() function.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Currently when inserting a large number of elements into an Array the ideal way to do it would be to first resize and then add elements instead of just using push_back. Dictionary should be able to do the same thing. Adding the reserve method to Dictionary would make it so you can avoid a ton unnecessary memory allocations and rehashes in languages outside of C++, where you can’t just use HashMap directly. Not reserving memory for the HashMap is probably worse than not doing it for Array too since it mallocs a new block of memory and copies the old data in on every resize instead of reallocing like Array does.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Should be simple to add just bind the method in Dictionary and have it call the reserve method from it’s DictionaryPrivate then write some documentation and maybe add a test. I could write the code to do it if approved.

If this enhancement will not be used often, can it be worked around with a few lines of script?

No.

Is there a reason why this should be core and not an add-on in the asset library?

Not possible.

@Calinou
Copy link
Member

Calinou commented May 27, 2025

@Calinou Calinou changed the title Bind HashMap::reserve to Dictionary Bind HashMap::reserve() to Dictionary May 27, 2025
@Ivorforce
Copy link
Member

Ivorforce commented May 28, 2025

GDScript has a lot of overhead, so not all functions that are important in C++ are important in GDScript. Before exposing reserve, we should measure whether it makes an important enough difference to warrant the additional complexity for GDScript users.

(Although, I suppose, it may still be worth it for C++ and Rust users, where it will definitely make a large difference)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants