Skip to content

Commit

Permalink
Memoize small Nat strings
Browse files Browse the repository at this point in the history
  • Loading branch information
nomeata committed Apr 12, 2024
1 parent 71c77ee commit 57cf9ae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/Init/Data/Repr.lean
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,17 @@ def toDigits (base : Nat) (n : Nat) : List Char :=
toDigitsCore base (n+1) n []

@[extern "lean_string_of_nat"]
protected def repr (n : @& Nat) : String :=
private opaque reprFastAux (n : @& Nat) : String

private def reprArray : Array String := Id.run do
List.range 128 |>.map Nat.reprFastAux |> Array.mk

private def reprFast (n : Nat) : String :=
if h : n < 128 then Nat.reprArray.get ⟨n, h⟩ else
Nat.reprFastAux n

@[implemented_by reprFast]
protected def repr (n : Nat) : String :=
(toDigits 10 n).asString

def superDigitChar (n : Nat) : Char :=
Expand Down
2 changes: 1 addition & 1 deletion stage0/src/stdlib_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ options get_default_options() {
// see https://lean-lang.org/lean4/doc/dev/bootstrap.html#further-bootstrapping-complications
#if LEAN_IS_STAGE0 == 1
// switch to `true` for ABI-breaking changes affecting meta code
opts = opts.update({"interpreter", "prefer_native"}, false);
opts = opts.update({"interpreter", "prefer_native"}, true);
// switch to `true` for changing built-in parsers used in quotations
opts = opts.update({"internal", "parseQuotWithCurrentStage"}, false);
// toggling `parseQuotWithCurrentStage` may also require toggling the following option if macros/syntax
Expand Down

0 comments on commit 57cf9ae

Please sign in to comment.