-
Notifications
You must be signed in to change notification settings - Fork 1
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
Super efficient way of embedding files or binary data in OCaml executables #21
Comments
I somehow came across this issue, and there are some other utilities available in OCaml:
I use(d) both (actually all three approaches, so crunch as well) in different applications, and they seem to work nicely. With caravan you've to hope that the linker (or strip) isn't working against you. And from your description of ocaml-crunch, I'd appreciate a PR that removes the chunking. The reason why there is an API is that it is meant for entire directories being embedded (not only a single file). Plus the mirage-kv API is satisfied, so it can act as a key-value store. Now, I don't know about your mileage, why "super efficient" is crucial. |
CC @MisterDA who suggested the task originally. |
I experimented with a malfunction-based replacement of crunch. My motivation was that crunch could IIRC be a bit slow due to parsing of very large string literals. I got stuck with how to use it with e.g. dune. Unfortunately, I think the code is on the drive of my now-dead laptop (possibly recoverable). |
A first step could be to expose binary data as string or bytes (think embedded assets, CSS, js, images…). Without special support, the compiler has to lex/parse a potentially long string, handling escape sequences along the way. Loosely related, there was ocaml/ocaml#10654 using The interface could be exposed as an extension node, taking a filename as a parameter, just like |
There is ocaml-crunch that takes files, splits them into chunks, generates a source OCaml module with an API. It's not super efficient: files have to be parsed at compile time, and recomposed when accessed. Room for improvement, don't generate OCaml code including the data to be embedded.
Idea: implement that with magic from the compiler, Dune, or cppo. Make files available as
Bytes
, in a module created at compile time.C23 will have an #embed preprocessor directive (under the hood, it's the linker's job). No parsing of data, available as a static const array of bytes. Checkout how Rust or Golang are doing it.
The text was updated successfully, but these errors were encountered: