Skip to content

Instantly share code, notes, and snippets.

@polytypic
Created October 23, 2024 09:25
Show Gist options
  • Save polytypic/6bbb40a8d517e9dfb7ccdcded8abca50 to your computer and use it in GitHub Desktop.
Save polytypic/6bbb40a8d517e9dfb7ccdcded8abca50 to your computer and use it in GitHub Desktop.
Support for FLS in the OCaml runtime

Support for FLS in the OCaml runtime


OCaml multicore architecture

  • DLS = Domain Local Storage
  • TLS = Thread Local Storage
  • FLS = Fiber Local Storage

FLS use cases


Status quo

Fiber.current in Picos obtains fiber record:

Fiber : {
  mutable forbid : bool;
  mutable packed : Computation.packed;
  mutable fls : non_float array;
}

The fiber record itself can act as identity.

fls array stores heterogenous values (via Obj) like DLS/TLS and is accessed through the fiber record.


Fiber.current performance

via effect:   27.8ns
via TLS.get:   6.9ns (experiment to optimize it)

Both of the above are too slow!

As an aside, it should be possible have TLS.get itself taking roughly 1ns.


Checked/Unchecked Mutex

Even with TLS.get hack, checked mutex is slower!

Mutex protected get (i.e. read of ref):

checked:   58.3 ns
unchecked: 17.3 ns (could be made a bit faster with FAD)

Benchmarks


Idea (originally from Leo)

FLS info could be stored within per fiber (or per handler) runtime data structures.

I don't (yet) have a proposal on how to do that, concerns: extensibility and modularity.

Independently developed things would ideally not clash. However, for my purposes in Picos, being able to store one word would be enough. I can then provide everything else through that.

As an aside: match_with closure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment