# JIT Compilation in OCaml

**URL:** <https://discuss.ocaml.org/t/jit-compilation-in-ocaml/2111>\
**Category:** Ecosystem\
**Tags:** compiler\
**Created:** [June 8, 2018, 9:58am UTC](https://discuss.ocaml.org/t/jit-compilation-in-ocaml/2111 "2018-06-08T09:58:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![egoholic](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/egoholic/32/574_2.png) [@egoholic](https://discuss.ocaml.org/u/egoholic)\
**Post date:** [June 8, 2018, 9:58am UTC](https://discuss.ocaml.org/t/jit-compilation-in-ocaml/2111/1 "2018-06-08T09:58:20Z")

</div>

Is it possible in OCaml to compile some pieces of code during runtime? For example, I don’t wont to read configs or localizations from files or DB and I don’t want to read it from memory, I just want to translate some pieces where I use the configuration data to kind of literals so that L18n.l “main.headers.orders” would not find a corresponding language localization, but JITcompiles this piece of code just like a concrete value - “Orders” string for English.

---

<div class="post-metadata">

**Author:** ![steinuil](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/steinuil/32/993_2.png) [@steinuil](https://discuss.ocaml.org/u/steinuil)\
**Post date:** [June 9, 2018, 9:13am UTC](https://discuss.ocaml.org/t/jit-compilation-in-ocaml/2111/2 "2018-06-09T09:13:24Z")

</div>

You could have a PPX that, _at compile time_, transforms all strings that look like `[%i18n.l "main.headers.orders"]` into the corresponding string for the language you want, like `"Order"`, though you couldn’t change the language after you’ve compiled that code.

If you wanted to do that at runtime… you could probably generate an `mli` file and an `ml` file for each language from your i18n file, use a PPX to translate `[%i18n.l "main.headers.orders"]` into something like `I18n_gen.main_headers_orders` and then use `Dynlink` to load the module with the correct translation at runtime, though that sounds like an awful lot of work for a very small performance gain. But knock yourself out if you want to try.

> **[janestreet/ocaml\_plugin](https://github.com/janestreet/ocaml_plugin)**
>
> Automatically build and dynlink ocaml source files - janestreet/ocaml\_plugin
