Changeset 4e8df745


Ignore:
Timestamp:
Oct 26, 2021, 4:27:10 PM (3 years ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
Children:
1733184
Parents:
2b30370
Message:

String performance improvements given hybrid design

Location:
libcfa/src/containers
Files:
2 edited

Legend:

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

    r2b30370 r4e8df745  
    1818
    1919#include <stdlib.hfa>  // e.g. malloc
    20 #include <string.h>    // e.g. strlen
    2120#include <assert.h>
    2221
     
    4342
    4443   
    45 static inline void compaction( VbyteHeap & );                           // compaction of the byte area
    46 static inline void garbage( VbyteHeap &, int );                         // garbage collect the byte area
    47 static inline void extend( VbyteHeap &, int );                  // extend the size of the byte area
    48 static inline void reduce( VbyteHeap &, int );                  // reduce the size of the byte area
    49 
    50 static inline void ?{}( VbyteHeap &, size_t = 1000 );
    51 static inline void ^?{}( VbyteHeap & );
    52 static inline void ByteCopy( char *, int, int, char *, int, int ); // copy a block of bytes from one location in the heap to another
    53 static inline int ByteCmp( char *, int, int, char *, int, int );        // compare 2 blocks of bytes
    54 static inline char *VbyteAlloc( VbyteHeap &, int );                     // allocate a block bytes in the heap
    55 static inline char *VbyteTryAdjustLast( VbyteHeap &, int );
    56 
    57 static inline void AddThisAfter( HandleNode &, HandleNode & );
    58 static inline void DeleteNode( HandleNode & );
    59 static inline void MoveThisAfter( HandleNode &, const HandleNode & );           // move current handle after parameter handle
     44static void compaction( VbyteHeap & );                          // compaction of the byte area
     45static void garbage( VbyteHeap &, int );                                // garbage collect the byte area
     46static void extend( VbyteHeap &, int );                 // extend the size of the byte area
     47static void reduce( VbyteHeap &, int );                 // reduce the size of the byte area
     48
     49static void ?{}( VbyteHeap &, size_t = 1000 );
     50static void ^?{}( VbyteHeap & );
     51static void ByteCopy( char *, int, int, char *, int, int ); // copy a block of bytes from one location in the heap to another
     52static int ByteCmp( char *, int, int, char *, int, int );       // compare 2 blocks of bytes
     53static char *VbyteAlloc( VbyteHeap &, int );                    // allocate a block bytes in the heap
     54static char *VbyteTryAdjustLast( VbyteHeap &, int );
     55
     56static void AddThisAfter( HandleNode &, HandleNode & );
     57static void DeleteNode( HandleNode & );
     58static void MoveThisAfter( HandleNode &, const HandleNode & );          // move current handle after parameter handle
    6059
    6160
    6261// Allocate the storage for the variable sized area and intialize the heap variables.
    6362
    64 static inline void ?{}( VbyteHeap & this, size_t Size ) with(this) {
     63static void ?{}( VbyteHeap & this, size_t Size ) with(this) {
    6564#ifdef VbyteDebug
    6665    serr | "enter:VbyteHeap::VbyteHeap, this:" | &this | " Size:" | Size;
     
    8180// Release the dynamically allocated storage for the byte area.
    8281
    83 static inline void ^?{}( VbyteHeap & this ) with(this) {
     82static void ^?{}( VbyteHeap & this ) with(this) {
    8483    free( StartVbyte );
    8584} // ~VbyteHeap
     
    9291// creator.
    9392
    94 static inline void ?{}( HandleNode & this ) with(this) {
     93static void ?{}( HandleNode & this ) with(this) {
    9594#ifdef VbyteDebug
    9695    serr | "enter:HandleNode::HandleNode, this:" | &this;
     
    107106// collection.
    108107
    109 static inline void ?{}( HandleNode & this, VbyteHeap & vh ) with(this) {
     108static void ?{}( HandleNode & this, VbyteHeap & vh ) with(this) {
    110109#ifdef VbyteDebug
    111110    serr | "enter:HandleNode::HandleNode, this:" | &this;
     
    124123// is the responsibility of the creator to destroy it.
    125124
    126 static inline void ^?{}( HandleNode & this ) with(this) {
     125static void ^?{}( HandleNode & this ) with(this) {
    127126#ifdef VbyteDebug
    128127    serr | "enter:HandleNode::~HandleNode, this:" | & this;
     
    237236}
    238237
    239 static inline void eagerCopyCtorHelper(string_res &s, const char* rhs, size_t rhslnth) with(s) {
     238static void eagerCopyCtorHelper(string_res &s, const char* rhs, size_t rhslnth) with(s) {
    240239    if( ambient_string_sharectx->activeHeap ) {
    241240        (Handle){ * ambient_string_sharectx->activeHeap };
     
    257256void ?{}(string_res &s, const char* rhs, size_t rhslnth) with(s) {
    258257    eagerCopyCtorHelper(s, rhs, rhslnth);
    259 }
    260 
    261 // String literal constructor
    262 void ?{}(string_res &s, const char* rhs) {
    263     (s){ rhs, strlen(rhs) };
    264258}
    265259
     
    490484}
    491485
    492 void ?=?(string_res &s, const char* other) {
    493     assign(s, other, strlen(other));
    494 }
    495 
    496486void ?=?(string_res &s, char other) {
    497487    assign(s, &other, 1);
     
    543533
    544534void append(string_res &str1, const char * buffer, size_t bsize) {
    545     size_t clnth = size(str1) + bsize;
    546     if ( str1.Handle.s + size(str1) == buffer ) { // already juxtapose ?
     535    size_t clnth = str1.Handle.lnth + bsize;
     536    if ( str1.Handle.s + str1.Handle.lnth == buffer ) { // already juxtapose ?
    547537        // no-op
    548538    } else {                                            // must copy some text
    549         if ( str1.Handle.s + size(str1) == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ?
     539        if ( str1.Handle.s + str1.Handle.lnth == VbyteAlloc(*str1.Handle.ulink, 0) ) { // str1 at end of string area ?
    550540            VbyteAlloc( *str1.Handle.ulink, bsize ); // create room for 2nd part at the end of string area
    551541        } else {                                        // copy the two parts
     
    553543            char * str1oldBuf = str1.Handle.s;  // must read after VbyteAlloc call in case it gs's
    554544            str1.Handle.s = str1newBuf;
    555             ByteCopy( str1.Handle.s, 0, str1.Handle.lnth, str1oldBuf, 0, str1.Handle.lnth);
     545            memcpy( str1.Handle.s, str1oldBuf,  str1.Handle.lnth );
    556546        } // if
    557         ByteCopy( str1.Handle.s, str1.Handle.lnth, bsize, (char*)buffer, 0, (int)bsize);
    558         //       VbyteHeap & this, char *Dst, int DstStart, int DstLnth, char *Src, int SrcStart, int SrcLnth
     547        memcpy( str1.Handle.s + str1.Handle.lnth, buffer, bsize );
    559548    } // if
    560549    str1.Handle.lnth = clnth;
     
    569558}
    570559
    571 void ?+=?(string_res &s, const char* other) {
    572     append( s, other, strlen(other) );
    573 }
    574560
    575561
     
    748734// Add a new HandleNode node n after the current HandleNode node.
    749735
    750 static inline void AddThisAfter( HandleNode & this, HandleNode & n ) with(this) {
     736static void AddThisAfter( HandleNode & this, HandleNode & n ) with(this) {
    751737#ifdef VbyteDebug
    752738    serr | "enter:AddThisAfter, this:" | &this | " n:" | &n;
     
    778764// Delete the current HandleNode node.
    779765
    780 static inline void DeleteNode( HandleNode & this ) with(this) {
     766static void DeleteNode( HandleNode & this ) with(this) {
    781767#ifdef VbyteDebug
    782768    serr | "enter:DeleteNode, this:" | &this;
     
    794780// allocation, the garbage collection routine is called.
    795781
    796 static inline char * VbyteAlloc( VbyteHeap & this, int size ) with(this) {
     782static char * __attribute__((noinline)) VbyteAlloc( VbyteHeap & this, int size ) with(this) {
    797783#ifdef VbyteDebug
    798784    serr | "enter:VbyteAlloc, size:" | size;
     
    804790    if ( NoBytes > ( uintptr_t )ExtVbyte ) {            // enough room for new byte-string ?
    805791                garbage( this, size );                                  // firer up the garbage collector
    806                 NoBytes = ( uintptr_t )EndVbyte + size;         // try again
    807                 if ( NoBytes > ( uintptr_t )ExtVbyte ) {        // enough room for new byte-string ?
    808             assert( 0 && "garbage run did not free up required space" );
    809                 } // if
     792                verify( (( uintptr_t )EndVbyte + size) <= ( uintptr_t )ExtVbyte  && "garbage run did not free up required space" );
    810793    } // if
    811794    r = EndVbyte;
     
    826809// VbyteAlloc to claim the new space, while doing optimal copying from old to new, then free old.
    827810
    828 static inline char * VbyteTryAdjustLast( VbyteHeap & this, int delta ) with(this) {
     811static char * VbyteTryAdjustLast( VbyteHeap & this, int delta ) with(this) {
    829812
    830813    if ( ( uintptr_t )EndVbyte + delta <= ( uintptr_t )ExtVbyte ) {
     
    848831// the address in the byte string area.
    849832
    850 static inline void MoveThisAfter( HandleNode & this, const HandleNode  & h ) with(this) {
     833static void MoveThisAfter( HandleNode & this, const HandleNode  & h ) with(this) {
    851834#ifdef VbyteDebug
    852835    serr | "enter:MoveThisAfter, this:" | & this | " h:" | & h;
  • libcfa/src/containers/string_res.hfa

    r2b30370 r4e8df745  
    1717
    1818#include <fstream.hfa>
     19#include <string.h>    // e.g. strlen
    1920
    2021   
     
    7273// Constructors, Assignment Operators, Destructor
    7374void ?{}(string_res &s); // empty string
    74 void ?{}(string_res &s, const char* initial); // copy from string literal (NULL-terminated)
    7575void ?{}(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer
     76static inline void ?{}(string_res &s, const char* rhs) { // copy from string literal (NULL-terminated)
     77    (s){ rhs, strlen(rhs) };
     78}
    7679
    7780void ?{}(string_res &s, const string_res & s2) = void;
     
    8588
    8689void assign(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer
    87 void ?=?(string_res &s, const char* other); // copy from string literal (NULL-terminated)
     90static inline void ?=?(string_res &s, const char* other) {  // copy from string literal (NULL-terminated)
     91    assign(s, other, strlen(other));
     92}
    8893void ?=?(string_res &s, const string_res &other);
    8994void ?=?(string_res &s, string_res &other);
     
    97102
    98103// Concatenation
     104void append(string_res &s, const char* buffer, size_t bsize);
    99105void ?+=?(string_res &s, char other); // append a character
    100106void ?+=?(string_res &s, const string_res &s2); // append-concatenate to first string
    101 void ?+=?(string_res &s, const char* other);
    102 void append(string_res &s, const char* buffer, size_t bsize);
     107static inline void ?+=?(string_res &s, const char* other) {
     108    append( s, other, strlen(other) );
     109}
    103110
    104111// Character access
Note: See TracChangeset for help on using the changeset viewer.