|
|
|
|
|
by bjourne
3643 days ago
|
|
> Another point with the above loop, is that ideally, even if it can't be optimized/memoized down to a constant - it really shouldn't have to be much slower than its C counterpart. Except for handling bignums in some way or other (perhaps on overflow only). Sadly, bignum handling is very expensive even with type hinting. Essentially, every time two numbers is added or subtracted you have to check for overflow: int z = hw_add(x, y);
if (over/under-flowed?()) {
bignum* bn = new bignum();
bignum_add(to_bignum(x), to_bignum(y), &bn);
return bn;
}
return z;
So most modern statically typed languages (none named, none forgotten!) actually cheat and use modular arithmetic instead. For example, the straightforward way of computing 9223372036854775807 + 1 on a 64bit system in one of those languages is likely to yield an incorrect result.Which imho is complete bullshit because there is no reason to sacrifice correctness for performance other than to win benchmarking contests. |
|
One good trick started by lua is to use double for everything and check the mantissa of the normalized result for overflows. A double can represent the whole int32 range. With double and nan-tagging you have a unique fast representation of all primitives and can use a double word stack. Which is the biggest win. Look at how python, perl and ruby are doing this. Horrible. And then look how a fast dynamic language is doing it.