Re: template mixin in class is virtual function?

2010-05-21 Thread div0
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

strtr wrote:
 Or more to the point:
 
 Can (Class) template mixin functions be devirtualized by the compiler or do I 
 (as
 optimization) need to manually copy paste the boiler plate code?

You can just wrap the mixin in a final block:

import std.stdio;

template bar(T) {
void test(T t) {
writefln(t: %s, t);
}
}

class foo {
final {
mixin bar!(int);
}
}

int main(){
scope f = new foo;
f.test(3);
return 0;
}

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFL9uBpT9LetA9XoXwRAv83AKDNlqLgcI7C0g5x5t6CnUq82C5/xACghKUF
p9whtC6i/L7jrJyuLg+RZWo=
=BGBu
-END PGP SIGNATURE-


Re: template mixin in class is virtual function?

2010-05-21 Thread strtr
== Quote from div0 (d...@users.sourceforge.net)'s article
 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1
 strtr wrote:
  Or more to the point:
 
  Can (Class) template mixin functions be devirtualized by the compiler or do 
  I (as
  optimization) need to manually copy paste the boiler plate code?
 You can just wrap the mixin in a final block:
 import std.stdio;
 template bar(T) {
   void test(T t) {
   writefln(t: %s, t);
   }
 }
 class foo {
   final {
   mixin bar!(int);
   }
 }
 int main(){
   scope f = new foo;
   f.test(3);
   return 0;
 }

Is that different from making all functions within the template final?



Re: template mixin in class is virtual function?

2010-05-21 Thread div0
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

strtr wrote:
 == Quote from div0 (d...@users.sourceforge.net)'s article
 -BEGIN PGP SIGNED MESSAGE-
 Hash: SHA1
 strtr wrote:
 Or more to the point:

 Can (Class) template mixin functions be devirtualized by the compiler or do 
 I (as
 optimization) need to manually copy paste the boiler plate code?
 You can just wrap the mixin in a final block:
 import std.stdio;
 template bar(T) {
  void test(T t) {
  writefln(t: %s, t);
  }
 }
 class foo {
  final {
  mixin bar!(int);
  }
 }
 int main(){
  scope f = new foo;
  f.test(3);
  return 0;
 }
 
 Is that different from making all functions within the template final?
 

Well it delegates the choice of virtual versus non virtual to the class
using the mixin. No idea if that's a good idea or a bad one; I guess
you'd have to think very carefully about what your mixin is trying to
achieve.

I just tried marking a function in the template final
and that seems to work as well, so you could have a mix of virtual and
non virtual in a template, but that feels like a hackish design.

So you can it appears do this: (though that's dmd 2.028)

template bar(T) {
   final:
void test(T t) {
writefln(t: %s, t);
}
void test2() {
}
}

class foo {
mixin bar!(int);
}

int main(){
foo f = new foo;
f.test(3);
f.test2();
return 0;
}

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFL9v4HT9LetA9XoXwRAgIvAKC8zwQP1EeaU/XkRijfm7m00nXqUQCdEdsN
CV/vi6l5JGnqH5M2WXU6gjU=
=0n7A
-END PGP SIGNATURE-


Re: template mixin in class is virtual function?

2010-05-21 Thread strtr
== Quote from div0 (d...@users.sourceforge.net)'s article
 strtr wrote:
  Is that different from making all functions within the template final?

 Well it delegates the choice of virtual versus non virtual to the class
 using the mixin. No idea if that's a good idea or a bad one; I guess
 you'd have to think very carefully about what your mixin is trying to
 achieve.
 I just tried marking a function in the template final
 and that seems to work as well, so you could have a mix of virtual and
 non virtual in a template, but that feels like a hackish design.
 So you can it appears do this: (though that's dmd 2.028)
 template bar(T) {
final:
   void test(T t) {
   writefln(t: %s, t);
   }
   void test2() {
   }
 }
 class foo {
   mixin bar!(int);
 }
 int main(){
   foo f = new foo;
   f.test(3);
   f.test2();
   return 0;
 }

I've been marking functions final on a per function basis within the templates 
but
after reading the docs I was afraid the compiler might just ignore the attribute
and make all mixin-template-functions virtual.

Mixins can add virtual functions to a class
http://www.digitalmars.com/d/1.0/template-mixin.html

But that sentence was probably meant as an example not the exclusive 
description :)

Anyway, Thanks!!
Shouldn't be thinking about optimization anyways ;)


Re: Loop optimization

2010-05-21 Thread Walter Bright

kai wrote:


Here is a boiled down test case:

void main (string[] args)
{
double [] foo = new double [cast(int)1e6];
for (int i=0;i1e3;i++)
{
for (int j=0;j1e6-1;j++)
{
foo[j]=foo[j]+foo[j+1];
}
}
}

Any ideas?


for (int j=0;j1e6-1;j++)

The j1e6-1 is a floating point operation. It should be redone as an int one:
 j1_000_000-1