# \[ANN\] nullable-array 0.1 (initial release)

**URL:** <https://discuss.ocaml.org/t/ann-nullable-array-0-1-initial-release/260>\
**Category:** Community\
**Tags:** announce\
**Created:** [May 23, 2017, 3:57pm UTC](https://discuss.ocaml.org/t/ann-nullable-array-0-1-initial-release/260 "2017-05-23T15:57:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chambart](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chambart/32/769_2.png) [@chambart](https://discuss.ocaml.org/u/chambart)\
**Post date:** [May 23, 2017, 3:57pm UTC](https://discuss.ocaml.org/t/ann-nullable-array-0-1-initial-release/260/1 "2017-05-23T15:57:51Z")

</div>

This is a small library providing a type intended to be used instead of `‘a option array’.

It is written in a style you shouldn’t copy (whoever you are, well maybe except Xavier or Mark). There are far too many Obj for it to be considered sane. I wrote that to be as efficient as it is currently possible in OCaml. I intend to update it to future potential changes.  
You could also consider that this is a good example of why you really shouldn’t use Obj.

I will probably publish quite soon some reasonably efficient vector (in the meaning of C++) and queue based on it.

I also took the opportunity to test discuss.

Have fun trying to break it!

> **[chambart/ocaml-nullable-array](https://github.com/chambart/ocaml-nullable-array)**
>
> Nullable-array is a small self-contained library providing an efficient implementation for a type equivalent to \`'a option array\` - chambart/ocaml-nullable-array

---

<div class="post-metadata">

**Author:** ![chambart](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chambart/32/769_2.png) [@chambart](https://discuss.ocaml.org/u/chambart)\
**Post date:** [May 23, 2017, 3:58pm UTC](https://discuss.ocaml.org/t/ann-nullable-array-0-1-initial-release/260/2 "2017-05-23T15:58:50Z")

</div>

I just submitted an opam package: [https://github.com/ocaml/opam-repository/pull/9280](https://github.com/ocaml/opam-repository/pull/9280)

---

<div class="post-metadata">

**Author:** ![Drup](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/drup/32/35_2.png) [@Drup](https://discuss.ocaml.org/u/Drup)\
**Post date:** [May 23, 2017, 8:53pm UTC](https://discuss.ocaml.org/t/ann-nullable-array-0-1-initial-release/260/3 "2017-05-23T20:53:02Z")

</div>

Nice package! I think you forgot the link in the first post.

---

<div class="post-metadata">

**Author:** ![chambart](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chambart/32/769_2.png) [@chambart](https://discuss.ocaml.org/u/chambart)\
**Post date:** [May 23, 2017, 9:13pm UTC](https://discuss.ocaml.org/t/ann-nullable-array-0-1-initial-release/260/4 "2017-05-23T21:13:03Z")

</div>

Here it is, with some links.

Also, I used that library as an excuse to test jbuilder. Thanks @jeremiedimino, that makes releasing a breeze. And it also works out of the box on windows (according to Appveyor when he doesn’t randomly fail to download some file…)

---

<div class="post-metadata">

**Author:** ![Yaron\_Minsky](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yaron_minsky/32/11_2.png) [@Yaron\_Minsky](https://discuss.ocaml.org/u/Yaron_Minsky)\
**Post date:** [May 23, 2017, 10:21pm UTC](https://discuss.ocaml.org/t/ann-nullable-array-0-1-initial-release/260/5 "2017-05-23T22:21:39Z")

</div>

We have a very similar module in Core\_kernel:

> <https://github.com/janestreet/core_kernel/blob/master/src/option_array.mli>

Which is in turn based on:

> <https://github.com/janestreet/core_kernel/blob/master/src/uniform_array.mli>

whose only point is to guarantee that you avoid the double-specialized array representation.

I think it would be great to get the core of this into the compiler proper. This is not the kind of thing that one should build on one’s own…

---

<div class="post-metadata">

**Author:** ![chambart](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chambart/32/769_2.png) [@chambart](https://discuss.ocaml.org/u/chambart)\
**Post date:** [May 23, 2017, 10:49pm UTC](https://discuss.ocaml.org/t/ann-nullable-array-0-1-initial-release/260/6 "2017-05-23T22:49:51Z")

</div>

nullable-array is a bit more complex and expensive to handles correctly combinations of marshaling/unmarshaling and polymorphic comparison (with and without the no-naked-pointers runtime).

I certainly agree that we could push something like that upstream, but it is always simpler to experiment and prove usefulness outside of the compiler before adding something to the stdlib. Pushing upstream would also allow for a slightly faster implementation.

In particular, to avoid problems with the no-naked-pointer runtime, I couldn’t represent None as the 0 pointer which loose 1 field per array. This could be lifted by not following null pointers in the no-naked-pointer variant, but at the cost of adding a test in the GC fast path (but probably well predicted by the CPU).

---

<div class="post-metadata">

**Author:** ![Yaron\_Minsky](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yaron_minsky/32/11_2.png) [@Yaron\_Minsky](https://discuss.ocaml.org/u/Yaron_Minsky)\
**Post date:** [May 24, 2017, 12:20am UTC](https://discuss.ocaml.org/t/ann-nullable-array-0-1-initial-release/260/7 "2017-05-24T00:20:59Z")

</div>

I’m not that excited about marshal and polymorphic compare, both of which I think of as warts on the language, but I can see why you’d want them for completeness. Certainly, having efficient data structures like Queues built on top of this makes sense, and we use it in various places in Core\_kernel for just this reason.

Indeed, we’d like to get some of these faster data structures into Base, but have resisted doing so because of the concern about having too much Obj.magic in Base.

---

<div class="post-metadata">

**Author:** ![lpw25](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lpw25/32/144_2.png) [@lpw25](https://discuss.ocaml.org/u/lpw25)\
**Post date:** [May 24, 2017, 6:00am UTC](https://discuss.ocaml.org/t/ann-nullable-array-0-1-initial-release/260/8 "2017-05-24T06:00:13Z")

</div>

> This could be lifted by not following null pointers in the no-naked-pointer variant, but at the cost of adding a test in the GC fast path (but probably well predicted by the CPU).

I was talking about this with Mark the other day. I think we should add this, as well as appropriate behaviours in marshalling and polymorphic compare, and then add an `Obj.null` value. This would make things like this much easier to implement.

As for `Uniform_array`, I really think that we should merge the [array data types pull request](https://github.com/ocaml/ocaml/pull/616), which would make that trivial to implement, and allow us to get rid of the float array hack.
