Open rakudrama opened 5 years ago
I really like this idea. Especially because that it can be applied selectively in some parts of the program and not others, so it doesn't require global tracking of values or determining that there are no dynamic accesses on the enum-like values.
QQ:
There is an
int
-valued discriminating field that is different for each constant.
do we care that it is an int
? maybe we just care that it is a primitive? or maybe we just care that there exists a single discriminating field (it is going to be constant based on the previous requirement, and if that field itself is enum-like, we could go deeper).
We care that it is an int since that allows a JavaScript implementation to detect a chain of integer comparisons and generate jump table or branch tree that is better than the implied linear scan.
A partial implementation was committed as e0846aa5c6ed3ecb07fbda2456cb38e62702504a. This handles the switch-label case for Dart enum classes. The improvement was 0.2% code size in Flutter Gallery, negligible in cm_shell.
I'm leaving this issue open, since it would have more impact if
PbEnum
classes were recognized
Dart enums, protocol buffer enums and other enum-like classes usually have discriminating
int
-valued member (often calledvalue
). This means that we can replaceidentical
comparisons of an enum value against a constructed constant with a comparison of thevalue
field against the discriminating value:or in a switch statement:
(or, guarding nullable values and reducing to the smallest out-of-domain non-negative number)
The benefits would be
A class is enum-like if we can determine that
const
.int
-valued discriminating field that is different for each constant.