r/ocaml•
wfdctrl

Object style code, how to avoid GC pressure?

I have a bunch of functions of the form: state -> params -> state, where state is a record. After profiling I realized that 50% of cycles in that region are spent in GC. I guess Ocaml can't tell it is safe to reuse the record and keeps allocating new ones. One option would be to make the record fields mutable, however I would prefer to keep the code pure.

Is there any other way to restructure the code to avoid the allocations?

Posted Aug 4, 2021, 12:54 PM UTC · edited

1714
lrz691•

Pooling maybe a solution, but it involves Obj.magic and friends. See Jane Street's core library for nice examples.

5
wfdctrlOP•

Tuple_pool, right? It seems like a viable solution, thank you for the suggestion :)

1
lambda-male•

Can you afford to just increase the memory overhead?

I remember I once tried optimizing a functional program with singly-threaded state, like you are describing. I found out that tweaking the runtime parameters to something ridiculous like

OCAMLRUNPARAM=o=100000,h=10000000

yielded better results than a (naive) change to mutating code (because of write barrier overheads). The memory use was about 10x higher though.

5
wfdctrlOP•

Yep, that really helps, 25% speed up (for the whole program) and the memory usage is actually not too bad. Thanks :)

2
sadiq_ml•

Little late to this but if your allocations are short-lived then you should look at adjusting the size of the minor heap (s=... in OCAMLRUNPARAM).

If this is set too small then your allocations may be prematurely promoted to the major heap and there will extra work in their promotion and sweeping them later on.

For short-lived allocations and a well tuned minor heap size, the allocations become incredibly cheap.

Which particular GC functions were hot in your profiling?

3
wfdctrlOP•

Adjusting the minor heap size didn't have an impact, I think that is because what gets collected is the previous version of the state, which was already moved to the major heap at that point.

The cycles in the GC are spent about 30/70 between the caml_empy_minor_heap and caml_major_collection_slice. Adjusting the o parameter makes the biggest difference.

1
sadiq_ml•

That is really odd. How big/small did you go with the minor heap size?

If you are generating a fair amount of short-lived garbage then large minor heaps will ensure you promote as little as possible to the major heap. This means you'll do less major heap marking/sweeping.

Changing o, the space overhead means you're happy to trade off more memory usage for less major heap mark/sweeping work. Changing the minor heap size should mean less stuff gets moved to the major heap in the first place. (Consider the extreme example where you set the minor heap size to be bigger than the amount of memory you'd ever allocate).

1
glacialthinker•

I don't have anything helpful to add, just commiserating on this. :)

I guess Ocaml can't tell it is safe to reuse the record and keeps allocating new ones.

This one feels to me like a promising aspect of pure functional with GC... which is rarely or never realized. There's the potential to gain the same performance benefits of imperative code, but only where it's truly safe to do -- very appealing!

I guess it probably requires whole-program optimization, at least for the style of functions you have. Unless a "copy+modify" and "mutable update" implementation of the function were emitted and chosen based on safe-reuse determined at the callsite? I'm not sure how bad the problem of determining safe-reuse explodes, but I'm guessing there must be complications...

Apparently Haskell's GHC can accomplish this optimization in some cases. Since it's pure and still limited in how effective it can do this, I assume it's a difficult problem.

3
das_kube•

That's the idea behind perceus, which refcounting with opportunistic reuse of memory that was about to be deleted. It seems to be promising for pure/algebraic effect-based languages.

3
lambda-male•

I don't think it has anything to do with purity or algebraic effects, though it requires that the values in your language are acyclic.

1
das_kube•

It also doesn't handle mutation, at least not in the paper. That jibes with the acyclic values.

1
lambda-male•

Apparently Haskell's GHC can accomplish this optimization in some cases

source?

Or do you mean fusion optimizations? I don't think they can help much in this case.

In-place updates in functional programming doesn't require whole-program optimization, it is an often advertised feature of uniqueness typing.

GHC has the dual linear typing, but you if you create an object and then immediately pass it to a linear continuation, it's guaranteed that there is always just one unique reference to it. With that you can more safely implement in-place updates in your code (as opposed to the compiler supporting uniqueness typing doing it for you).

2
glacialthinker•

I'm having trouble finding anything about it now. Perhaps I misread something or imagined it some time ago, sorry.

In-place updates in functional programming doesn't require whole-program optimization, it is an often advertised feature of uniqueness typing.

With a function having signature state -> params -> state, in a separate module (translation unit)... how would it work for input state which is no-longer needed (an in-place update is preferable) and cases where the input state is still required (copy modified)? I guess there could be some hints for link-time optimization? This is pretty far from anything I work on, so I might just be out-of-date (or touch). :)

Is uniqueness typing currently used in OCaml, or Haskell?

1
lambda-male•

Usually with uniqueness typing you would have something like *state -> params -> *state with the asterisk annotating uniqueness, that is when we have a reference to a value of type *state, we are guaranteed that we have the unique reference (no aliasing pointers) to it. That's why we can modify it in place - there is no one holding onto the old input state. It's similar to what Rust offers.

That's a low-levelish explanation, in type theory terms uniqueness typing is usually achieved by a substructural type system, in which we can't duplicate a variable in a context.

Uniqueness types are niche, Clean and Idris 1 are some "bigger" examples of languages that have them. Linear types are strongly related and can be used to achieve similar things, GHC 9 has them.

3