dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.57k forks source link

[vm/ffi] `@Constant` for compile-time null terminated strings #52342

Open ds84182 opened 1 year ago

ds84182 commented 1 year ago

Would be nice to have to avoid managing allocations for C strings, and in AOT it could be part of the .rodata section of the executable.

Would support UTF-8 (Uint8) and UTF-16 (Uint16). Could be extended for value and struct constants behind a pointer as well.

@Constant('Hello, World!\n')
external Pointer<Uint8> get greeting;

puts(greeting);

// Or maybe...?
const greeting = Constant<Pointer<Uint8>>('Hello, World!\n');

puts(greeting.ptr);

cc @dcharkes

dcharkes commented 1 year ago

That's an interesting idea.

When addressing https://github.com/dart-lang/sdk/issues/47718, we should get the right infrastructure to do this.

Though, how do we provide an API for JIT in this case? Do we in JIT allocate at runtime and leak?

And maybe we can try to align the syntax with https://github.com/dart-lang/sdk/issues/50551.

// Global var in native asset, similar to `@Native external` function.
@NativeSymbol(/* optional symbol and asset */); // No type argument, as the Pointer already has it.
external Pointer<Uint8> get greeting;

// Global var in Dart.
@Constant('Hello, World!\n')
external Pointer<Uint8> get greeting;

Maybe if we address https://github.com/dart-lang/sdk/issues/50565 and https://github.com/dart-lang/sdk/issues/50551, we can just put them in a src/constants.c instead. Is there a strong preference for having them in Dart?

ds84182 commented 1 year ago

My preference for having them in Dart is mostly reducing the need to alloc & copy strings to the heap. It's a pain to manage temporary allocations for string-heavy APIs. A good example is Vulkan, where you have to look up all your functions using an instance-provided lookup function.

For JIT, the strings could live as long as the VM. Might require interning with a string cache of some kind.