dart2js: Integer division by two is extremely slow compared to shift. #14166
Labels
closed-obsolete
Closed as the reported issue is no longer relevant
type-bug
Incorrect behavior (everything from a crash to more subtle misbehavior)
web-dart2js
In Dart, integer division by 2 is the same as right-shift by 1.
It isn't so in dart2js because dart2js doesn't have integers, and only does 32-bit shift operations, but it should still be possible to do some integer
divisions faster.
If you implement two binary searches where the only difference is using a single division or shift to find the middle index, i.e., either:
int mid = min + ((max - min) >> 1);
or
int mid = min + ((max - min) ~/ 2);
then the performance of the former is 120% better than the latter.
That is, the single ~/2 takes more time (more than >>) than the entire rest of the algorithm.
I'd prefer to be able to use ~/2 instead of >>1 when conceptually I'm really halving a value, and not shifting bits, but the overhead is too significant to ignore.
The text was updated successfully, but these errors were encountered: