Skip to content

JS String.join and concat assemble fragment ropes through Base #1676

Closed as not planned

Description

@oxura

The JS backend still calls the Base fragment-joining functions even though each fragment is already a JS string. Passing the fragment references to the native join avoids the per-piece rope-building path. Empty and singleton lists must not acquire an unnecessary array allocation.

On e5527d5 (Bend 2.0.37), Linux x64, Bun 1.3.13, I measured actual exported module calls. The table uses medians of seven interleaved before/after rounds with identical input, including module conversion; no standalone JS reference is substituted for the patched compiler.

Export Calls Current main, ms Local native path, ms
join 300 2.969 1.401
concat 300 1.570 1.157

Library (lib.bend):

import Base

def join(xs: List<&2, String>, sep: String) -> String:
  String.join(xs, sep)

def concat(xs: List<&2, String>) -> String:
  String.concat(xs)

Compile with bun bend2/main.ts lib.bend -o lib.mjs. This standalone driver (bench.mjs) reproduces the baseline workload; run it with bun bench.mjs:

const api = (await import(process.argv[2] ?? './lib.mjs')).default;
const list = xs => xs.reduceRight((tail, head) => ({$: 'Con', head, tail}), {$: 'Nil'});
const parts = list(Array.from({length: 300}, (_, i) => ['ab', '', '😀', 'Z'][i % 4]));
const cases = [['join', [parts, ','], 300], ['concat', [parts], 300]];
let sink;
for (const [name, args, calls] of cases) {
  for (let i = 0; i < 30; i++) sink = api[name](...args);
  const samples = [];
  for (let round = 0; round < 7; round++) {
    const start = performance.now();
    for (let i = 0; i < calls; i++) sink = api[name](...args);
    samples.push(performance.now() - start);
  }
  console.log(name, samples.sort((a, b) => a - b)[3]);
}
console.log('sink:', typeof sink);

The local implementation passed 2936 API checks covering full-width U32 Chars, all 1024 astral scalars with low unit DC00, first-class references, and numeric bounds. Existing string/read programs and a new packed-Char fixture also ran on JS and native C. These are local workloads, not a general speed guarantee. No parser, kernel or cap change is needed.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions