Changeset 7b0e8b7


Ignore:
Timestamp:
Oct 18, 2021, 4:31:04 PM (3 years ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
6f7aff3
Parents:
804bf677
Message:

String heap growth implemented

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/containers/string_res.cfa

    r804bf677 r7b0e8b7  
    4444   
    4545static inline void compaction( VbyteHeap & );                           // compaction of the byte area
    46 static inline void garbage( VbyteHeap & );                              // garbage collect the byte area
     46static inline void garbage( VbyteHeap &, int );                         // garbage collect the byte area
    4747static inline void extend( VbyteHeap &, int );                  // extend the size of the byte area
    4848static inline void reduce( VbyteHeap &, int );                  // reduce the size of the byte area
     
    179179}
    180180
     181size_t DEBUG_string_bytes_in_heap( VbyteHeap * heap ) {
     182    return heap->CurrSize;
     183}
     184
    181185const char * DEBUG_string_heap_start( VbyteHeap * heap ) {
    182186    return heap->StartVbyte;
    183187}
    184 
    185188
    186189// Returns the size of the string in bytes
     
    759762    NoBytes = ( uintptr_t )EndVbyte + size;
    760763    if ( NoBytes > ( uintptr_t )ExtVbyte ) {            // enough room for new byte-string ?
    761                 garbage( this );                                        // firer up the garbage collector
     764                garbage( this, size );                                  // firer up the garbage collector
    762765                NoBytes = ( uintptr_t )EndVbyte + size;         // try again
    763766                if ( NoBytes > ( uintptr_t )ExtVbyte ) {        // enough room for new byte-string ?
    764 // unimplemented feature - assert, not verify
    765 assert( 0 && "need to implement actual growth" );
    766                         // extend( size );                              // extend the byte-string area
     767            assert( 0 && "garbage run did not free up required space" );
    767768                } // if
    768769    } // if
     
    925926// the heap.  The heap is then compacted in the existing heap or into the newly allocated heap.
    926927
    927 void garbage(VbyteHeap & this ) with(this) {
     928void garbage(VbyteHeap & this, int minreq ) with(this) {
    928929#ifdef VbyteDebug
    929930    serr | "enter:garbage";
     
    949950    AmountFree = ( uintptr_t )ExtVbyte - ( uintptr_t )StartVbyte - AmountUsed;
    950951   
    951     if ( AmountFree < ( int )( CurrSize * 0.1 )) {      // free space less than 10% ?
    952 
    953 // unimplemented feature - assert, not verify
    954 assert( 0 && "need to implement actual growth" );
    955 //              extend( CurrSize );                             // extend the heap
     952    if ( ( double ) AmountFree < ( CurrSize * 0.1 ) || AmountFree < minreq ) {  // free space less than 10%  or not enough to serve cur request
     953
     954                extend( this, max( CurrSize, minreq ) );                                // extend the heap
    956955
    957956                        //  Peter says, "This needs work before it should be used."
     
    980979#undef VbyteDebug
    981980
    982 //WIP
    983 #if 0
    984981
    985982
     
    987984// area is deleted.
    988985
    989 void VbyteHeap::extend( int size ) {
     986void extend( VbyteHeap & this, int size ) with (this) {
    990987#ifdef VbyteDebug
    991988    serr | "enter:extend, size:" | size;
     
    997994   
    998995    CurrSize += size > InitSize ? size : InitSize;      // minimum extension, initial size
    999     StartVbyte = EndVbyte = new char[CurrSize];
     996    StartVbyte = EndVbyte = alloc(CurrSize);
    1000997    ExtVbyte = (void *)( StartVbyte + CurrSize );
    1001     compaction();                                       // copy from old heap to new & adjust pointers to new heap
    1002     delete OldStartVbyte;                               // release old heap
     998    compaction(this);                                   // copy from old heap to new & adjust pointers to new heap
     999    free( OldStartVbyte );                              // release old heap
    10031000#ifdef VbyteDebug
    10041001    serr | "exit:extend, CurrSize:" | CurrSize;
     
    10061003} // extend
    10071004
     1005//WIP
     1006#if 0
    10081007
    10091008// Extend the size of the byte-string area by creating a new area and copying the old area into it. The old byte-string
  • libcfa/src/containers/string_res.hfa

    r804bf677 r7b0e8b7  
    3434
    3535VbyteHeap * DEBUG_string_heap();
     36size_t DEBUG_string_bytes_in_heap( VbyteHeap * heap );
    3637size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap );
    3738const char * DEBUG_string_heap_start( VbyteHeap * heap );
  • tests/collections/.expect/string-ctx-manage.txt

    r804bf677 r7b0e8b7  
    44bye
    55hi
     6bye
    67done
  • tests/collections/.expect/string-gc.txt

    r804bf677 r7b0e8b7  
    3838x from 5 to 15
    3939y from 5 to 15
     40======================== fillNoCompact
     41about to expand, a = aaa
     42expanded, a = aaa
     43about to expand, a = aaa
     44expanded, a = aaa
     45about to expand, a = aaa
     46expanded, a = aaa
     47about to expand, a = aaa
     48expanded, a = aaa
     49about to expand, a = aaa
     50expanded, a = aaa
  • tests/collections/string-ctx-manage.cfa

    r804bf677 r7b0e8b7  
    3636    string y = x; // y allocates into private pad, implying eager copy
    3737    assert( y.inner->Handle.s != x.inner->Handle.s);
     38    assert( DEBUG_string_bytes_in_heap(y.inner->Handle.ulink) == y.inner->Handle.lnth );  // y is in a perfectly fitting heap
    3839    sout | y; // hi
    3940
    40     // -- following hits "need to implement actual growth"
    41     //    and it passes if I modify string_res.cfa to oversize the owned heaps
    42 
    43     // x = "bye";
    44     // y = x; // into private y => eager copy
    45     // assert( y.inner->Handle.s != x.inner->Handle.s);
    46     // sout | y; // bye
     41    x = "bye";
     42    y = x; // into private y => eager copy
     43    assert( y.inner->Handle.s != x.inner->Handle.s);
     44//    assert( DEBUG_string_bytes_in_heap(y.inner->Handle.ulink) == y.inner->Handle.lnth );  // optimization required
     45    sout | y; // bye
    4746}
    4847
  • tests/collections/string-gc.cfa

    r804bf677 r7b0e8b7  
    120120}
    121121
     122void fillNoCompact() {
     123    // show that allocating in a heap filled with mostly live strings (no collectable garbage) causes heap growth
     124
     125    sout | "======================== fillNoCompact";
     126
     127    size_t lastTimeBytesAvail = bytesRemaining();
     128    assert( lastTimeBytesAvail >= 200 ); // starting this test with nontrivial room
     129
     130    // mostly fill the pad
     131    string_res a = "aaa";  // will have to be moved
     132    string_res z = "zzz";
     133    for (i; 5) {
     134        while ( bytesRemaining() > 10 ) {
     135            z += ".";
     136        }
     137        sout | "about to expand, a = " | a;
     138        while ( bytesRemaining() <= 10 ) {
     139            z += ".";
     140        }
     141        sout | "expanded, a = " | a;
     142
     143        // each growth gives more usable space than the last
     144        assert( bytesRemaining() > lastTimeBytesAvail );
     145        lastTimeBytesAvail = bytesRemaining();
     146    }
     147}
     148
    122149int main() {
    123150    basicFillCompact();
    124151    fillCompact_withSharedEdits();
     152    fillNoCompact();
    125153}
Note: See TracChangeset for help on using the changeset viewer.