Binary data-structure serialization

bearophile bearophileHUGS at lycos.com
Wed Jun 2 04:51:45 PDT 2010


Andrei Alexandrescu:
> De facto no, de jure yes.

Walter is listening you (see comment 6):
http://d.puremagic.com/issues/show_bug.cgi?id=2070

I have used a good number of "scope" in classes for my small programs that I am slowly porting from D1 to D2. A higher level language, that omits manual low-level optimizations requires a more powerful compiler, able to optimize away some abstractions by itself. So I hope D will soon use a good escape analysis to stack-allocate classes (and dynamic arrays), as recent versions of HotSpot do (but even with an escape analysis, the objects inside objects get allocated elsewhere anyway. Maybe this requires another kind of escape analysis?). (I have said "use" because I think DMD is already able to perform some escape analysis).


http://java.sun.com/javase/6/webnotes/6u14.html
>Optimization Using Escape Analysis: The -XX:+DoEscapeAnalysis option directs HotSpot to look for objects that are created and referenced by a single thread within the scope of a method compilation. Allocation is omitted for such non-escaping objects, and their fields are treated as local variables, often residing in machine registers. Synchronization on non-escaping objects is also elided.<


EA, from page 52:
http://webdocs.cs.ualberta.ca/~amaral/IBM-Stoodley-talks/UofAKASWorkshop.pdf


To focus it's better to start with a very simple example. So at first dmd can remove the heap allocation in this basic case:


import std.stdio: writeln;

final class Rectangle {
    int x, y;

    this(int xx, int yy) {
        this.x = xx;
        this.y = yy;
    }

    int area() {
        return this.x * this.y;
    }
}

void main () {
    enum N = 100;

    ulong tot = 0;
    foreach (x; 0 .. N)
        foreach (y; 0 .. N) {
            Rectangle rectangle = new Rectangle(x, y);
            tot += rectangle.area();
        }

    writeln(tot);
}



So the optimization phase can turn that into something equivalent to:

import std.stdio: writeln;

void main () {
    enum N = 100;
    ulong tot = 0;
    foreach (x; 0 .. N)
        foreach (y; 0 .. N)
            tot += x * y;
    writeln(tot);
}


That was a naive/toy example, but you can't start from very complex things. Later more complex cases can be added, like dynamic arrays, synchronized methods, etc.

Bye,
bearophile


More information about the Digitalmars-d mailing list