X-Git-Url: https://git.llucax.com/software/dgc/cdgc.git/blobdiff_plain/e22e481ad2881b43acdc16fd6061bee9d68270f7..11b7fc5cdb1df190c6c434fea14a00e700721fbc:/rt/gc/cdgc/gc.d?ds=sidebyside diff --git a/rt/gc/cdgc/gc.d b/rt/gc/cdgc/gc.d index 14d323b..e7fdbd2 100644 --- a/rt/gc/cdgc/gc.d +++ b/rt/gc/cdgc/gc.d @@ -52,6 +52,22 @@ import rt.gc.cdgc.dynarray: DynArray; import cstdlib = tango.stdc.stdlib; import cstring = tango.stdc.string; +/* + * This is a small optimization that proved it's usefulness. For small chunks + * or memory memset() seems to be slower (probably because of the call) that + * simply doing a simple loop to set the memory. + */ +void memset(void* dst, int c, size_t n) +{ + // This number (32) has been determined empirically + if (n > 32) { + cstring.memset(dst, c, n); + return; + } + auto p = cast(ubyte*)(dst); + while (n-- > 0) + *p++ = c; +} version (GNU) { @@ -369,8 +385,8 @@ class GC // Return next item from free list gcx.bucket[bin] = (cast(List*)p).next; if( !(bits & BlkAttr.NO_SCAN) ) - cstring.memset(p + size, 0, binsize[bin] - size); - debug (MEMSTOMP) cstring.memset(p, 0xF0, size); + memset(p + size, 0, binsize[bin] - size); + debug (MEMSTOMP) memset(p, 0xF0, size); } else { @@ -422,7 +438,7 @@ class GC assert(size != 0); void *p = mallocNoSync(size, bits); - cstring.memset(p, 0, size); + memset(p, 0, size); return p; } @@ -516,7 +532,7 @@ class GC synchronized (gcLock) { debug (MEMSTOMP) - cstring.memset(p + size, 0xF2, psize - size); + memset(p + size, 0xF2, psize - size); pool.freePages(pagenum + newsz, psz - newsz); } return p; @@ -531,9 +547,9 @@ class GC if (i == pagenum + newsz) { debug (MEMSTOMP) - cstring.memset(p + psize, 0xF0, + memset(p + psize, 0xF0, size - psize); - cstring.memset(pool.pagetable + pagenum + + memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, newsz - psz); return p; } @@ -645,8 +661,8 @@ class GC if (sz < minsz) return 0; debug (MEMSTOMP) - cstring.memset(p + psize, 0xF0, (psz + sz) * PAGESIZE - psize); - cstring.memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, sz); + memset(p + psize, 0xF0, (psz + sz) * PAGESIZE - psize); + memset(pool.pagetable + pagenum + psz, B_PAGEPLUS, sz); gcx.p_cache = null; gcx.size_cache = 0; return (psz + sz) * PAGESIZE; @@ -737,7 +753,7 @@ class GC size_t n = pagenum; while (++n < pool.npages && pool.pagetable[n] == B_PAGEPLUS) npages++; - debug (MEMSTOMP) cstring.memset(p, 0xF2, npages * PAGESIZE); + debug (MEMSTOMP) memset(p, 0xF2, npages * PAGESIZE); pool.freePages(pagenum, npages); } else @@ -745,7 +761,7 @@ class GC // Add to free list List *list = cast(List*)p; - debug (MEMSTOMP) cstring.memset(p, 0xF2, binsize[bin]); + debug (MEMSTOMP) memset(p, 0xF2, binsize[bin]); list.next = gcx.bucket[bin]; gcx.bucket[bin] = list; @@ -1159,11 +1175,11 @@ class GC size_t n; size_t bsize = 0; - cstring.memset(&stats, 0, GCStats.sizeof); + memset(&stats, 0, GCStats.sizeof); - for (n = 0; n < gcx.npools; n++) + for (n = 0; n < pools.length; n++) { - Pool *pool = gcx.pooltable[n]; + Pool* pool = pools[n]; psize += pool.npages * PAGESIZE; for (size_t j = 0; j < pool.npages; j++) { @@ -1331,8 +1347,12 @@ const uint notbinsize[B_MAX] = [ ~(16u-1),~(32u-1),~(64u-1),~(128u-1),~(256u-1), ~(512u-1),~(1024u-1),~(2048u-1),~(4096u-1) ]; DynArray!(void*) roots; + DynArray!(Range) ranges; +DynArray!(Pool) pools; + + /* ============================ Gcx =============================== */ @@ -1352,9 +1372,6 @@ struct Gcx byte *minAddr; // min(baseAddr) byte *maxAddr; // max(topAddr) - size_t npools; - Pool **pooltable; - List *bucket[B_MAX]; // free list for each size @@ -1378,19 +1395,19 @@ struct Gcx //printf("Gcx.invariant(): this = %p\n", this); size_t i; - for (i = 0; i < npools; i++) + for (i = 0; i < pools.length; i++) { - Pool *pool = pooltable[i]; + Pool* pool = pools[i]; pool.Invariant(); if (i == 0) { assert(minAddr == pool.baseAddr); } - if (i + 1 < npools) + if (i + 1 < pools.length) { - assert(pool.opCmp(pooltable[i + 1]) < 0); + assert(*pool < pools[i + 1]); } - else if (i + 1 == npools) + else if (i + 1 == pools.length) { assert(maxAddr == pool.topAddr); } @@ -1419,22 +1436,20 @@ struct Gcx /** * Find Pool that pointer is in. * Return null if not in a Pool. - * Assume pooltable[] is sorted. + * Assume pools is sorted. */ Pool *findPool(void *p) { if (p >= minAddr && p < maxAddr) { - if (npools == 1) + if (pools.length == 1) { - return pooltable[0]; + return pools[0]; } - for (size_t i = 0; i < npools; i++) + for (size_t i = 0; i < pools.length; i++) { - Pool *pool; - - pool = pooltable[i]; + Pool* pool = pools[i]; if (p < pool.topAddr) { if (pool.baseAddr <= p) @@ -1636,7 +1651,7 @@ struct Gcx /** * Allocate a new pool of at least size bytes. - * Sort it into pooltable[]. + * Sort it into pools. * Mark all memory in the pool as B_FREE. * Return the actual number of bytes reserved or 0 on error. */ @@ -1660,27 +1675,22 @@ struct Gcx size_t pn; Pool* pool; - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { - pool = pooltable[n]; + pool = pools[n]; for (pn = 0; pn < pool.npages; pn++) { if (cast(Bins)pool.pagetable[pn] != B_FREE) break; } if (pn < pool.npages) - { - n++; continue; - } pool.Dtor(); - cstdlib.free(pool); - cstring.memmove(pooltable + n, - pooltable + n + 1, - (--npools - n) * (Pool*).sizeof); - minAddr = pooltable[0].baseAddr; - maxAddr = pooltable[npools - 1].topAddr; + pools.remove_at(n); + n--; } + minAddr = pools[0].baseAddr; + maxAddr = pools[pools.length - 1].topAddr; } @@ -1705,9 +1715,9 @@ struct Gcx // This code could use some refinement when repeatedly // allocating very large arrays. - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { - pool = pooltable[n]; + pool = pools[n]; pn = pool.allocPages(npages); if (pn != OPFAIL) goto L1; @@ -1724,7 +1734,7 @@ struct Gcx } // Try collecting freedpages = fullcollectshell(); - if (freedpages >= npools * ((POOLSIZE / PAGESIZE) / 4)) + if (freedpages >= pools.length * ((POOLSIZE / PAGESIZE) / 4)) { state = 1; continue; @@ -1761,10 +1771,10 @@ struct Gcx L1: pool.pagetable[pn] = B_PAGE; if (npages > 1) - cstring.memset(&pool.pagetable[pn + 1], B_PAGEPLUS, npages - 1); + memset(&pool.pagetable[pn + 1], B_PAGEPLUS, npages - 1); p = pool.baseAddr + pn * PAGESIZE; - cstring.memset(cast(char *)p + size, 0, npages * PAGESIZE - size); - debug (MEMSTOMP) cstring.memset(p, 0xF1, size); + memset(cast(char *)p + size, 0, npages * PAGESIZE - size); + debug (MEMSTOMP) memset(p, 0xF1, size); return p; Lnomemory: @@ -1774,16 +1784,11 @@ struct Gcx /** * Allocate a new pool with at least npages in it. - * Sort it into pooltable[]. + * Sort it into pools. * Return null if failed. */ Pool *newPool(size_t npages) { - Pool* pool; - Pool** newpooltable; - size_t newnpools; - size_t i; - // Minimum of POOLSIZE if (npages < POOLSIZE/PAGESIZE) npages = POOLSIZE/PAGESIZE; @@ -1796,9 +1801,9 @@ struct Gcx } // Allocate successively larger pools up to 8 megs - if (npools) + if (pools.length) { - size_t n = npools; + size_t n = pools.length; if (n > 8) n = 8; // cap pool size at 8 megs n *= (POOLSIZE / PAGESIZE); @@ -1806,41 +1811,21 @@ struct Gcx npages = n; } - pool = cast(Pool *) cstdlib.calloc(1, Pool.sizeof); - if (pool) + Pool p; + p.initialize(npages); + if (!p.baseAddr) { - pool.initialize(npages); - if (!pool.baseAddr) - goto Lerr; - - newnpools = npools + 1; - newpooltable = cast(Pool **) cstdlib.realloc(pooltable, - newnpools * (Pool *).sizeof); - if (!newpooltable) - goto Lerr; - - // Sort pool into newpooltable[] - for (i = 0; i < npools; i++) - { - if (pool.opCmp(newpooltable[i]) < 0) - break; - } - cstring.memmove(newpooltable + i + 1, newpooltable + i, - (npools - i) * (Pool *).sizeof); - newpooltable[i] = pool; - - pooltable = newpooltable; - npools = newnpools; + p.Dtor(); + return null; + } - minAddr = pooltable[0].baseAddr; - maxAddr = pooltable[npools - 1].topAddr; + Pool* pool = pools.insert_sorted(p); + if (pool) + { + minAddr = pools[0].baseAddr; + maxAddr = pools[pools.length - 1].topAddr; } return pool; - - Lerr: - pool.Dtor(); - cstdlib.free(pool); - return null; } @@ -1857,9 +1842,9 @@ struct Gcx byte* p; byte* ptop; - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { - pool = pooltable[n]; + pool = pools[n]; pn = pool.allocPages(1); if (pn != OPFAIL) goto L1; @@ -2053,9 +2038,9 @@ struct Gcx size_cache = 0; anychanges = 0; - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { - pool = pooltable[n]; + pool = pools[n]; pool.mark.zero(); pool.scan.zero(); pool.freebits.zero(); @@ -2072,9 +2057,9 @@ struct Gcx } } - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { - pool = pooltable[n]; + pool = pools[n]; pool.mark.copy(&pool.freebits); } @@ -2104,13 +2089,13 @@ struct Gcx while (anychanges) { anychanges = 0; - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { uint *bbase; uint *b; uint *btop; - pool = pooltable[n]; + pool = pools[n]; bbase = pool.scan.base(); btop = bbase + pool.scan.nwords; @@ -2170,9 +2155,9 @@ struct Gcx debug(COLLECT_PRINTF) printf("\tfree'ing\n"); size_t freedpages = 0; size_t freed = 0; - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { - pool = pooltable[n]; + pool = pools[n]; uint* bbase = pool.mark.base(); size_t pn; for (pn = 0; pn < pool.npages; pn++, bbase += PAGESIZE / (32 * 16)) @@ -2201,7 +2186,7 @@ struct Gcx List *list = cast(List *)p; - debug (MEMSTOMP) cstring.memset(p, 0xF3, size); + debug (MEMSTOMP) memset(p, 0xF3, size); } pool.pagetable[pn] = B_FREE; freed += PAGESIZE; @@ -2221,7 +2206,7 @@ struct Gcx List *list = cast(List *)p; - debug (MEMSTOMP) cstring.memset(p, 0xF3, size); + debug (MEMSTOMP) memset(p, 0xF3, size); freed += size; } @@ -2241,7 +2226,7 @@ struct Gcx debug(COLLECT_PRINTF) printf("\tcollecting big %x\n", p); pool.pagetable[pn] = B_FREE; freedpages++; - debug (MEMSTOMP) cstring.memset(p, 0xF3, PAGESIZE); + debug (MEMSTOMP) memset(p, 0xF3, PAGESIZE); while (pn + 1 < pool.npages && pool.pagetable[pn + 1] == B_PAGEPLUS) { pn++; @@ -2251,7 +2236,7 @@ struct Gcx debug (MEMSTOMP) { p += PAGESIZE; - cstring.memset(p, 0xF3, PAGESIZE); + memset(p, 0xF3, PAGESIZE); } } } @@ -2265,9 +2250,9 @@ struct Gcx // Free complete pages, rebuild free list debug(COLLECT_PRINTF) printf("\tfree complete pages\n"); size_t recoveredpages = 0; - for (n = 0; n < npools; n++) + for (n = 0; n < pools.length; n++) { - pool = pooltable[n]; + pool = pools[n]; for (size_t pn = 0; pn < pool.npages; pn++) { Bins bin = cast(Bins)pool.pagetable[pn]; @@ -2310,7 +2295,7 @@ struct Gcx } debug(COLLECT_PRINTF) printf("recovered pages = %d\n", recoveredpages); - debug(COLLECT_PRINTF) printf("\tfree'd %u bytes, %u pages from %u pools\n", freed, freedpages, npools); + debug(COLLECT_PRINTF) printf("\tfree'd %u bytes, %u pages from %u pools\n", freed, freedpages, pools.length); return freedpages + recoveredpages; } @@ -2432,7 +2417,7 @@ struct Pool pagetable = cast(ubyte*) cstdlib.malloc(npages); if (!pagetable) onOutOfMemoryError(); - cstring.memset(pagetable, B_FREE, npages); + memset(pagetable, B_FREE, npages); this.npages = npages; } @@ -2521,19 +2506,19 @@ struct Pool */ void freePages(size_t pagenum, size_t npages) { - cstring.memset(&pagetable[pagenum], B_FREE, npages); + memset(&pagetable[pagenum], B_FREE, npages); } /** - * Used for sorting pooltable[] + * Used for sorting pools */ - int opCmp(Pool *p2) + int opCmp(in Pool other) { - if (baseAddr < p2.baseAddr) + if (baseAddr < other.baseAddr) return -1; else - return cast(int)(baseAddr > p2.baseAddr); + return cast(int)(baseAddr > other.baseAddr); } }