Skip to content

Tokeniser: numbers read where they are put on the stack - #71

Merged
AOShei merged 2 commits into
mainfrom
tokeniser
Nov 22, 2026
Merged

AOShei merged 2 commits into
mainfrom
tokeniser

Conversation

@AOShei

@AOShei AOShei commented Nov 22, 2026

Copy link
Copy Markdown
Owner

The speed item from the profiling that's worth doing on batch workloads. No change to output.

Why this one. I profiled one worker reading the 1,089 files of the public corpus's tuning set:

Stage Share of time
Decompression 20.1%
Content tokeniser 16.3%
Layout 15.7%
Object parsing 10.5%
Fonts, all of it 6.5%
— Unicode maps (ToUnicode CMaps) 1.3%

A faster Unicode-map reader could save at most 1.3% of a batch, so I left it out. The tokeniser was the largest stage on the list.

What changes in internal/content/scanner.go:

  • Next classes bytes with a table of its own: the classes of package syntax, with the comment sign and the bytes that start a number kept apart.
  • Next reads numbers, the most common operand, itself, and puts each on the operand stack where it's made. Before, numbers went through operand, and the 40-byte result was copied twice.
  • The arrays of TJ read their numbers the same way. Every other operand is read as before.

Results:

  • Tuning set, one worker: 17.31 G → 16.52 G instructions (−4.5%). The tokeniser's share of the time falls from 16% to 10%.
  • BenchmarkScanner: 47.6 M → 37.9 M instructions (−20%), 2.15 → 1.35 ms.

Same output:

  • Differential tests. TestSameAsBefore and the new FuzzSameAsBefore compare Next with the reader as it was, kept in the test as nextRef. They check every operator, operand, array element, inline image and counter, with the page whole and split over two streams.

  • Corpus. The JSON output, warnings included, has the same SHA-256 as on main for all 3,253 files of the corpus cache (public corpus, pdf.js, PDFium, the six soak sets) and the fixtures.

  • Fuzzing with -fuzzminimizetime 0s, nothing found:

    Target Time Inputs
    FuzzSameAsBefore 10 min 105 million
    FuzzScanner 5 min 79 million
    FuzzExtract 5 min 29 million
    FuzzOpen 5 min —
    FuzzPage 5 min 7 million
  • The new target is listed in .github/fuzz-targets.txt, and the full test suite passes.

Not done: going further would mean changing what an operand is (offsets instead of a byte slice), which touches every use in the interpreter, for perhaps 1–2% more.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DzYLyEzLqkwuqPQ9LXgMpD

AOShei and others added 2 commits November 22, 2026 18:02
content.Scanner.Next classes bytes with a table of its own, the classes
of package syntax with the comment sign and the bytes that start a
number apart, and reads a number itself, putting it on the operand
stack where it is made, rather than through operand, whose result was
copied twice. The arrays of TJ read their numbers the same way. The
other operands are read as before.

What it gives is unchanged: TestSameAsBefore and FuzzSameAsBefore
compare it, operator by operator, with the reader as it was, kept in
the test; and the JSON of all 3,253 files of the corpus cache and the
fixtures has the same hash as on main.

The tokeniser took 16 percent of the time to read the tuning set of the
public corpus on one worker, and takes 10; the tuning set takes 4.5
percent fewer instructions (17.31 G to 16.52 G), BenchmarkScanner 20
percent fewer (47.6 M to 37.9 M).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DzYLyEzLqkwuqPQ9LXgMpD
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DzYLyEzLqkwuqPQ9LXgMpD
@AOShei
AOShei merged commit 16c0ab6 into main Nov 22, 2026
12 checks passed
@AOShei
AOShei deleted the tokeniser branch November 22, 2026 17:57
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