coalton-lang / coalton

Coalton is an efficient, statically typed functional programming language that supercharges Common Lisp.
https://coalton-lang.github.io/
MIT License
1.12k stars 67 forks source link

constant fold `fromInt` on literals #1176

Closed stylewarning closed 1 month ago

stylewarning commented 1 month ago

When we type an integer literal like 5, it'll often codegen into a (fromInt 5) call to allow integers to represent a broad collection of numerical types without programmer effort.

However, this translation happens at run-time, causing even simple loops to be sometimes 2-10x slower with copious allocation.

A "proper" solution to this problem would be to have more mature compiler passes so that we get this optimization "for free". But this is a common case and is very low-hanging fruit.

Note that it's likely the folded value will need to be *PRINT-READABLY* since it will be written out into compiled Lisp code. Therefore we may restrict this optimization to standard numerical types: ints and floats.