Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Jane Street Tech Blog 12 min read Deep dive hardMaster

Using OxCaml to implement type-safe reference counting between OCaml and Python

Wrapping Python objects as OCaml values in PyOCaml ties each object's release to the OCaml GC finalizer, so large data frames or GPU buffers can stay alive long after Python code drops them. OxCaml's modes fix this: `@ local` arguments let C glue borrow Python references without an incref, `@ unique` returns let it steal the reference back without one, and `borrow_` keeps a value unique across local calls. For manual control, `Py.newref`, a uniqueness-consuming `Py.decref` and `ppx_release` provide type-safe explicit reference counting that frees objects promptly even when exceptions are raised.

Related posts

  • Oxidizing OCaml: Rust-Style Ownership

    Jane Street Tech Blog hardMaster

    Jane Street's OCaml extensions add Rust-style ownership through mode axes. A uniqueness axis (unique, exclusive, shared) enables in-place functional updates, type-changing strong updates and safe resource APIs. Borrowing via `&` combines with locality so values can be lent without consuming them. Exclusively mutable fields give mutation without move semantics, and a linearity axis (once vs many) stops closures that capture unique values from being invoked twice, which would otherwise break type safety. Unlike Rust, these modes carry no lifetime variables, so they avoid higher-order polymorphism and don't interfere with type inference.

  • Oxidizing OCaml: Locality

    Jane Street Tech Blog hardMaster

    Jane Street is extending OCaml with modes, starting with a locality mode that marks values as local (cannot escape their region) or global, so the compiler can stack-allocate short-lived values while keeping garbage collection everywhere else. Locality is inferred automatically and checked by the compiler. An `exclave` keyword lets a function return a local value into its caller's region, and local parameters let APIs like `with_file` guarantee that resources are not used after release. Unlike Rust's lifetime variables, modes are separate from types and do not affect type inference. They are deep and form a sub-mode order (global ≤ local), which forbids heap-to-stack pointers.

  • What the interns have wrought, 2025 edition

    Jane Street Tech Blog hardMaster

    Jane Street's 2025 intern projects include an index-aware evaluator for JSQL, the company's internal SQL dialect, which over-approximates key ranges from WHERE clauses and specializes execution to Map, Hashtbl or multi-index containers, giving ~700x and ~30x speedups on 3.8M-row benchmarks. A redesigned ocamltorch API uses OxCaml's local mode and refcount scopes to free GPU tensors deterministically instead of forcing Gc.full_major calls. A third project is a shared-memory library that passes pointers between trading processes instead of serializing data, using atomic refcounts and OxCaml visibility modes (read_write, read, local) to enforce race-free, borrow-checker-like access.

  • Introducing Immortal Objects for Python

    Meta Engineering mediumAdept

    Instagram's pre-fork Django servers were losing shared memory to copy-on-write because CPython mutates reference counts and GC headers even on effectively read-only objects. PEP-683 Immortal Objects marks a special refcount value so the runtime never modifies an object's refcount or GC header, which keeps those pages shared and lets objects be shared across threads without the GIL. Upstreaming required preserving backward compatibility across platforms, compilers, word sizes and endianness, and careful register allocation held the refcount-path check to about a 2% regression. The change is a building block for PEP-684 (per-interpreter GIL) and PEP-703 (optional GIL).

  • What the interns have wrought, 2022 edition

    Jane Street Tech Blog mediumAdept

    Three Jane Street intern projects from 2022 show substantial tooling work in OCaml. magic-trace gained a sampling mode that can cover 20 seconds to 20 minutes on Intel or AMD hardware, plus multi-process tracing, CPU-frequency transition reporting and a perf dlfilter backend that speeds trace decoding by 10–50%. Datafetcher jobs can now be compiled into Incremental computations, using a Response_or_waiting type to represent async state and mandatory memoization of fetch nodes. Quickcheck gained a [@@contract] syntax for deriving property tests from interface signatures, along with early support for bisimulation testing of two implementations of the same API.

The quest for software engineering knowledge.

Look:
Layout: