Bug 13136 – Optimize double lookup from if (v in assocArray) { return assocArray[v]; }

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-07-15T20:03:21Z
Last change time
2019-05-14T06:42:15Z
Assigned to
No Owner
Creator
Mark Isaacson

Comments

Comment #0 by markisaa — 2014-07-15T20:03:21Z
Andrei just informed me that I was wrong in assuming that the compiler would optimize out the double lookup performed by: if (v in assocArray) { return assocArray[v]; } And that I should instead use: if (auto valuePtr = v in assocArray) { return *valuePtr; } He suggested that I post an enhancement request to make these compile to equivalent code. Personally I'm a fan of the former approach as I find it to be a bit cleaner.
Comment #1 by bearophile_hugs — 2014-07-15T20:45:22Z
(In reply to Mark Isaacson from comment #0) > He suggested that I post an enhancement request to make these compile to > equivalent code. Note that LDC already performs this optimization.
Comment #2 by nanayamae — 2019-05-14T06:42:15Z
I found this issue when browsing and I propose closing it as fixed. https://dlang.org/spec/hash-map.html#inserting_if_not_present In the meanwhile single-lookup assoc. array operations were added.