package ppx_update

  1. Overview
  2. Docs
PPX library to optimize record updates

Install

Dune Dependency

Authors

Maintainers

Sources

0.81.tar.gz
md5=494f7e6c72d76c96b5be41b655a44dae
sha512=55c7cbbeebf089daadd69115278c114339dd6c5f80c02dde71066da70a7b68b5962ab7978891ed70ea4c261d755528dbdbf9b5c5106c3b15416eb6d8b1e0d8e1

README.md.html

ppx_update

ppx_update is a small utility ppx for optimizing record updates with certain patterns.

A pattern one comes across when writing certain kinds of OCaml programs is:

type a = {...}
type b = {...}
type c = {a:a; b:b; ...}

let update c =
  let a' = ... in
  let b' = ... in
  {c with a'; b'; ... }

Now, if a' and b' are the same as c's previous values a and b, what we want to do for efficiency is this:

let update c =
  let a' = ... in
  let b' = ... in
  if c.a == a' && c.b == b' then
     c
  else
    {c with a'; b'; ... }

Physical equality is a very quick check that helps us avoid allocation when it's not needed. ppx_update lets you do this more easily:

let update c =
  let a' = ... in
  let b' = ... in
  [%up {c with a'; b'; ... }] (* will automatically take care of comparisons *)

Similarly, if b is mutable:

type a = {...}
type c = {mutable a:a; ...}

let update c =
  let b' = ... in
  c.b <- b'

In this case, we want to try and avoid the write barrier in case b' == b, which we can check for with

let update c =
  let b' = ... in
  if c.b == b' then
    ()
  else 
    c.b <- b'

With ppx_update:

let update c =
  let b' = ... in
  [%upf c.b <- b']  (* will only update if there's no physical address match *)

Using with dune

Simply add to your preprocessing sections in dune

; In your library or executable section
  (libraries ppx_update)
  (preprocess (ppx ppx_update))

Commands

Two extensions are currently supported:

  • %up (for update): handles record update. Will only work if you have a record that is functionally updated using the with keyword.

  • %upf (for update_field): handles mutable field updates.

Note that ppx_update is simple, so if you update with a long expression, that expression will be computed twice. For example, this is good:

c.foo <- foo';

but this is not a good idea:

c.foo <- long_computation a b c;

since the long computation will be performed twice.

OCaml

Innovation. Community. Security.