std.data.json formal review

Johannes Pfau via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 18 04:02:17 PDT 2015


Am Tue, 18 Aug 2015 09:10:25 +0000
schrieb "Marc Schütz" <schuetzm at gmx.net>:

> On Monday, 17 August 2015 at 22:34:36 UTC, Andrei Alexandrescu 
> wrote:
> > On 8/17/15 2:51 PM, deadalnix wrote:
> >>  From the compiler perspective, the tag is much nicer. 
> >> Compiler can use
> >> jump table for instance.
> >
> > The pointer is a more direct conduit to a jump table.
> 
> Not really, because it most likely doesn't point to where you 
> need it, but to a `TypeInfo` struct instead, which doesn't help 
> you in a `switch` statement. Besides, you probably shouldn't 
> compare pointers vs integers, but pointers vs enums.

Here's an example with an enum tag, showing what compilers can do:
http://goo.gl/NUZwNo

ARM ASM is easier to read for me. Feel free to switch to X86.

The jump table requires only one instruction (the cmp #4 shouldn't be
necessary for a final switch, probably a GDC/GCC enhancement). All
instructions/data should be in the instruction cache. There's no
register save / function call overhead.


If you use a pointer:
http://goo.gl/9kb0vQ

No jump table optimization. Cache should be OK as well. No call
overhead.


Note how both examples can also combine the code for uint/int. If you
use a function pointer instead you'll call different function.


Calling a function through pointer:
http://goo.gl/zTU3sA

You have one indirect call. Probably hard for the branch prediction,
although I don't really know. Probably also worse regarding cache. I
also cheated by using one pointer only for add. In reality you'll need
to store one pointer per operation or use a switch inside the called
function.


I think it's reasonable to expect the enum version to be faster. To be
really sure we'd need some benchmarks.



More information about the Digitalmars-d mailing list