www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Two Optimizations

I have two optimization suggestions: perhaps not part of the language design.

1. If an object is created in a function scope (with new), but the pointer to
that object is never copied into an outside object, it automatically becomes an
auto object.  This might be difficult to detect in some cases, but not doing the
optimization is harmless.


foo(char[] s)
char[] xyz = "hello, " ~ s;
char[] b = xyz ~ "\0";

//(possibly use or store b somewhere)

Since the pointer to xyz is never put in an object, it can become "auto".

2. Object absorbtion:

In a similar way, objects that are never passed out of a class could be

class rectangle {
int area() {return a*b;}
int a = 5;
int b = 7;

int foo()
rectangle r = new rectangle;

return r.area();

Would be equivalent to:

int foo()
int r_a = 5;
int r_b = 7;
return r_a * r_b;

. opening the door to more optimizations, without putting referenced objects on
the stack.  These opts are restricted in a similar way to "register" vars, i.e.
taking the address conflicts with optimization.

May 10 2004