Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose {Int,Int32,Int64,Nativeint}.{min,max} #10389

Closed
alainfrisch opened this issue May 3, 2021 · 5 comments · Fixed by #10392
Closed

Expose {Int,Int32,Int64,Nativeint}.{min,max} #10389

alainfrisch opened this issue May 3, 2021 · 5 comments · Fixed by #10392
Labels

Comments

@alainfrisch
Copy link
Contributor

Stdlib.{min,max} are quite bad performance-wise (even when inlined, they will still call the polymorphic comparison C primitive). In #10251, I proposed to add compiler primitives to allow the optimizer generate better code when those functions are used on simple numerical types, but @xavierleroy's position (#10251 (comment)) is rather that these functions should eventually go away, and one should stop using them.

So, I believe we should:

  • Expose specialized versions of the functions in Int, Int32, Int64, Nativeint. For Char, Uchar and String, it's less obviously useful. For Bool, we already have || and &&, but it could still perhaps be useful (for the sake of uniformity, and also to avoid the short-circuit behavior?). For Float, the functions already exist (also with *_num variants); idem for Zarith`s Q and Z modules. Are people using those functions on non-numerical types?
  • Mark Stdlib.{min,max} as being deprecated, in order to help users spot the usually inefficient call sites, and prepare for a possible removal.
@nojb
Copy link
Contributor

nojb commented May 3, 2021

Sounds good! One small implementation issue that I can think of is that Stdlib.{min,max} is used in the implementation of several other stdlib modules (String, Bytes, Buffer, Hashtbl, Random, ...). To avoid introducing too many dependencies to Int, I guess that we will have to duplicate the definition of the (type-specialized) min/max functions inside the modules that use them?

@alainfrisch
Copy link
Contributor Author

To avoid introducing too many dependencies to Int

Do you see a strong reason to avoid such dependencies? Int depends only on Stdlib and is itself tiny. Btw, I don't understand why, in Int many definition are copied from Stdlib, but not everything (e.g. compare is imported from Stdlib, while many others external are redeclared in Int).

But yes, inlining min or max on int where needed shouldn't be too hard :-)

@xavierleroy
Copy link
Contributor

+1 for adding min and max to Int, Int32, Int64, Nativeint. I don't think these operators are very useful nor intuitive for non-numeric types such as string or bool, so let's not have them.

-1 for deprecating Stdlib.min and Stdlib.max now. Maybe once the specialized versions have been available for several years.

@alainfrisch
Copy link
Contributor Author

Ok! What about char? Do you put in the category of non-numeric types as well?

@xavierleroy
Copy link
Contributor

I don't think of char as a numeric type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants