digitalmars.D.learn - Fastest Way of Accessing Entries in an AA
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (4/4) Jan 08 2015 Is
- Dragos Carp (2/6) Jan 08 2015 aa.get(key, ValueType.init)
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (5/13) Jan 08 2015 That was too easy ;)
- ketmar via Digitalmars-d-learn (6/22) Jan 08 2015 On Thu, 08 Jan 2015 15:59:10 +0000
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (6/12) Jan 08 2015 Correction:
- Foo (5/17) Jan 08 2015 You know, that you kan reuse the result of the in operator by AAs?
- Jonathan M Davis via Digitalmars-d-learn (14/35) Jan 08 2015 This idiom is quite common:
- Foo (3/51) Jan 08 2015 I just wasn't sure that he knows about it.
- Jonathan M Davis via Digitalmars-d-learn (9/45) Jan 09 2015 Oh, he might not have known about it (certainly, the fact that he called...
- ketmar via Digitalmars-d-learn (11/27) Jan 08 2015 On Thu, 08 Jan 2015 23:06:38 +0000
Is key in aa ? aa[key] : ValueType.init; the most efficient way to maybe return a value from an associative array aa?
Jan 08 2015
On Thursday, 8 January 2015 at 15:45:27 UTC, Nordlöw wrote:Is key in aa ? aa[key] : ValueType.init; the most efficient way to maybe return a value from an associative array aa?aa.get(key, ValueType.init)
Jan 08 2015
On Thursday, 8 January 2015 at 15:49:46 UTC, Dragos Carp wrote:On Thursday, 8 January 2015 at 15:45:27 UTC, Nordlöw wrote:That was too easy ;) It would be nice if the compiler could detect usage of aa.get() automatically and issue either a diagnostics or transform it in an optimization pass.Is key in aa ? aa[key] : ValueType.init; the most efficient way to maybe return a value from an associative array aa?aa.get(key, ValueType.init)
Jan 08 2015
On Thu, 08 Jan 2015 15:59:10 +0000 "Nordl=C3=B6w" via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:On Thursday, 8 January 2015 at 15:49:46 UTC, Dragos Carp wrote:how can it? compiler doesn't know what the code is supposed to do. if compilers will know such things someday, we can stop writing programs altogether, as compilers will be able to write any program for us. ;-)On Thursday, 8 January 2015 at 15:45:27 UTC, Nordl=C3=B6w wrote:=20 That was too easy ;) =20 It would be nice if the compiler could detect usage of aa.get()=20 automatically and issue either a diagnostics or transform it in=20 an optimization pass.Is key in aa ? aa[key] : ValueType.init; the most efficient way to maybe return a value from an=20 associative array aa?aa.get(key, ValueType.init)
Jan 08 2015
On Thursday, 8 January 2015 at 16:11:07 UTC, ketmar via Digitalmars-d-learn wrote:how can it? compiler doesn't know what the code is supposed to do. if compilers will know such things someday, we can stop writing programs altogether, as compilers will be able to write any program for us. ;-)Correction: I thought it would be nice if the compiler explained to me that key in aa ? aa[key] is a sub-optimal performance-wise.
Jan 08 2015
On Thursday, 8 January 2015 at 23:06:39 UTC, Nordlöw wrote:On Thursday, 8 January 2015 at 16:11:07 UTC, ketmar via Digitalmars-d-learn wrote:You know, that you kan reuse the result of the in operator by AAs? example: auto ptr = key in aa; ptr ? *ptr : ValueType.inithow can it? compiler doesn't know what the code is supposed to do. if compilers will know such things someday, we can stop writing programs altogether, as compilers will be able to write any program for us. ;-)Correction: I thought it would be nice if the compiler explained to me that key in aa ? aa[key] is a sub-optimal performance-wise.
Jan 08 2015
On Friday, January 09, 2015 00:20:07 Foo via Digitalmars-d-learn wrote:On Thursday, 8 January 2015 at 23:06:39 UTC, Nordlöw wrote:This idiom is quite common: if(auto ptrToValue = key in aa) { } though I'm not sure that that quite fits in with what Nordlow seems to be trying to do with init. aa.get probably does a better job of that, though looking at the implementation for get, it's basically doing what you're suggesting: auto p = key in aa; return p ? *p : defaultValue; though that has the downside of using a lazy parameter for the default value, which is convenient but doesn't do great things for performance. - Jonathan M DavisOn Thursday, 8 January 2015 at 16:11:07 UTC, ketmar via Digitalmars-d-learn wrote:You know, that you kan reuse the result of the in operator by AAs? example: auto ptr = key in aa; ptr ? *ptr : ValueType.inithow can it? compiler doesn't know what the code is supposed to do. if compilers will know such things someday, we can stop writing programs altogether, as compilers will be able to write any program for us. ;-)Correction: I thought it would be nice if the compiler explained to me that key in aa ? aa[key] is a sub-optimal performance-wise.
Jan 08 2015
On Friday, 9 January 2015 at 06:18:53 UTC, Jonathan M Davis via Digitalmars-d-learn wrote:On Friday, January 09, 2015 00:20:07 Foo via Digitalmars-d-learn wrote:I just wasn't sure that he knows about it.On Thursday, 8 January 2015 at 23:06:39 UTC, Nordlöw wrote:This idiom is quite common: if(auto ptrToValue = key in aa) { } though I'm not sure that that quite fits in with what Nordlow seems to be trying to do with init. aa.get probably does a better job of that, though looking at the implementation for get, it's basically doing what you're suggesting: auto p = key in aa; return p ? *p : defaultValue; though that has the downside of using a lazy parameter for the default value, which is convenient but doesn't do great things for performance. - Jonathan M DavisOn Thursday, 8 January 2015 at 16:11:07 UTC, ketmar via Digitalmars-d-learn wrote:You know, that you kan reuse the result of the in operator by AAs? example: auto ptr = key in aa; ptr ? *ptr : ValueType.inithow can it? compiler doesn't know what the code is supposed to do. if compilers will know such things someday, we can stop writing programs altogether, as compilers will be able to write any program for us. ;-)Correction: I thought it would be nice if the compiler explained to me that key in aa ? aa[key] is a sub-optimal performance-wise.
Jan 08 2015
On Friday, January 09, 2015 07:51:27 Foo via Digitalmars-d-learn wrote:On Friday, 9 January 2015 at 06:18:53 UTC, Jonathan M Davis via Digitalmars-d-learn wrote:Oh, he might not have known about it (certainly, the fact that he called in and then [] in his origanal code implies that he didn't), and it was definitely useful to tell him. My point was simply that get applies better for his use case than using in directly. In general though, in is of _far_ more use than [], precisely because it combines asking whether the element is there and getting it into one command. Personally, I pretty much never use [] on AAs. - Jonathan M DavisOn Friday, January 09, 2015 00:20:07 Foo via Digitalmars-d-learn wrote:I just wasn't sure that he knows about it.You know, that you kan reuse the result of the in operator by AAs? example: auto ptr = key in aa; ptr ? *ptr : ValueType.initThis idiom is quite common: if(auto ptrToValue = key in aa) { } though I'm not sure that that quite fits in with what Nordlow seems to be trying to do with init. aa.get probably does a better job of that, though looking at the implementation for get, it's basically doing what you're suggesting: auto p = key in aa; return p ? *p : defaultValue; though that has the downside of using a lazy parameter for the default value, which is convenient but doesn't do great things for performance. - Jonathan M Davis
Jan 09 2015
On Thu, 08 Jan 2015 23:06:38 +0000 "Nordl=C3=B6w" via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:On Thursday, 8 January 2015 at 16:11:07 UTC, ketmar via=20 Digitalmars-d-learn wrote:although it's right in most cases, it's still implying that compiler is able to understand the code behind `in` and `[key]`, as they aren't built into the compiler, but rather coded in druntime. producing warning on such code can create wrong impression about compiler code analysing abilities. this time i think that it's a work for a linter, 'cause there is nothing wrong with the code, it's just a questionable style. so let linter question it. ;-)how can it? compiler doesn't know what the code is supposed to=20 do. if compilers will know such things someday, we can stop writing=20 programs altogether, as compilers will be able to write any program for=20 us. ;-)=20 Correction: =20 I thought it would be nice if the compiler explained to me that =20 key in aa ? aa[key] =20 is a sub-optimal performance-wise.
Jan 08 2015