[Issue 6445] [CTFE] Absurd memory usage (still) on building array

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Aug 15 00:48:46 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=6445


Don <clugdbug at yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID


--- Comment #5 from Don <clugdbug at yahoo.com.au> 2011-08-15 00:48:37 PDT ---
(In reply to comment #4)
> (In reply to comment #2)
> > (In reply to comment #0)
> > > The following program uses over a gigabyte of memory at compile time.  I
> > > thought these kinds of issues were fixed by the rewrite of CTFE in the last
> > > release.
> > 
> > No, bug 1382 is still open. Some cases of it have been fixed (eg, the one in
> > comment 4). I'm not sure why this particular case is so bad, I'll take a look
> > at it when I get a chance.
> > Note that CTFE still uses copy-on-write for values, and no memory is ever
> > released (this is more general than bug 1382). My guess is that the code below
> > just runs for a very long time.
> 
> Values == ints, floats, other primitives?  What about static arrays?

Just integers (includes int, bool, char types) and floats. Static arrays aren't
duplicated.

This one just uses a lot of memory because it performs so many assignments, as
you can see if you count the number of iterations, as in the code below.
It's O(n^^2), so with n=1000, it loops about 4 million times.

It's nothing to do with arrays, and the memory usage is only linear with number
of assignments, so I'm marking this as invalid.
I'll create a new bug for the general memory usage issue.

-----------
enum staticFacTableLen = 10;
//  10 -->     436 iterations
// 100 -->   45598 iterations
// 200 -->  179242
// 400 -->  704609
// 600 --> 1568674
// 800 --> out of memory

int makeLogFacTable() pure nothrow {

    static int logCount(real x) pure nothrow @safe
    {
        immutable xMinusPlus = (x - 1) / (x + 1);
        immutable xMinusPlusSquared = xMinusPlus * xMinusPlus;
        real xMinusPlusPow = xMinusPlus * xMinusPlusSquared;
        int kkk = 0;

        real ret = xMinusPlus;
        real power = 3;

        while(true)
        {
            ++kkk;
            immutable toAdd = xMinusPlusPow / power;
            immutable oldRet = ret;
            ret += toAdd;

            if(ret == oldRet || ret != ret)
            {
                return kkk;
            }

            power += 2;
            xMinusPlusPow *= xMinusPlusSquared;
        }

        assert(0);
    }
    int totalcount = 0;
    foreach(i; 1..staticFacTableLen) {
        totalcount +=logCount(i);
    }
    return totalcount;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list