Method template optimization that works in C++, but not very well D

bearophile bearophileHUGS at lycos.com
Wed Feb 19 07:40:40 PST 2014


Vladimir:

> Where I'm wrong? How  I  can rewrite the program to make effect 
> more evident? Or may be, this optimization is meaningless in 
> D...?

The L1 code cache is small and it's easy to create cache misses, 
so increasing the code size leads to nonlinear changes in the 
run-time.


> https://gist.github.com/Vladimir-Z/1a1755ce91cb0e7636b5

Small general suggestions for your code (untested!):

> enum TableSize = 5000000;

=>

In generale in D the name of functions and variabiles start with 
a lower case.

enum tableSize = 5_000_000;


> struct Row //of table

=>

struct Row /// Row of table.


>    enum NumberOptions = 1 << NumberField; // = pow(2, 
> NumberField)

=>

     enum NumberOptions = 2 ^^ numberField;


>    uint [NumberField] Fields;

=>

     uint[numberField] Fields;



> Row GenerateRow()
> {
>     Row row;
>     row.Fields[0] = rand() % 10;
>     row.Fields[1] = (rand() % 1)*(rand() % 10);
>     return row;

=>

Row generateRow()
{
     return Row(uniform(0, 10),
                uniform(0, 2) * uniform(0, 10));


> int ProccessRow(in Row TableRow, ref bool[] isUseField)
> {
>     int sum = 0;
>     foreach (i, value; TableRow.Fields)
>     {
>         if (isUseField[i])
>             sum += value * value - i;

=>

int proccessRow(in Row TableRow, in bool[] isUseField) pure 
nothrow
{
     int sum = 0;
     foreach (immutable i, immutable value; tableRow.fields)
     {
         if (isUseField[i])
             sum += value ^^ 2 - i;


> template TypeTuple(T...) { alias T TypeTuple; }

=>

alias TypeTuple(Types...) = Types;



> ProccessRowPtr [Row.NumberOptions] filters;

=>

__gshared proccessRowPtr[row.numberOptions] filters;

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list