Skip to content

Commit e9df183

Browse files
authored
perf: avoid ref count increments for borrowed array accesses (#9866)
1 parent 954957c commit e9df183

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/Lean/Compiler/IR/RC.lean

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ private partial def visitFnBody (b : FnBody) : M Unit := do
7070
match e with
7171
| .proj _ parent =>
7272
addDerivedValue parent x
73+
| .fap ``Array.getInternal args =>
74+
if let .var parent := args[1]! then
75+
addDerivedValue parent x
76+
| .fap ``Array.get!Internal args =>
77+
if let .var parent := args[2]! then
78+
addDerivedValue parent x
7379
| .reset _ x =>
7480
removeFromParent x
7581
| _ => pure ()
@@ -351,7 +357,13 @@ private def processVDecl (ctx : Context) (z : VarId) (t : IRType) (v : Expr) (b
351357
| .fap f ys =>
352358
let ps := (getDecl ctx f).params
353359
let b := addDecAfterFullApp ctx ys ps b bLiveVars
354-
let b := .vdecl z t v b
360+
let v :=
361+
if f == ``Array.getInternal && bLiveVars.borrows.contains z then
362+
.fap ``Array.getInternalBorrowed ys
363+
else if f == ``Array.get!Internal && bLiveVars.borrows.contains z then
364+
.fap ``Array.get!InternalBorrowed ys
365+
else v
366+
let b := .vdecl z t v b
355367
addIncBefore ctx ys ps b bLiveVars
356368
| .ap x ys =>
357369
let ysx := ys.push (.var x) -- TODO: avoid temporary array allocation

0 commit comments

Comments
 (0)