Hashtbl.Make vs. Hashtbl.create -- when to use what?

I’ve always used Hashtbl.Make simply because I didn’t know about Hashtbl.create.

I looked at the implementation of the hashing algorithm hash tables created with Hashtbl.create use and I found it a bit freaky.

Is it correct to conclude that Hashtbl.Make should be used whenever possible, and Hashtbl.create should mainly be reserved for cases where there is some obvious benefit to being more generic? (e.g. implementing a generic memoize function)

1 Like

Are you referring to the OCaml standard library Hashtbl module? There probably isn’t any official recommendation on this, but my personal feeling would be that Hashtbl.create should only be used for the same sorts of key types where you would be comfortable using polymorphic compare, which, similarly to the generic hash function, dispatches on heap block headers to figure out how to compare two values.

1 Like

Yes, standard library.[see side rant] As a relative newcomer to OCaml with no understanding of how the OCaml heap works (except that there is a minor and a major) which kinds of values would you recommend being comfortable with using with polymorphic compare and, more specificaly to this question, the generic hashing function?

side rant: I realize OCaml’s standard library is somewhat lacking and Base/Core provide many, many benefits. Batteries is also an improvement, I guess—but! (and this is a big butt) do I really have to specify if I’m talking about the standard library? I will specify in 100% of cases when I talk about Core or any other library that shadows modules in the standard library. Do other people not do this?

(also, I am kinda drunk at this point. please take my snarkiness with that in mind. I appreciate your help.)

And on that note, is Stdlib.Hashtbl.create similar to (Jane Street) Base.Hashtbl.Poly.create? (give or take a function. I didn’t bother to look up the exact function name.)

Polymorphic compare is appropriate to use with values where the notion of “equality” you want is the same as “structural” equality. Things like ordinary variants, records, lists, arrays, primitives, etc. A data structure where this might not work correctly is, for example, binary search trees representing sets, where two different tree structures may still represent the same set of elements.

Jane Street’s libraries discourage the use of polymorphic compare altogether (see this thread for example).

I agree that if unspecified, it is reasonable to assume the standard library. My question was intended more as just a confirmation that we were talking about the same thing. Sorry if that was confusing.

I believe so. That is the right function name. I think they use the same hash function, as well, although the tables themselves are represented differently.

Prost!

2 Likes

Thanks. I think that about covers it!

1 Like