Changeset d8d512e


Ignore:
Timestamp:
Sep 8, 2021, 4:40:08 PM (3 years ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
5a40e4e
Parents:
f26923e
Message:

Reorganizing string constructor/assignment overload calls for better performance.

Location:
libcfa/src/containers
Files:
2 edited

Legend:

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

    rf26923e rd8d512e  
    223223}
    224224
    225 void ?=?(string_res &s, const char* other) {
    226     string_res sother = other;
    227     const string_res & sother_ref = sother;
    228     s = sother_ref;  // `s = sother` calls autogen ?=?
    229 }
    230 
    231 void ?=?(string_res &s, char other) {
    232     char otherCstr[2] = {other, 0};
    233     s = otherCstr;
    234 }
    235 
    236 // Copy assignment operator
    237 void ?=?(string_res & this, const string_res & rhs) with( this ) {
     225void assign(string_res &this, const char* buffer, size_t bsize) {
    238226
    239227    char * afterBegin = this.Handle.s + this.Handle.lnth;
     
    251239
    252240    string_res pasting = { beforeBegin, beforeLen };
    253     pasting += rhs;
     241    append(pasting, buffer, bsize);
    254242    string_res after = { afterBegin, afterLen }; // juxtaposed with in-progress pasting
    255243    pasting += after;                        // optimized case
     
    258246
    259247    this.Handle.s = pasting.Handle.s + beforeLen;
    260     this.Handle.lnth = rhs.Handle.lnth;
     248    this.Handle.lnth = bsize;
    261249    MoveThisAfter( this.Handle, pasting.Handle );
    262250
     
    314302}
    315303
     304void ?=?(string_res &s, const char* other) {
     305    assign(s, other, strlen(other));
     306}
     307
     308void ?=?(string_res &s, char other) {
     309    assign(s, &other, 1);
     310}
     311
     312// Copy assignment operator
     313void ?=?(string_res & this, const string_res & rhs) with( this ) {
     314    assign(this, rhs.Handle.s, rhs.Handle.lnth);
     315}
     316
    316317void ?=?(string_res & this, string_res & rhs) with( this ) {
    317318    const string_res & rhs2 = rhs;
     
    340341}
    341342
     343
    342344///////////////////////////////////////////////////////////////////
    343 // Slice-Concatenate helper
    344 
    345 void append(string_res &str1, const string_res & str_src, size_t start, size_t end) {
    346     size_t clnth = size(str1) + end - start;
    347     if ( str1.Handle.s + size(str1) == str_src.Handle.s && start == 0) { // already juxtapose ?
     345// Concatenation
     346
     347void append(string_res &str1, const char * buffer, size_t bsize) {
     348    size_t clnth = size(str1) + bsize;
     349    if ( str1.Handle.s + size(str1) == buffer ) { // already juxtapose ?
     350        // no-op
    348351    } else {                                            // must copy some text
    349352        if ( str1.Handle.s + size(str1) == VbyteAlloc(HeapArea, 0) ) { // str1 at end of string area ?
    350             VbyteAlloc(HeapArea, end - start); // create room for 2nd part at the end of string area
     353            VbyteAlloc(HeapArea, bsize); // create room for 2nd part at the end of string area
    351354        } else {                                        // copy the two parts
    352355            char * str1oldBuf = str1.Handle.s;
     
    354357            ByteCopy( HeapArea, str1.Handle.s, 0, str1.Handle.lnth, str1oldBuf, 0, str1.Handle.lnth);
    355358        } // if
    356         ByteCopy( HeapArea, str1.Handle.s, str1.Handle.lnth, str_src.Handle.lnth, str_src.Handle.s, start, end);
     359        ByteCopy( HeapArea, str1.Handle.s, str1.Handle.lnth, bsize, (char*)buffer, 0, (int)bsize);
    357360        //       VbyteHeap & this, char *Dst, int DstStart, int DstLnth, char *Src, int SrcStart, int SrcLnth
    358361    } // if
     
    360363}
    361364
    362 
    363 
    364 ///////////////////////////////////////////////////////////////////
    365 // Concatenation
    366 
    367365void ?+=?(string_res &str1, const string_res &str2) {
    368     append( str1, str2, 0, size(str2) );
     366    append( str1, str2.Handle.s, str2.Handle.lnth );
    369367}
    370368
    371369void ?+=?(string_res &s, char other) {
    372     string_res other_s = { &other, 1 };
    373     s += other_s;
     370    append( s, &other, 1 );
    374371}
    375372
    376373void ?+=?(string_res &s, const char* other) {
    377     string_res other_s = other;
    378     s += other_s;
     374    append( s, other, strlen(other) );
    379375}
    380376
  • libcfa/src/containers/string_res.hfa

    rf26923e rd8d512e  
    8282}
    8383
    84 void ?=?(string_res &s, const char* other); // copy assignment from literal
     84void assign(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer
     85void ?=?(string_res &s, const char* other); // copy from string literal (NULL-terminated)
    8586void ?=?(string_res &s, const string_res &other);
    8687void ?=?(string_res &s, string_res &other);
    87 void ?=?(string_res &s, char other);  // Str tolerates memcpys; still saw calls to autogen
     88void ?=?(string_res &s, char other);
    8889
    8990void ^?{}(string_res &s);
     
    9697void ?+=?(string_res &s, char other); // append a character
    9798void ?+=?(string_res &s, const string_res &s2); // append-concatenate to first string
    98 void ?+=?(string_res &s, const char* other); // append-concatenate to first string
     99void ?+=?(string_res &s, const char* other);
     100void append(string_res &s, const char* buffer, size_t bsize);
    99101
    100102// Character access
Note: See TracChangeset for help on using the changeset viewer.