-
Notifications
You must be signed in to change notification settings - Fork 39
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
Rework Zig SDK #264
Rework Zig SDK #264
Conversation
@voigt I've still got a bit of work to do on this PR, but just tagging you in case you have some early feedback/to get the discussion going. |
I've done all of the code changes I was planning to do, so I've marked this ready for review. There are still some outstanding documentation/housekeeping tasks, but I figured it'd be good to start those after code review. Let me know of any suggestions/questions/considerations. Thanks! |
This already looks great - thank you for the excuse to look a bit deeper into Zig 0.12 🤓 I'm starting to look into your PR! |
Tried to build examples, but have no luck with the recent zig master:
In the meantime, I will try the version you documented. |
@voigt thanks for testing it out! I can confirm I see this too after updating to the latest Zig. Looks like the dependency for one of the examples, |
Created a PR against |
Thanks a lot for your contribution @sea-grass, and @voigt for taking the time to review it! Although I am not knowledgeable on Zig, I'll have a general pass tomorrow and confirm that the examples are working :) |
This is amazing! Thank you @sea-grass for the contribution and @voigt for reviewing and testing the different changes. We will go over the code and provide feedback soon. For now, I just activated the CI for this MR. Thank you! |
This commit introduces `kits/zig/wws`, with the intention of replacing `kits/zig/worker`. The design of the new SDK improves upon the previous one by exposing utilities for parsing wws requests and serializing wws responses, to give the user more control over memory while still keeping the SDK easy to use. The name was changed to make it more ergonomic within the Zig ecosystem (the developer will pull in a `wws` dependency to interface with wws, rather than a `worker` dependency).
The dependency `zig-router` doesn't yet support the latest Zig. Once that library is updated, we can uncomment this example. `std.fs.Dir.openFileWasi` has been removed, so we can just use `std.fs.Dir.openFile`.
I see that the latest Zig version is 0.11 yet. If we document it, I think it's fine, but it would be even better if we release all changes when 0.12 is officially out, not sure about their timeline. However, I would say it's definitely not a blocker to merge this work. I have downloaded https://ziglang.org/builds/zig-linux-x86_64-0.12.0-dev.2833+8802ec583.tar.xz and used this one for my tests. So far it's looking terrific. I only found a couple of issues:
Regarding your task Thanks for your work both, I think we are very close to merge this work :) |
08d599b
to
488c9bb
Compare
I'm not sure when 0.12 will be released either, but as Zig is pre-1.0 it seems to me that it's fairly common for Zig developers to keep up with the latest Zig master, for the most part. Zig 0.12 introduces the built-in package manager, after all. I know of one Zig project, Mach, which doesn't keep up with the very latest but instead has a recommended supported Zig version (which currently points to Zig
Good catch! I missed adding the features for those examples. They should work correctly now.
I have some preliminary results but am hoping to share something more concrete soon. I think I was a bit hasty in saying that "the new method is way faster." The old method made no heap allocations and the current method does make heap allocations, and from further testing it's clear that no heap allocations is always faster. That influenced me to add the
Once I have some spare time I'll run some more measurements with the latest release build of WWS and post a simple table comparing the performance between each of these examples. |
I +1 on the belief that Zig developers are usually on the latest master. For 0.12, this is an even more significant benefit, as the package manager improves working with the language. I'd be okay with going with 0.12 as long as we document the version. I really like the improvements you made. They definitely make wws' Zig support more polished! 🎉 |
Thanks, @voigt! I couldn't have made this progress on the Zig integration without your initial implementation, so thanks a lot for all the ground work and getting it started! |
As promised, here are some simple benchmarks comparing the Zig KV examples. Each benchmark was run using a release build of the WWS main branch (at 4b05de2), on a MacBook Pro with an M2 chip. Zig version 0.12.0-dev.2811+3cafb9655 was used to compile the examples (by running $ hey -z 10s http://127.0.0.1:8080/path-to-example Here are the results, sorted by Requests/sec:
The The |
Side note: If you're interested in running the benchmarks yourself, check out the |
Just finished updating the docs! Let me know if you want me to update/clarify anything else. Also, for the last item on my TODO ("Figure out the suggested way to get Zig developers to add wws as a dependency") I figured adding the
The Zig package manager does support referencing dependencies directly from e.g. a GitHub repo, but AFAIK it requires the Zig project (i.e. the Pending any further change requests, I think this is just about done! I also tested the examples on the latest Zig version as of today ( |
This was an amazing job @sea-grass ! Thank you very much for all the progress and work here. I plan to give it a final review over these days and merge it! Thank you also for your patience here :) |
Hello @sea-grass @voigt , Sorry for the long time to reply and thank you for your contribution. After some long discussions, this project is currently in the process of being archived. However, the development of this project will continue as an independent community fork present at https://github.com/webassemblylabs/wasm-workers-server. We would be grateful if you could recreate your contribution under that fork and we will work on applying it. Thank you ❤️ |
Thanks for letting me know @Angelmmiguel. I'll re-open this PR against that repo. |
Summary
This PR reworks the Zig SDK to provide more control to the user w.r.t. dynamic allocation (fixes #218) and also provides facilities to write the worker config directly from the Zig
build.zig
, so Zig developers can run e.g.zig build; wws zig-out root
and be up and running with wws in a matter of seconds.TODO
kits/zig/worker
) and new SDK (in my initial testing, the new method is way faster)Details
A summary of changes, so far:
examples/zig-examples
to ease maintenance of Zig example workers and also demonstrate composition of multiple wws workers in a single serverworker
towws
wws
module exposes some build-time functions to build a Zig worker and associated configwws
module exposes functions to read the request object from stdin (or any reader) and write the request object to stdout (or any writer)I believe these changes align with the goals of a worker:
Overall, with the new API surface, developing wws workers with Zig looks like this:
wws
as a dependencyconst worker = try wws.addWorker(...)
to build your Zig program as a WASM moduleworker.addToWriteFiles(b, write_files)
to copy the worker and its associated config to a WWS server directorysrc/main.zig
), usewws.parseStream(allocator, parse_config)
to decode the requestwws.Response
wws.writeResponse(response, writer)
to write the responseMinimal Zig worker now looks like this (click to expand)
To build/run the server:
Outstanding questions:
build.zig
to the root of this repository, but that may be confusing.