Scope storage class [Was: DMD 1.037 and 2.020 releases]

Robert Fraser fraserofthenight at gmail.com
Wed Nov 26 12:18:27 PST 2008


bearophile wrote:
> To test the new scoping features of D 2.021 I have used a small stressing program, coming from this page, originally by Knuth:
> 
> http://www.rosettacode.org/wiki/Man_or_boy_test
> 
> More info:
> http://en.wikipedia.org/wiki/Man_or_boy_test
> 
> My purpose is to see the D2 compiler being able to compute results up to N=25 on my PC (that has 2 GB RAM and a 32 bit operating system).
> 
> ---------------------------
> 
> This is the code I have used for DMD 1.037:
> 
> import std.c.stdio: printf;
> 
> int a(int k, lazy int x1, lazy int x2, lazy int x3, lazy int x4, lazy int x5) {
>     int delegate() b;
>     b = {
>         k -= 1;
>         return a(k, b(), x1, x2, x3, x4);
>     };
>     return k <= 0 ? x4 + x5 : b();
> }
> 
> void main() {
>     printf("%d\n", a(15, 1, -1, -1, 1, 0)); // N is 15
> }
> 
> ---------------------------
> 
> This is the code I have used for DMD 2.021. I was not sure how to use the scope, so if you have improvements please tell me:
> 
> import std.c.stdio: printf;
> 
> int a(scope int k, lazy int x1, lazy int x2, lazy int x3, lazy int x4, lazy int x5) {
>     scope int delegate() b;
>     b = {
>         k -= 1;
>         return a(k, b(), x1, x2, x3, x4);
>     };
>     return k <= 0 ? x4 + x5 : b();
> }
> 
> void main() {
>     printf("%d\n", a(15, 1, -1, -1, 1, 0)); // N is 15
> }
> 
> ---------------------------
> 
> The results for higher values of N are:
> k   21           22            23         24          25
> A   -389695     -865609     -1922362    -4268854    -9479595
> 
> In both cases I have compiled the code with:
> dmd -O -release -inline -L/STACK:1700000000 man_or_boy.d
> 
> The results:
> 
> DMD V.1.037 (exe: 168_476: bytes):
>   N=24:  788 MB RAM, 3 seconds
>   N=25: 1.57 GB RAM, 6.42 seconds
> 
> DMD V.2.021 (exe: 99_356 bytes):
> 
> The code was too much slow in D2, so I have stopped it. Do you know if the D2 code can be improved to have performance similar to D1 ones?
> 
> Bye,
> bearophile

Try marking all the "lazy" parameters as "scope" ("lazy" creates delegates).


More information about the Digitalmars-d-announce mailing list