Repeatedly changing observers in an incremental computation

In a previous post incremental was suggested to organize computations so that they’d be “as lazy and deferred as possible”.

As I understand it, incremental does not update immediately after a change in input variables, and waits for a call to stabilize before doing the calculation, and in doing so, it will refrain from updating nodes that are not useful to compute observed values.

I have a use case where I want to build a large number of computations, and repeatedly observe a tiny but changing subset of them after a variable modification (at each iteration I change a different variable). I’m basically considering to use the following function:

let eval x =
  let o = Inc.observe x in
  Inc.stabilize () ;
  let y = Inc.Observer.value_exn o in
  Inc.Observer.disallow_future_use o ;

and then repeatedly call Var.set and eval on different pairs of variable and computation. I don’t know incremental very well, so I’d like to know if this would be a severe misuse of the library and if there is a better way to achieve “as lazy and deferred as possible” computations when both modified and observed nodes of the computation graph change at each query.

1 Like

This seems like a reasonable idiom, though it is a bit unusual to be constantly creating and destroying observers, so I think it’s wiyth benchmarking that. Another possible approach is to have a bind node that dynamically changes what value (or values) from the computation it’s presenting.

Another thing to keep in mind is that the incrementality of a computation constructed in this style could be very good or very bad, depending on how much of the graph is reused from so to step. For any of this to make sense, there needs to be a lot of shared computation.


That’s a very interesting option, thanks! Yes, the computations are massively shared but above all, there are lots of them that shouldn’t be made until a change is validated (I’d like to use incremental to write a Markov Chain Monte Carlo sampler).

1 Like