Skip to content

perf: add ASCII fast path in parseGround#287

Open
dyxushuai wants to merge 2 commits intorockorager:mainfrom
dyxushuai:fix/parser-ascii-fastpath
Open

perf: add ASCII fast path in parseGround#287
dyxushuai wants to merge 2 commits intorockorager:mainfrom
dyxushuai:fix/parser-ascii-fastpath

Conversation

@dyxushuai
Copy link
Contributor

@dyxushuai dyxushuai commented Dec 28, 2025

Problem

parseGround handles the common ASCII keypress path by running UTF-8 decoding and grapheme iteration even for single-byte printable ASCII. This adds avoidable overhead for each keystroke.

Fix

Add a fast path for printable ASCII (0x20..0x7E) that returns a keypress directly. We only take the fast path when the next byte is ASCII (or absent) to avoid multi-codepoint graphemes like combining marks/keycap sequences that start with ASCII but continue with non-ASCII bytes. Control bytes and ESC keep the existing mapping rules.

Bench (local, zig build bench, iterations=200, 80x24)

Baseline = pre-fast-path work (UTF-8 + grapheme iteration)
Fast = current parseGround fast path

Case Baseline ns/frame Fast ns/frame Improvement Speedup
ASCII 'a' 194 51 -73.7% 3.80x
Mixed stream (ASCII + CSI + UTF-8) 5,465 3,464 -36.6% 1.58x

Improvement: ASCII 'a' -73.7% (3.80x); Mixed stream -36.6% (1.58x).

Tests

  • zig build test
  • zig build bench

Copilot AI review requested due to automatic review settings December 28, 2025 14:38
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a performance optimization for parsing ASCII keypress events by introducing a fast path that bypasses UTF-8 decoding and grapheme iteration for printable ASCII characters (0x20-0x7E). The fast path only applies when the next byte is also ASCII or absent, ensuring multi-codepoint graphemes like combining marks and keycap sequences that start with ASCII are correctly handled by the slower path.

Key changes:

  • Added fast path in parseGround for printable ASCII with safety check for multi-byte sequences
  • Added benchmark comparison showing 3.35x speedup for ASCII input
  • Added uucode dependency to benchmark imports for baseline measurement

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/Parser.zig Adds fast path for ASCII characters (0x20-0x7E) that skips UTF-8/grapheme processing when next byte is ASCII or absent
bench/bench.zig Adds baseline benchmark function and comparison tests for ASCII parsing performance
build.zig Adds uucode module import to benchmark executable for baseline comparison

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dyxushuai dyxushuai marked this pull request as draft December 28, 2025 15:11
@dyxushuai dyxushuai marked this pull request as ready for review December 28, 2025 15:22
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