dart-lang / dart-lang/native

Make `Arena` and `using` availble for other abstractions than FFI?

Open
#928 6 comments 1 reaction 0 assignees View on GitHub
package:ffi
Dominant language
Dart
Stars
275
Forks
144
Avg merge
2d 10h
Merged PRs (30d)
47

Description

Currently, `Arena` and `using` live here in `package:ffi`.

However, `Disposable`s [1] could benefit from using `using` as well. That way `dispose` would be correctly invoked on both exceptional control flow and when code is executed in other zones. https://flutter.dev/go/introduce-disposable

Manual `try`-`finally` will work for exceptional control flow, but not for executing `dispose` in the correct zone.

As `Arena` is currently heavily tied to FFI by making it implement `Allocator`, we would need to design some kind of way that would work both with `Disposable`s and `Allocator`s.

```dart
using((arena) {
final Pointer myPointer = arena(10);
final Disposable myDisposable = ...;
arena.using(myDisposable, myDisposable.dispose); // This should get a dedicated method likely.
});
// myPointer is freed.
// myDisposable is disposed.
```

If we would use `Arena` for `Disposable`s, it would be weird to import `package:ffi`.

cc @polina-c Would having something like an `Arena` and `using` benefit the code using `Disposable`s? Over writing try-finally blocks? Does Flutter use zones at all? Or do you think `Disposable` should do without a lexical scoping construct that releases at the end of the scope?

@lrhn Maybe you have bright ideas on how we might introduce some way to have `Arena`s for different types of things (`Pointer`s, `Disposable`s, ...) in a unified way? It seems a common pattern to have a try-finally (that should also run in the right zone).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.