# OCaml runtime: hash cons‘ing for strings?

**URL:** <https://discuss.ocaml.org/t/ocaml-runtime-hash-cons-ing-for-strings/5870>\
**Category:** Learning\
**Created:** [May 29, 2020, 7:06pm UTC](https://discuss.ocaml.org/t/ocaml-runtime-hash-cons-ing-for-strings/5870 "2020-05-29T19:06:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)\
**Post date:** [May 29, 2020, 7:06pm UTC](https://discuss.ocaml.org/t/ocaml-runtime-hash-cons-ing-for-strings/5870/1 "2020-05-29T19:06:42Z")

</div>

Since OCaml‘s strings are now immutable, could the runtime system offer optionally to maintain a pool of strings that maximizes sharing? Every new string would be checked to already exist in the pool and an existing one would be reused. The pool would be a weak set to facilitate garbage collection. Obviously this trades performance for memory because it requires a lookup when a string enters the runtime system. Hence, it should be optional. Has this been considered?

---

<div class="post-metadata">

**Author:** ![c-cube](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/c-cube/32/1727_2.png) [@c-cube](https://discuss.ocaml.org/u/c-cube)\
**Post date:** [May 29, 2020, 7:22pm UTC](https://discuss.ocaml.org/t/ocaml-runtime-hash-cons-ing-for-strings/5870/2 "2020-05-29T19:22:34Z")

</div>

You can quite easily do it manually with `Weak.Make`, I think. I’m not sure what the benefit to it for every string in a program would be, though, sounds like it would slow down things a lot.

---

<div class="post-metadata">

**Author:** ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)\
**Post date:** [May 29, 2020, 8:18pm UTC](https://discuss.ocaml.org/t/ocaml-runtime-hash-cons-ing-for-strings/5870/3 "2020-05-29T20:18:59Z")

</div>

I’m experimenting with this in a large application in a central place and that triggered the idea because it’s more difficult to do it everywhere.

---

<div class="post-metadata">

**Author:** ![zozozo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zozozo/32/59_2.png) [@zozozo](https://discuss.ocaml.org/u/zozozo)\
**Post date:** [May 29, 2020, 8:28pm UTC](https://discuss.ocaml.org/t/ocaml-runtime-hash-cons-ing-for-strings/5870/4 "2020-05-29T20:28:53Z")

</div>

> [@c-cube](#):
>
> You can quite easily do it manually with `Weak.Make` , I think. I’m not sure what the benefit to it for every string in a program would be, though, sounds like it would slow down things a lot.

The slowdown part depends on your usage actually: if you create most strings at the beginning and then mostly compare them, then hashconsing strings can actually be a huge benefit because comparison, or at least equality can be done insanely fast by using physical equality (so a single instruction comparing the address of the hashconsed strings), and actually, hashconsing strings is a technique regularly used in theorem provers which fit that scenario.

But in the general case, it is indeed not clear what the performance of hashconsing all strings would have, and since it’s relatively easy to do as a library (e.g. [ocaml-hashcons/hashcons.mli at master · backtracking/ocaml-hashcons · GitHub](https://github.com/backtracking/ocaml-hashcons/blob/master/hashcons.mli) , but I’m sure there are plenty others), I’m not sure it would be useful to add complexity to the runtime for it.
