lean4-htt/tests/elab/compile_recursive_array_access.lean
Henrik Böving 86175bea00
perf: teach borrow inference about arrays (#13064)
This PR informs the borrow inference that if an `Array` is borrowed and
we index into it, the value we obtain is effectively a borrowed value as
well. This helps improve the ABI of operations that recurse on linked
structures containing arrays such as tries or persistent hash maps.
2026-03-23 18:10:50 +00:00

51 lines
1.5 KiB
Text

/-!
This test asserts that the compiler is able to handle compilation of functions that recurse through
nested arrays in a way that does not unnecessarily remove borrow annotations.
-/
inductive NAryTree where
| tip (x : String)
| node (ys : Array NAryTree)
deriving Inhabited
/--
trace: [Compiler.explicitRc] size: 19
def followPath @&tree @&path : obj :=
cases tree : obj
| NAryTree.tip =>
cases path : obj
| List.nil =>
let x.1 := oproj[0] tree;
inc x.1;
return x.1
| _ =>
let _x.2 := instInhabitedNAryTree.default._closed_0;
return _x.2
| NAryTree.node =>
cases path : obj
| List.cons =>
let ys.3 := oproj[0] tree;
let head.4 := oproj[0] path;
let tail.5 := oproj[1] path;
let _x.6 := instInhabitedNAryTree.default;
let _x.7 := Array.get!InternalBorrowed ◾ _x.6 ys.3 head.4;
let _x.8 := followPath _x.7 tail.5;
return _x.8
| _ =>
let _x.9 := instInhabitedNAryTree.default._closed_0;
return _x.9
[Compiler.explicitRc] size: 3
def followPath._boxed tree path : obj :=
let res := followPath tree path;
dec path;
dec tree;
return res
-/
#guard_msgs in
set_option trace.Compiler.explicitRc true in
def followPath (tree : NAryTree) (path : List Nat) : String :=
match tree, path with
| .tip x, [] => x
| .node ys, idx :: path => followPath ys[idx]! path
| _, _ => default