Repository navigation
Conversation
The `Type.Id.uid` function converts a type identifier to an integer by going through `Obj.Extension_constructor.of_val`. In this context, `Obj.Extension_constructor.of_val` is just an expensive way to compute the identity function: it performs multiple checks, including multiple calls to `caml_obj_tag`, in order to determine if it is passed a nullary extension variant, a non-nullary extension variant, or something that is not an extension variant at all (in which case it raises an Invalid_argument exception). When called with a nullary extension variant (which is just represented as its constructor), `Obj.Extension_constructor.of_val` is always the identity. Since the usage in `Type.Id` is always with a nullary extension variant, use `Obj.magic` as a less expensive identity function. For context, this came up while optimising code making *many* lookups in small heterogenous maps, such that it ended up being noticeable in profiling.
|
Instead of |
|
Probably, I was not aware this existed — it indeed should be much better. |
| - #13740: Improve performance of Weak.find_aux | ||
| (Josh Berdine, review by Gabriel Scherer) | ||
|
|
||
| - #15049: Do not use Obj.Extension_constructor.of_val in Type.Id |
There was a problem hiding this comment.
Nit, but Changes entries should be written from the point of view of the reader, not the writer. Here, a random reader who comes across this entry will not know how or why this would be relevant to her. Instead, something like Avoid use of Obj.Extension_constructor.of_val in Type.Id for better performance. or Type.Id: replace Obj.Extension_constructor.of_val by [%extension_constructor] for better performance. (which has the advantage of publicizing [%extension_constructor]) would be better in this respect.
There was a problem hiding this comment.
I updated the changes entry to mention [%extension_constructor] and the fact that it improves both performance and type safety compared to Obj.Extension_constructor.of_val.
| - #13740: Improve performance of Weak.find_aux | ||
| (Josh Berdine, review by Gabriel Scherer) | ||
|
|
||
| - #15049: Improve performance and type safety of Type.Id by using |
There was a problem hiding this comment.
Thanks, I've pushed a fix to trunk directly
The
Type.Id.uidfunction converts a type identifier to an integer by going throughObj.Extension_constructor.of_val. In this context,Obj.Extension_constructor.of_valis just an expensive way to compute the identity function: it performs multiple checks, including multiple calls tocaml_obj_tag, in order to determine if it is passed a nullary extension variant, a non-nullary extension variant, or something that is not an extension variant at all (in which case it raises an Invalid_argument exception).When called with a nullary extension variant (which is just represented as its constructor),
Obj.Extension_constructor.of_valis always the identity. Since the usage inType.Idis always with a nullary extension variant, useObj.magicas a less expensive identity function.For context, this came up while optimising code making many lookups in small heterogenous maps, such that it ended up being noticeable in profiling.