You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Avoid string allocation for unchanged case conversion and whole-string substrings #194
String::toLowerCase and toUpperCase allocate a result even when no character changes. String::substr also duplicates the backing store when the normalized range covers the complete string. substring delegates to substr.
I checked the current implementation at 03a4e8d with native probes on Linux x86_64 through WSL, Haxe 4.3.3, HXCPP_M64 and hxcpp_smart_strings:
already_lowercase_123.toLowerCase() returned equal content with a different backing pointer.
ALREADY_UPPERCASE_123.toUpperCase() did the same.
substr(0, length) on the lowercase input also returned equal content with a different backing pointer.
An illustrative run of 10000 unchanged lowercase conversions of a 4096-byte string took 25.464 ms and produced the expected total output length of 40960000. This is a single local measurement, not a stable baseline or an estimated speedup.
The whole-range substring case can return the original string after the existing argument normalization. For case conversion, consider scanning for the first character that changes before allocating. If none changes, return the original; otherwise allocate once, copy the prefix and convert the remainder. This avoids a separate full pre-scan on the changed path.
Compare unchanged identifiers, already-normalized protocol strings, short mixed-case strings and long text. Measure allocation counts and GC activity as well as elapsed time. Keep null/empty behavior, Unicode mappings and the existing wide-to-ASCII normalization behavior covered, including the equality fix in #101. Do not expand this into arbitrary substring views that retain a much larger source buffer.
Related: #151 for benchmark coverage. The opportunity is specifically avoiding copies for operations whose result is the complete original string.
Activity
Skip the copy when case conversion or substr changes nothing