Use Span
collections in String
/StringName
operations to reduce memory allocation/copy
#12499
Labels
Span
collections in String
/StringName
operations to reduce memory allocation/copy
#12499
Describe the project you are working on
Godot
Describe the problem or limitation you are having in your project
Godot's current
String
relies onCOWData
for its underlying storage, which results in frequent memory operations during string manipulation, often leading to performance issues. The introduction ofSpan
makes zero-copy string modifications possible. Although in most cases this only delays the memory operation (since we eventually still need aString
), forStringName
, 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 aLocalVector<Span>
, but with aString
-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>>
, providingoperator[]
andoperator+=
. 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.
The text was updated successfully, but these errors were encountered: