You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
import (
"sort"
)
varslice=make([]int, 1000)
funcmain() {
closedVar:=10closedSlice:=slice[10:100]
// The closure to sort.Find doesn't escape, so// neither should the variables closed over.sort.Find(len(closedSlice), func(iint) int {
returnclosedSlice[i] -closedVar
})
}
results in:
tinygo build -target pico -print-allocs=. closures.go
/Users/a/proj/tinygo/closures.go:11:2: object allocated on the heap: escapes at line 12
/Users/a/proj/tinygo/closures.go:10:2: object allocated on the heap: escapes at line 12
...
Since the function argument to sort.Find doesn't escape, the variables referenced in the closure shouldn't escape either.
This issue makes it hard to use closures in a GC friendly way, essentially forcing the manual inlining of sort.Find and similar functions. In future, I expect similar issues for Go iterator functions (as anticipated in Go 1.23).
The text was updated successfully, but these errors were encountered:
This example,
results in:
Since the function argument to
sort.Find
doesn't escape, the variables referenced in the closure shouldn't escape either.This issue makes it hard to use closures in a GC friendly way, essentially forcing the manual inlining of
sort.Find
and similar functions. In future, I expect similar issues for Go iterator functions (as anticipated in Go 1.23).The text was updated successfully, but these errors were encountered: