-
Notifications
You must be signed in to change notification settings - Fork 259
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
Pass serialized Go structs to TinyGo #523
Comments
I'll re-title this as serialized struct as it is less mysterious than the other way to pass structs around (via reference). Intentionally not getting into the latter approach as there's still quite a lot of learning to do. Suffice to say if you are interested in reference (pointer) based approach @rag has some interesting work started here anuraaga/opentelemetry-collector-contrib#633 I'll reply back on the serialization approach in a bit! |
ps you didn't ask, but this works for parsing. I'm using a fork of @birros to play with this because this easygo has some dependencies needed. I'll update what if anything I figure out birros/wazero-demo@main...codefromthecrypt:easyjson-debugging |
I think the problem was that we didn't set Cap on the underlying slice. When I did that, it worked. See also #524 |
I think the best way is using Google Flatbuffers or Cap'n'Proto. Flatbuffers is supported on TinyGo and Go, and have zero-copy decoding, so you don't pay the price for decoding or accessing single/random values. Also, Flatbuffers have 'Object-API' which makes easier to encode structs "into linear memory", so it's "similar" to I don't think any "schema-less" encoding will be good. Schema is unavoidable for zero-copy serializers and fast encoders. I think it's the best and fastest way to share data, with minimal overhead. If performance is not a concern you can also try Protobuf, it works with TinyGo (with some hack): tinygo-org/tinygo#447 (comment). If your I'm also creating one serializer for that purpose (currently, it supports Zig, Go and AssemblyScript) and it's faster and safer than Flatbuffers (it's ~80% faster than Flatbuffers on Wazero, compiled with TinyGo |
@inkeliz thanks for sharing and very exciting! sounds like something I can play with while at GlueCon! |
@knqyf263 ps regardless, let me know if declaring the slice specifying Cap worked for you. You'll need a stable byte slice declaration regardless of tool I think. except if you end up using host pointers serialized as uint64 (less ideal) or externref (more safe as as you can't accidentally allow pointer arithmetic because it has no ops like that). |
@codefromthecrypt Thanks for updating. Yes, @inkeliz Thanks for suggesting the Flatbuffers approach. I actually referred to the issue in TinyGo before raising this issue and wondered if protobuf is the best. I'll try Flatbuffers. Also, I'm really looking forward to your serializer. |
@codefromthecrypt I also tried gjson as I saw the following comment, but it doesn't support struct marshaling. Please correct me if I'm wrong. |
yeah afaik gson can only be used for accessing each field individually, not for serde entire structs tidwall/gjson#193 |
Protocol Buffers doesn't seem to work with TinyGo as of today. I faced the same error. |
Anyway, thank you all. I actually need a schema-less approach this time. I will go with easyjson for now. Let me close this issue. |
@knqyf263 ps @inkeliz made something you may enjoy looking at. https://github.com/inkeliz/karmem |
Cool! I'm wondering if it supports |
Currently, One reason why my serializer is faster than Flatbuffers because it doesn't generate a lot of garbage and re-uses the same existent struct, slices, etc. In my case, that is very important, and that seems to work well for TinyGo. The issue with |
First of all, thanks for the allocation examples.
https://github.com/tetratelabs/wazero/tree/main/examples/allocation
I assumed serialized Go struct can be passed to WASM as a byte array like the above examples and it can be decoded in WASM so that we can pass Go structs to WASM and vice versa. I started with JSON serialization as some JSON libraries seem to work with TinyGo. But fastjson doesn't support struct unmarshaling AFAIK and easyjson didn't work in my environment as below.
TinyGo code is like the following.
Do you have any recommendations for passing Go structs? It doesn't have to be JSON.
The text was updated successfully, but these errors were encountered: