I would like to propose exposing erts_cmp as erlang:compare/1.
In Erlang/Elixir, there are several data structures that rely on order, such as gb_trees, ordsets, and Elixir’s upcoming data structure for the type system.
Today, those data structures rely on comparisons:
insert_1(Key, Value, {Key1, V, Smaller, Bigger}, S) when Key < Key1 ->
...
insert_1(Key, Value, {Key1, V, Smaller, Bigger}, S) when Key > Key1 ->
...
insert_1(Key, Value, nil, _S) ->
...
The issue is that if you have a nested data structure where the difference between the keys are on the leaves, you are doing a lot of repeated work across those three branches (especially if they are equal).
My understanding, from looking at the generated .S files for a simple program, is that those are not optimised today, at least not for the compiler. How feasible does the compiler team think it would be to optimise those automatically? If not possible, could we expose erlang:compare/2 (potentially returning eq, lt, gt)?
Thank you,