-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Working with GC types without copy #305
Comments
Yes, agreed. It's definitely the plan of record to add a |
Before ref-types, gc-types, stringref and other features are stable, we have enough time to discuss how the gc language should obtain wasi data. In fact, after considering gc types, there is a better correspondence between the wasi type and the wasm type. No options indicate pointer mode, add
|
Another benefit is that if all gc types are used, there is no need to bring in a memory allocator, which helps reduce the size and warm up faster. rustc's Other smaller allocators sacrifice either speed or security. (component
;; Define a memory allocator
(core module $MockMemory ;; Replace here by an actual allocator module, such as libc
(func $realloc (export "realloc") (param i32 i32 i32 i32) (result i32)
(i32.const 0)
)
(memory $memory (export "memory") 255)
)
(core instance $mock_memory (instantiate $MockMemory))
;; import wasi function
(import "wasi:random/[email protected]" (instance $wasi:random/[email protected]
(export "get-random-bytes" (func (param "length" u64) (result (list u8))))
))
;; wasi function to wasm function
(core func $wasi:random/[email protected]/get-random-bytes (canon lower
(func $wasi:random/[email protected] "get-random-bytes")
(memory $mock_memory "memory")
(realloc (func $mock_memory "realloc"))
))
;; import wasm function
(core module $TestRandom
(type (func (param i64 i32)))
(import "wasi:random/[email protected]" "get-random-bytes" (func $wasi:random/[email protected]/get-random-bytes (type 0)))
)
;; instantiate wasm module with wasi instance
(core instance $test_random (instantiate $TestRandom
(with "wasi:random/[email protected]" (instance (export "get-random-bytes" (func $wasi:random/[email protected]/get-random-bytes))))
))
) If using the gc type, this can be simplified to: (component
;; import wasi function
(import "wasi:random/[email protected]" (instance $wasi:random/[email protected]
(export "get-random-bytes" (func (param "length" u64) (result (list u8))))
))
;; wasi function to wasm function
(core func $wasi:random/[email protected]/get-random-bytes (canon lower
(func $wasi:random/[email protected] "get-random-bytes")
reference-type
))
;; import wasm function
(core module $TestRandom
(type (func (param i64) (result (array u8))))
(import "wasi:random/[email protected]" "get-random-bytes" (func $wasi:random/[email protected]/get-random-bytes (type 0)))
)
;; instantiate wasm module with wasi instance
(core instance $test_random (instantiate $TestRandom
(with "wasi:random/[email protected]" (instance (export "get-random-bytes" (func $wasi:random/[email protected]/get-random-bytes))))
))
) Obtaining a field of gc type requires only one instruction and does not require pointer algebra (at least three instructions), further reducing the binary size. |
Yes, really good point regarding mutability vs. immutability; we probably do want both as ABI options. A really nice benefit of immutability is that if both sides of a component-to-component call use immutable GC references, no copy needs to be made when passing a reference across the boundary. OTOH, if your language ultimately does need a mutable array of bytes, then the immutable GC option may impose an extra unnecessary copy; thus having both options make sense. String its its own story, but definitely a Unicode-encoded |
Considering the complexity of mutable and some incoming features such as Taking into account proposals such as thread and share-everything-threading, you can consider implementing this feature in stages. The initial version only provided immutable types that did not require copying. Mutability is a post-MVP content, before which users need to sacrifice certain performance to manually implement some glue code to copy to the required types. |
I'm having some trouble switching to wasi preview 2.
For example, the following interface:
The function signature is
func (u64) -> (list<u8>)
But its lower type is
core func (i64, i32) -> ()
, which is very difficult to use.If I want to convert it to
core type (array (mut u8))
, a very long glue code is required.I hope to add a GC mode canon option that can make the lower type similar to
core func (i64) -> (array u8)
.For complex nested types, getting the specified data requires very complex pointer algebra, whereas if using array it only requires multiple
array.get
.I think this helps simplify the use of some external interfaces, such as:
The text was updated successfully, but these errors were encountered: