Commit graph

7 commits

Author SHA1 Message Date
Andreas Kling
b6bef6b688 Libraries: Use UTF-16 for JS-visible runtime strings
Produce JS-visible string results as UTF-16 at their source, including
numeric formatting, BigInt and BigFraction formatting, URI encoding,
console formatting, parser errors, regular expression errors, Intl and
Temporal records, LibUnicode locale boundaries, and LibWeb bindings.

Handle fractional radix formatting through the UTF-16 builder view.
2026-06-22 19:51:25 +02:00
Andreas Kling
13969b6bd4 LibJS: Store primitive strings as UTF-16
Keep primitive string storage in Utf16String and remove the UTF-8
storage path from PrimitiveString. ASCII strings still use compact
Utf16String ASCII storage, while UTF-16 becomes the only owned
representation.
2026-06-22 19:51:25 +02:00
Shannon Booth
9e58eb24de Tests/LibJS: Remove unneeded GC::Root use in PrimitiveString test 2026-05-29 17:21:17 +02:00
Aliaksandr Kalenik
74e2dac929 LibJS: Avoid ropes for short flat string concatenations
Short string concatenations are a common allocation churn pattern in JS
execution. Many of these results are immediately observed as flat
strings, so the intended win is to avoid spending GC and flattening work
on an intermediate representation that does not carry its weight.

Microbenchmark:

    const n = 10_000_000;
    function bench(a, b) {
        let total = 0;
        for (let i = 0; i < n; ++i)
            total += (a + b).length;
        if (total !== n * 2)
            throw new Error(String(total));
    }
    bench("a", "b");

Measured with hyperfine against the same build with the fast path
disabled:

    baseline:  822.7 ms +/- 25.7 ms
    optimized: 385.5 ms +/- 21.5 ms
    speedup:   2.13 +/- 0.14 times faster
2026-04-30 16:25:52 +02:00
Andreas Kling
ce8f92cf6a LibJS: Reuse cached ASCII strings for substrings
Teach the PrimitiveString substring creation path to return the
VM's preallocated single-character ASCII strings instead of always
allocating a deferred Substring.

This keeps one-code-unit ASCII substrings on the same fast path as
direct string creation, including callers like charAt and indexed
string property access.
2026-04-12 19:15:50 +02:00
Andreas Kling
f6f791969d LibJS: Use Substring for regexp results
Return JS::Substring objects from the builtin regexp exec and split
paths instead of eagerly copying UTF-16 slices into new strings.
Matches, captures, and split pieces can now point back at the original
input until someone asks for the string contents.

Add focused runtime coverage for UTF-16 captures and regex split
captures so these lazy slices stay exercised.
2026-04-11 00:35:36 +02:00
Andreas Kling
1182250414 LibJS: Add deferred PrimitiveString substrings
Introduce JS::Substring as a lazily materialized PrimitiveString
variant that stores an originating string plus a UTF-16 offset and
length. This makes substring creation cheap while still reifying to
a normal string when character data is requested.

Track which short strings actually live in the VM caches so lazily
resolved ropes and substrings do not evict unrelated cached strings
when they are finalized. Add focused unit tests for nested ranges,
rope-backed substrings, surrogate boundaries, and cache behavior.
2026-04-11 00:35:36 +02:00