Skip UTF-8 character decoding in String.crlfToLf when input has no CR byte

Open Beginner friendly
#16,958 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
performance

Research direction

Find the implementation of String.crlfToLf and its normalization regression checks in the Lean 4 repository. Read how the current code scans UTF-8, then add the byte scan for ASCII CR before the existing normalization path. Run the regression suite described in the issue and confirm its byte and Lake text hash checks still pass.

Written by the indexing model from the issue text.

Description

P-medium

String.crlfToLf already returns its input if it finds no CRLF pair. It
currently decodes and advances over every UTF-8 character to reach that
decision. Lake calls it in Hash.ofText when checking source files, including
on a warm build.

Scan the string's existing UTF-8 bytes for ASCII CR. If none is present, return
the input immediately. If a CR is present, use the existing normalization
code. The CR byte cannot occur inside a multibyte UTF-8 character.

Reproduction

Save the following as Crlf.lean and run lean --run Crlf.lean with the
toolchain under test. It verifies the output bytes before timing normalization
of a large LF-only input.

def main : IO Unit := do
  let cases := ["", "a\nb", "a\r\nb", "a\rb", "\r\r\n\n\r",
                "α\n中文🙂", "α\r\n中文\r🙂", "\x00\r\n"]
  for s in cases do
    unless s.crlfToLf.toByteArray == (s.replace "\r\n" "\n").toByteArray do
      throw <| IO.userError s!"Unexpected normalization for {repr s}"
  let line := "def value : Nat := 42\n"
  let bytes := (String.join (List.replicate 50000 line)).toByteArray
  let some source := String.fromUTF8? bytes
    | throw <| IO.userError "Invalid UTF-8 in fixture"
  let sourceRef ← IO.mkRef source
  let start ← IO.monoNanosNow
  let mut size := 0
  for _ in [:100] do
    let source ← sourceRef.get
    size := size + source.crlfToLf.utf8ByteSize
  let elapsed := (← IO.monoNanosNow) - start
  IO.println s!"Normalized {size} bytes in {elapsed} ns"

Validation

A stock Lean 4.36.0 warm build checked 9,118 Mathlib-workspace sources
(102.8 MB). All used LF without any CR byte. An isolated A–B–B–A experiment
using a native byte-scan prototype reduced mean elapsed time from 6.355 s to
5.790 s (8.9%). That experiment evaluated the fast-path idea. The byte scan in
this patch is implemented in Lean.

The regression check covers eight explicit cases and 9,331 generated strings
across ASCII, CR, LF, NUL and multibyte UTF-8. It checks both normalized bytes
and Lake's text hash against the expected normalized string.

The actual candidate was also tested in a Stage2 Release SDK containing
both this change and a Lake import trace optimization. Against the official
4.36.0 Linux x86-64 SDK, the Mathlib warm no-op went from 6.571 s to 5.030 s
(23.5% lower), with Lake CPU time dropping from 13.380 s to 11.437 s. After
editing one line, the first compiler fork moved from 6.523 s to 4.927 s.
Both workloads used a stock–candidate–candidate–stock sequence. These timings
measure the combined SDK. The normalization regression suite passed on both
installed SDKs, and the root and regenerated Mathlib umbrella traces and seven
compiler outputs matched byte for byte.

Dominant language
Lean
Stars
10.5k
Forks
1.2k
Avg merge
19h 36m
Merged PRs (30d)
323

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Performance issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.