Skip to content

Use Span collections in String/StringName operations to reduce memory allocation/copy #12499

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
YYF233333 opened this issue May 25, 2025 · 1 comment

Comments

@YYF233333
Copy link

Describe the project you are working on

Godot

Describe the problem or limitation you are having in your project

Godot's current String relies on COWData for its underlying storage, which results in frequent memory operations during string manipulation, often leading to performance issues. The introduction of Span makes zero-copy string modifications possible. Although in most cases this only delays the memory operation (since we eventually still need a String), for StringName, the target string may already exist, in which case we can simply increase the reference count to avoid copying and memory allocation.

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

A span collection is a reference-type object whose lifetime matches the shortest-lived underlying String it references. It's somewhat similar to a LocalVector<Span>, but with a String-like interface. It enables zero-copy string concatenation and supports certain read-only operations, such as hash computation (as long as you don't need raw pointers). In certain scenarios, deferring memory operations can potentially eliminate the need for them altogether (as in #106802) or reduce the number of operations required (as in #99929).

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

A naive implementation would be LocalVector<Span<char32_t>>, providing operator[] and operator+=. I've been a bit busy lately, but I'll write a concrete implementation when I have time.

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

It is core.

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

It is core.

@Ivorforce
Copy link
Member

Ivorforce commented May 28, 2025

I was definitely planning on moving read-based String operations to a StringSpan interface, such that they can be used on views without copying. That should reduce many unnecessary copies already.

A vector-like type of string spans seems more niché to me. I'm guessing you're proposing this for godotengine/godot#106802, where it definitely makes sense (though perhaps a Span<Span<char32_t>> would suffice here).
For godotengine/godot#99929, a more complex solution is needed, because not all string parts are made of the same stuff. Eventually, I'd like to support concatenating strings, numbers, and spans all while reducing allocations. This should be possible with typed arguments, but requires removing implicit to-String conversions, as I've found from previous experiments.

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