You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In D there are handy associative array literals, so D code contains idioms that are more typical of dynamic languages as Python:
size_t foo(in char c) {
immutable size_t[char] indexer =
['D': 2, 'R': 5, 'C': 8, 'H': 9, 'W': 0];
return indexer[c];
}
size_t foo(in char c) {
return ['D': 2, 'R': 5, 'C': 8, 'H': 9, 'W': 0][c];
}
So I suggest to add to D front-end an optimization, that rewrites those usages of associative arrays with a switch:
size_t foo(in char c) {
size_t value = size_t.max;
switch (c) {
case 'D': value = 2; break;
case 'R': value = 5; break;
case 'C': value = 8; break;
case 'H': value = 9; break;
case 'W': value = 0; break;
default: assert(false);
}
return value;
}
This should be faster, avoid GC activity, and produce simpler binaries.
The text was updated successfully, but these errors were encountered:
bearophile_hugs reported this on 2014-05-21T23:23:00Z
Transferred from https://issues.dlang.org/show_bug.cgi?id=12785
Description
In D there are handy associative array literals, so D code contains idioms that are more typical of dynamic languages as Python: size_t foo(in char c) { immutable size_t[char] indexer = ['D': 2, 'R': 5, 'C': 8, 'H': 9, 'W': 0]; return indexer[c]; } size_t foo(in char c) { return ['D': 2, 'R': 5, 'C': 8, 'H': 9, 'W': 0][c]; } So I suggest to add to D front-end an optimization, that rewrites those usages of associative arrays with a switch: size_t foo(in char c) { size_t value = size_t.max; switch (c) { case 'D': value = 2; break; case 'R': value = 5; break; case 'C': value = 8; break; case 'H': value = 9; break; case 'W': value = 0; break; default: assert(false); } return value; } This should be faster, avoid GC activity, and produce simpler binaries.The text was updated successfully, but these errors were encountered: