Bug 12785 – Optimize with switches some associative array usage idioms

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-05-21T23:23:00Z
Last change time
2024-12-13T18:20:49Z
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: dmd#17660 →

Comments

Comment #0 by bearophile_hugs — 2014-05-21T23:23:00Z
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.
Comment #1 by robert.schadek — 2024-12-13T18:20:49Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17660 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB