Jump to page: 1 2 3
Thread overview
Dynamic Arrays & Class Properties
Aug 27, 2008
Mason Green
Aug 27, 2008
Denis Koroskin
Aug 27, 2008
Mason Green
Aug 27, 2008
Denis Koroskin
Aug 28, 2008
Mason Green
Aug 28, 2008
Brad Roberts
Aug 28, 2008
Brad Roberts
Aug 29, 2008
Brad Roberts
Aug 30, 2008
Walter Bright
Aug 30, 2008
Brad Roberts
Aug 30, 2008
Walter Bright
Sep 01, 2008
bearophile
Aug 28, 2008
Sergey Gromov
Aug 28, 2008
Denis Koroskin
Aug 28, 2008
Denis Koroskin
Aug 29, 2008
Bill Baxter
August 27, 2008
Hi,

Anyone know how to expose dynamic array elements via class properties?  I would like to do something this this:

class Foo {

    private int[] m_dummy;

    this() {
        m_dummy ~= 19;
        m_dummy ~= 77;
    }

    int dummy( ??????? ) {
        return m_dummy[x];
    }
}

void main() {
    auto foo = new Foo();
    Cout(foo.dummty[0]);     // Print 19
}

Any help would be much appreciated!  The question marks are where I'm stuck....

Thanks,
Mason

August 27, 2008
On Wed, 27 Aug 2008 17:59:09 +0400, Mason Green <mason.green@gmail.com> wrote:

> Hi,
>
> Anyone know how to expose dynamic array elements via class properties?  I would like to do something this this:
>
> class Foo {
>
>     private int[] m_dummy;
>
>     this() {
>         m_dummy ~= 19;
>         m_dummy ~= 77;
>     }
>
>     int dummy( ??????? ) {
>         return m_dummy[x];
>     }
> }
>
> void main() {
>     auto foo = new Foo();
>     Cout(foo.dummty[0]);     // Print 19
> }
>
> Any help would be much appreciated!  The question marks are where I'm stuck....
>
> Thanks,
> Mason
>

maybe something like this:

struct ConstArrayReference(T)
{
        T opIndex(int index) {
                return array[index];
        }

        private T[] array;
}

class Foo
{
    private int[] m_dummy;

    this() {
        m_dummy ~= 19;
        m_dummy ~= 77;
    }

    ConstArrayReference!(int) dummy() {
        ConstArrayReference!(int) r = { m_dummy };
        return r;
    }
}

void main() {
    auto foo = new Foo();
    Cout(foo.dummy[0]).newline;     // Prints 19
}


Too bad we don't have references (yet?) :(
August 27, 2008
Denis Koroskin Wrote:

> maybe something like this:
> 
> struct ConstArrayReference(T)
> {
>          T opIndex(int index) {
>                  return array[index];
>          }
> 
>          private T[] array;
> }
> 
> class Foo
> {
>      private int[] m_dummy;
> 
>      this() {
>          m_dummy ~= 19;
>          m_dummy ~= 77;
>      }
> 
>      ConstArrayReference!(int) dummy() {
>          ConstArrayReference!(int) r = { m_dummy };
>          return r;
>      }
> }
> 
> void main() {
>      auto foo = new Foo();
>      Cout(foo.dummy[0]).newline;     // Prints 19
> }
> 
> 
> Too bad we don't have references (yet?) :(

Thanks for the smart solution!

Unfortunately this seems like a lot of extra overhead... I may just end up keeping the dynamic arrays public!

In this particular case it would be nice to have the option of declaring friend classes, like in C++!!!

Regards,
Mason

August 27, 2008
On Wed, 27 Aug 2008 19:47:26 +0400, Mason Green <mason.green@gmail.com> wrote:

> Denis Koroskin Wrote:
>
>> maybe something like this:
>>
>> struct ConstArrayReference(T)
>> {
>>          T opIndex(int index) {
>>                  return array[index];
>>          }
>>
>>          private T[] array;
>> }
>>
>> class Foo
>> {
>>      private int[] m_dummy;
>>
>>      this() {
>>          m_dummy ~= 19;
>>          m_dummy ~= 77;
>>      }
>>
>>      ConstArrayReference!(int) dummy() {
>>          ConstArrayReference!(int) r = { m_dummy };
>>          return r;
>>      }
>> }
>>
>> void main() {
>>      auto foo = new Foo();
>>      Cout(foo.dummy[0]).newline;     // Prints 19
>> }
>>
>>
>> Too bad we don't have references (yet?) :(
>
> Thanks for the smart solution!
>
> Unfortunately this seems like a lot of extra overhead...

No, there shouldn't be any. But the code doesn't look good.

> I may just end up keeping the dynamic arrays public!
>
> In this particular case it would be nice to have the option of declaring friend classes, like in C++!!!
>
> Regards,
> Mason
>
August 28, 2008
"Mason Green" <mason.green@gmail.com> wrote in message news:g93sue$k3v$1@digitalmars.com...
> Denis Koroskin Wrote:
>
>> maybe something like this:
>>
>> struct ConstArrayReference(T)
>> {
>>          T opIndex(int index) {
>>                  return array[index];
>>          }
>>
>>          private T[] array;
>> }
>>
>> class Foo
>> {
>>      private int[] m_dummy;
>>
>>      this() {
>>          m_dummy ~= 19;
>>          m_dummy ~= 77;
>>      }
>>
>>      ConstArrayReference!(int) dummy() {
>>          ConstArrayReference!(int) r = { m_dummy };
>>          return r;
>>      }
>> }
>>
>> void main() {
>>      auto foo = new Foo();
>>      Cout(foo.dummy[0]).newline;     // Prints 19
>> }
>>
>>
>> Too bad we don't have references (yet?) :(
>
> Thanks for the smart solution!
>
> Unfortunately this seems like a lot of extra overhead... I may just end up keeping the dynamic arrays public!

There's no memory/register overhead; ConstArrayReference!(T).sizeof == (T[]).sizeof.

And if you use -inline, there's no function call overhead either.  :)


August 28, 2008
Jarrett Billingsley Wrote:

> 
> There's no memory/register overhead; ConstArrayReference!(T).sizeof == (T[]).sizeof.
> 
> And if you use -inline, there's no function call overhead either.  :)
> 
> 

Ah, thanks for the tip!

BTW, how much does using the -inline compiler flag have an effect on improving performance? Anyone have a good explanation as to how it works?

Thanks,
Mason
August 28, 2008
"Mason Green" <mason.green@gmail.com> wrote in message news:g96346$94o$1@digitalmars.com...
> Jarrett Billingsley Wrote:
>
>>
>> There's no memory/register overhead; ConstArrayReference!(T).sizeof ==
>> (T[]).sizeof.
>>
>> And if you use -inline, there's no function call overhead either.  :)
>>
>>
>
> Ah, thanks for the tip!
>
> BTW, how much does using the -inline compiler flag have an effect on improving performance? Anyone have a good explanation as to how it works?
>
> Thanks,
> Mason

It uh, inlines function calls.  So if you have a small function like:

int getSomething(ref SomeStruct s)
{
    return s.x * SomeConstant;
}

and you call it:

auto foo = getSomething(s);

It can inline the call to that function, making it effectively:

auto foo = s.x * SomeConstant;

but with the advantage that you don't have to break the rules of the language to do it (i.e. even if s.x is private, this will still work. Removing function calls, especially in tight loops and with very-often-called functions, *can* really improve performance.

The compiler has a cost/benefit heuristic that it uses to decide what functions should be inlined and what functions should just be called at runtime.  I'm not entirely sure the rules it uses but I know that any loops used in the function automatically disqualifies it for inlining.  More or less if your function is a one-liner that just evaluates an expression you can almost be guaranteed that it'll be inlined.

Notice before I said that inlining *can* improve performance.  It also *can* make it worse.  If it inlines too much, tight loops can become too large to fit into the processor's instruction cache and the code can actually run slower than if it were a separate function.  But in many cases, inlining improves perfomance.  How much is certainly a function of your coding style, what mood the compiler is in, and the phase of the moon.

Be warned though: the DMD backend sometimes generates buggy or incorrect code when using -inline, so be sure to test thoroughly both with and without the flag.


August 28, 2008
Mason Green <mason.green@gmail.com> wrote:
> Hi,
> 
> Anyone know how to expose dynamic array elements via class properties?  I would like to do something this this:
> 
> class Foo {
> 
>     private int[] m_dummy;
> 
>     this() {
>         m_dummy ~= 19;
>         m_dummy ~= 77;
>     }
> 
>     int dummy( ??????? ) {
>         return m_dummy[x];
>     }
> }
> 
> void main() {
>     auto foo = new Foo();
>     Cout(foo.dummty[0]);     // Print 19
> }
> 
> Any help would be much appreciated!  The question marks are where I'm stuck....

class Foo {
    private int[] m_dummy;
    const int dummy() {
        return m_dummy;
    }
}

I think this is better than tons of templates.

-- 
SnakE
August 28, 2008
Jarrett Billingsley wrote:
> 
> Be warned though: the DMD backend sometimes generates buggy or incorrect code when using -inline, so be sure to test thoroughly both with and without the flag. 
> 
> 

This is very much nitpicking, but the DMD *backend* is most likely not at fault here. All inlining is done in the frontend at the AST level, which is what is broken.

In LLVMDC I've had to disable the DMDFE inlining as it completely breaks our codegen. Luckily LLVM does a pretty good job at inlining on the bitcode IR...

Tomas
August 28, 2008
On Thu, 28 Aug 2008 22:22:44 +0400, Sergey Gromov <snake.scaly@gmail.com> wrote:

> Mason Green <mason.green@gmail.com> wrote:
>> Hi,
>>
>> Anyone know how to expose dynamic array elements via class properties?  I would like to do something this this:
>>
>> class Foo {
>>
>>     private int[] m_dummy;
>>
>>     this() {
>>         m_dummy ~= 19;
>>         m_dummy ~= 77;
>>     }
>>
>>     int dummy( ??????? ) {
>>         return m_dummy[x];
>>     }
>> }
>>
>> void main() {
>>     auto foo = new Foo();
>>     Cout(foo.dummty[0]);     // Print 19
>> }
>>
>> Any help would be much appreciated!  The question marks are where I'm stuck....
>
> class Foo {
>     private int[] m_dummy;
>     const int dummy() {
>         return m_dummy;
>     }
> }
>
> I think this is better than tons of templates.
>

First, it should be like this:

class Foo {
    this()
    {
        m_dummy = new int[1];
        m_dummy[0] = 19;
    }

    const(int)[] dummy() {
        return m_dummy;
    }

    private int[] m_dummy;
}

Second, that's D2 :)

That's kinda strange, but unfortunately most of the discussion here is about D1, not D2 :(
There is no const in D1:

const int[] test = [0, 1, 2, 3];
test[0] = 2;

writefln(test[0]); // prints 2
« First   ‹ Prev
1 2 3