lean4-htt/tests/bench/ilean_roundtrip.lean
Paul Reichert 98e4b2882f
refactor: migrate to new ranges (#8841)
This PR migrates usages of `Std.Range` to the new polymorphic ranges.

This PR unfortunately increases the transitive imports for
frequently-used parts of `Init` because the ranges now rely on iterators
in order to provide their functionality for types other than `Nat`.
However, iteration over ranges in compiled code is as efficient as
before in the examples I checked. This is because of a special
`IteratorLoop` implementation provided in the PR for this purpose.

There were two issues that were uncovered during migration:

* In `IndPredBelow.lean`, migrating the last remaining range causes
`compilerTest1.lean` to break. I have minimized the issue and came to
the conclusion it's a compiler bug. Therefore, I have not replaced said
old range usage yet (see #9186).
* In `BRecOn.lean`, we are publicly importing the ranges. Making this
import private should theoretically work, but there seems to be a
problem with the module system, causing the build to panic later in
`Init.Data.Grind.Poly` (see #9185).
* In `FuzzyMatching.lean`, inlining fails with the new ranges, which
would have led to significant slowdown. Therefore, I have not migrated
this file either.
2025-07-07 12:41:53 +00:00

54 lines
1.7 KiB
Text

import Lean.Data.Lsp
def genModuleRefs (n : Nat) : IO Lean.Lsp.ModuleRefs := do
let someLoc : Lean.Lsp.RefInfo.Location := {
range := ⟨⟨333, 444⟩, ⟨444, 555⟩⟩
parentDecl? := some {
name := "A.Reasonably.Long.ParentDecl.Name.barfoo",
range := ⟨⟨1111, 2222⟩, ⟨3333, 4444⟩⟩
selectionRange := ⟨⟨5555, 6666⟩, ⟨7777, 8888⟩⟩
}
}
let mut someUsages : Array Lean.Lsp.RefInfo.Location := #[]
for _ in [0, 200] do
someUsages := someUsages.push someLoc
let someInfo : Lean.Lsp.RefInfo := {
definition? := someLoc
usages := someUsages
}
let mut refs : Lean.Lsp.ModuleRefs := .empty
for i in *...n do
let someIdent := Lean.Lsp.RefIdent.const s!"A.Reasonably.Long.Module.Name{i}" s!"A.Reasonably.Long.Declaration.Name.foobar{i}"
refs := refs.insert someIdent someInfo
return refs
@[noinline]
def compress (refs : Lean.Lsp.ModuleRefs) : IO String := do
return Lean.Json.compress (Lean.ToJson.toJson refs)
@[noinline]
def parse (s : String) : IO (Except String Lean.Json) := do
return Lean.Json.parse s
def main (args : List String) : IO Unit := do
let n := (args[0]!).toNat!
let refs ← genModuleRefs n
let compressStartTime ← IO.monoMsNow
let s ← compress refs
let compressEndTime ← IO.monoMsNow
let compressTime : Float := (compressEndTime - compressStartTime).toFloat / 1000.0
IO.println s!"compress: {compressTime}"
let parseStartTime ← IO.monoMsNow
let r ← parse s
let parseEndTime ← IO.monoMsNow
let parseTime : Float := (parseEndTime - parseStartTime).toFloat / 1000.0
match r with
| .ok _ =>
IO.println s!"parse: {parseTime}"
| .error _ =>
IO.println s!"parse: {parseTime}"
IO.println "error"