Krystian
09/13/2023, 2:14 PMJeff Lockhart
09/13/2023, 3:18 PMcreateCleaner
to run a cleanup action when your wrapper class is deallocated by GC. See https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.native.ref/create-cleaner.htmlKrystian
09/13/2023, 4:26 PMKrystian
09/13/2023, 7:10 PMinner
as well? Or does it have to be a standard Class onlyJeff Lockhart
09/13/2023, 7:29 PMit
reference to the resource to release memory or perform other cleanup actions.Krystian
09/13/2023, 7:37 PMJeff Lockhart
09/13/2023, 7:45 PMKrystian
09/13/2023, 7:55 PMJeff Lockhart
09/13/2023, 9:03 PMisCleanersLeakCheckerActive
does some checks to ensure cleaners aren't leaking resources by not being run. I haven't used it.Krystian
09/13/2023, 9:13 PMJeff Lockhart
09/13/2023, 9:39 PMkotlin.native.runtime.GC.collect()
.Krystian
09/14/2023, 8:50 AMKrystian
09/14/2023, 2:14 PMJeff Lockhart
09/14/2023, 2:46 PMKrystian
09/14/2023, 3:30 PMJeff Lockhart
09/14/2023, 3:42 PMKrystian
09/14/2023, 4:23 PMJeff Lockhart
09/14/2023, 6:19 PMKrystian
09/14/2023, 6:36 PMKrystian
09/14/2023, 7:34 PM@OptIn(ExperimentalForeignApi::class)
inline fun Vec2(x: Float = 0F, y: Float = 0F, allocator: AutofreeScope = MemScope()): Vector2 {
return allocator.alloc<Vector2> {
this.x = x
this.y = y
}
}
After doing some tests that implementation just gave me more performance than creating a class wrapper with an allocated value pointer and a cleaner that frees it (without a function)
and I'm 100% positive my attempt is probably the worst thing possible, but somehow just works better than the other things I tried?Jeff Lockhart
09/14/2023, 8:27 PMallocator
is passed to the function that is cleared by the caller (i.e. using Arena
and calling clear()
), the default MemScope()
only allocates memory, but never frees it.
memScoped { ... }
uses a MemScope
and calls clear at the end of its lambda scope, which you could also wrap the call to Vec2()
by passing the memScoped { ... }
this
reference.Krystian
09/14/2023, 9:11 PM