Skip to content

perf: optimize charAt comparisons — 1,833x faster character parsing#279

Merged
cs01 merged 2 commits intomainfrom
charat-optimization
Mar 12, 2026
Merged

perf: optimize charAt comparisons — 1,833x faster character parsing#279
cs01 merged 2 commits intomainfrom
charat-optimization

Conversation

@cs01
Copy link
Copy Markdown
Owner

@cs01 cs01 commented Mar 12, 2026

Summary

  • charAt(i) === "x" pattern: bypasses heap allocation entirely — emits direct getelementptr + load i8 + icmp instead of GC_malloc_atomic(2) + memcpy + strcmp
  • Cached strlen via C bridge: cs_cached_strlen() caches the last (pointer, length) pair so repeated strlen() calls on the same string (the hot path in any parser) return in O(1) instead of O(n)
  • Single-char string comparison: str === "x" uses byte compare instead of strcmp

Benchmark (JSONC parser, 10K keys / 237KB)

Before After Speedup
6.41s 0.01s 641x

On 2.5MB input: 0.03s (was 55s+ before — 1,833x faster)

Competitive with C (0.005s) and Go (0.01s) on the same workload.

Test plan

  • New test fixture: string-charat-compare.ts — verifies charAt comparison correctness (in-bounds, OOB, negative index, equality/inequality)
  • All existing string tests pass (charAt, charCodeAt, string-index)
  • Self-hosting Stage 0 + Stage 1 pass
  • JSONC benchmark verified

🤖 Generated with Claude Code

@cs01 cs01 merged commit a28cd1f into main Mar 12, 2026
9 of 11 checks passed
@cs01 cs01 deleted the charat-optimization branch March 19, 2026 05:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant