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

Always use String for StringName internally #11517

Open
Ivorforce opened this issue Jan 8, 2025 · 1 comment
Open

Always use String for StringName internally #11517

Ivorforce opened this issue Jan 8, 2025 · 1 comment

Comments

@Ivorforce
Copy link

Ivorforce commented Jan 8, 2025

Describe the project you are working on

Godot performance.

Describe the problem or limitation you are having in your project

Godot is currently using a split implementation for StringName internally:

  • StringName initialized from static C strings is using const char *.
  • StringName initialized from Strings, and unequal to a static C string on initialization, is using String.

Using a split has 3 downsides:

  • StringName to String conversion needs to copy the string anew for each invocation.
  • StringName implementations are somewhat slower because they need to jump, leading to branch mispredictions, adding a small cost to calls.
  • StringName char * paths are slower because length is not known (though this could be solved with spans / StrRange).

Using a split has two upsides:

  • StringName creation from static strings is fast, because no string has to be allocated
    • The difference applies only when no previous StringName had existed for the string. In this case, the StringName has to be inserted into the database, and there is overhead anyway.
  • StringName of static C strings needs less RAM because they can point to char* of the source code.

I have measured the potential RAM use of StringName of static C strings and conversion call counts on a launch through the project launcher and into a project with 10k nodes.
RAM use was measured at 1mb (~200k characters) from 12k strings.
There were 40k calls to operator String(), each allocating new memory for a string.

It is somewhat questionable if 12k different static c string StringNames should even exist in Godot; I hope somebody has an insight into that. I suspect the StaticCString constructor may be misused for translations or similar, skewing the numbers.

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

Godot could switch to always use String internally in StringName.
This would cost it upwards of 1mb in memory, but speed up String interoperation.

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

N/A

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?

It's core.

@Calinou Calinou changed the title Always use String for StringName internally. Always use String for StringName internally Jan 8, 2025
@Calinou
Copy link
Member

Calinou commented Jan 8, 2025

It is somewhat questionable if 12k different static c string StringNames should even exist in Godot; I hope somebody has an insight into that. I suspect the StaticCString constructor may be misused for translations or similar, skewing the numbers.

You can run Godot with --debug-stringnames so it prints statistics on StringName usage on exit.

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