Skip UTF-8 character decoding in String.crlfToLf when input has no CR byte
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
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from leanprover/lean4
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
leanprover/lean4#16897 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
leanprover/lean4#16892 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 86/100
leanprover/lean4#16886 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 78/100
leanprover/lean4#16820 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 1-3 hours Newbie friendliness 90/100
leanprover/lean4#16816 ·
Maintainers usually reply within 1 day
All issues in leanprover/lean4
Similar issues
-
bot-triaged module: inductor module: performance oncall: pt2 triaged
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
area: ui bug priority: low
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
tetsuji16/microProject#1893 ·
Maintainers usually reply within 1 day
-
A JSON chat answer blocks the backend event loop for the whole RAG runPossibly taken @NopeCharon claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
The-OpenROAD-Project/ORAssistant#656 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
domattioli/agents-inc#257 ·
Maintainers usually reply within 1 day
-
Trial runs the agent's `populate_context_post_run` on the event loop, so trials that end together stall the orchestrator for secondsPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
harbor-framework/harbor#4652 ·
Maintainers usually reply within 1 day