Changeset 08ed947 for libcfa/src/containers/string_res.cfa
- Timestamp:
- Feb 23, 2022, 6:13:02 PM (2 years ago)
- Branches:
- ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
- Children:
- afe9e45, c5af4f9
- Parents:
- cc7bbe6
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/containers/string_res.cfa
rcc7bbe6 r08ed947 16 16 #include "string_res.hfa" 17 17 #include "string_sharectx.hfa" 18 19 #include <stdlib.hfa> // e.g. malloc 18 #include "stdlib.hfa" 19 20 // Workaround for observed performance penalty from calling CFA's alloc. 21 // Workaround is: EndVbyte = TEMP_ALLOC(char, CurrSize) 22 // Should be: EndVbyte = alloc(CurrSize) 23 #define TEMP_ALLOC(T, n) (( T* ) malloc( n * sizeof( T ) )) 24 20 25 #include <assert.h> 21 26 … … 67 72 NoOfCompactions = NoOfExtensions = NoOfReductions = 0; 68 73 InitSize = CurrSize = Size; 69 StartVbyte = EndVbyte = alloc(CurrSize);74 StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize); 70 75 ExtVbyte = (void *)( StartVbyte + CurrSize ); 71 76 Header.flink = Header.blink = &Header; … … 246 251 Handle.s = VbyteAlloc(*Handle.ulink, rhslnth); 247 252 Handle.lnth = rhslnth; 248 for ( int i = 0; i < rhslnth; i += 1 ) { // copy characters 249 Handle.s[i] = rhs[i]; 250 } // for 253 memmove( Handle.s, rhs, rhslnth ); 251 254 s.shareEditSet_prev = &s; 252 255 s.shareEditSet_next = &s; … … 264 267 Handle.lnth = rhslnth; 265 268 (s.shareEditSet_owns_ulink){ false }; 266 for ( int i = 0; i < rhslnth; i += 1 ) { // copy characters 267 Handle.s[i] = rhs[i]; 268 } // for 269 memmove( Handle.s, rhs, rhslnth ); 269 270 s.shareEditSet_prev = &s; 270 271 s.shareEditSet_next = &s; … … 595 596 596 597 int find(const string_res &s, char search) { 597 for (i; size(s)) { 598 if (s[i] == search) return i; 599 } 600 return size(s); 601 } 598 return findFrom(s, 0, search); 599 } 600 601 int findFrom(const string_res &s, size_t fromPos, char search) { 602 // FIXME: This paricular overload (find of single char) is optimized to use memchr. 603 // The general overload (find of string, memchr applying to its first character) and `contains` should be adjusted to match. 604 char * searchFrom = s.Handle.s + fromPos; 605 size_t searchLnth = s.Handle.lnth - fromPos; 606 int searchVal = search; 607 char * foundAt = (char *) memchr(searchFrom, searchVal, searchLnth); 608 if (foundAt == 0p) return s.Handle.lnth; 609 else return foundAt - s.Handle.s; 610 } 611 612 int find(const string_res &s, const string_res &search) { 613 return findFrom(s, 0, search); 614 } 615 616 int findFrom(const string_res &s, size_t fromPos, const string_res &search) { 617 return findFrom(s, fromPos, search.Handle.s, search.Handle.lnth); 618 } 619 620 int find(const string_res &s, const char* search) { 621 return findFrom(s, 0, search); 622 } 623 int findFrom(const string_res &s, size_t fromPos, const char* search) { 624 return findFrom(s, fromPos, search, strlen(search)); 625 } 626 627 int find(const string_res &s, const char* search, size_t searchsize) { 628 return findFrom(s, 0, search, searchsize); 629 } 630 631 int findFrom(const string_res &s, size_t fromPos, const char* search, size_t searchsize) { 602 632 603 633 /* Remaining implementations essentially ported from Sunjay's work */ 604 634 605 int find(const string_res &s, const string_res &search) { 606 return find(s, search.Handle.s, search.Handle.lnth); 607 } 608 609 int find(const string_res &s, const char* search) { 610 return find(s, search, strlen(search)); 611 } 612 613 int find(const string_res &s, const char* search, size_t searchsize) { 635 614 636 // FIXME: This is a naive algorithm. We probably want to switch to someting 615 637 // like Boyer-Moore in the future. … … 621 643 } 622 644 623 for (size_t i = 0; i < s.Handle.lnth; i++) {645 for (size_t i = fromPos; i < s.Handle.lnth; i++) { 624 646 size_t remaining = s.Handle.lnth - i; 625 647 // Never going to find the search string if the remaining string is … … 740 762 serr | "enter:AddThisAfter, this:" | &this | " n:" | &n; 741 763 #endif // VbyteDebug 764 // Performance note: we are on the critical path here. MB has ensured that the verifies don't contribute to runtime (are compiled away, like they're supposed to be). 742 765 verify( n.ulink != 0p ); 743 766 verify( this.ulink == n.ulink ); … … 823 846 NoOfExtensions += 1; 824 847 CurrSize *= 2; 825 StartVbyte = EndVbyte = alloc(CurrSize);848 StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize); 826 849 ExtVbyte = StartVbyte + CurrSize; 827 850 … … 959 982 960 983 984 static double heap_expansion_freespace_threshold = 0.1; // default inherited from prior work: expand heap when less than 10% "free" (i.e. garbage) 985 // probably an unreasonable default, but need to assess early-round tests on changing it 986 987 void TUNING_set_string_heap_liveness_threshold( double val ) { 988 heap_expansion_freespace_threshold = 1.0 - val; 989 } 990 991 961 992 // Garbage determines the amount of free space left in the heap and then reduces, leave the same, or extends the size of 962 993 // the heap. The heap is then compacted in the existing heap or into the newly allocated heap. … … 986 1017 AmountFree = ( uintptr_t )ExtVbyte - ( uintptr_t )StartVbyte - AmountUsed; 987 1018 988 if ( ( double ) AmountFree < ( CurrSize * 0.1 ) || AmountFree < minreq ) { // free space less than 10%or not enough to serve cur request1019 if ( ( double ) AmountFree < ( CurrSize * heap_expansion_freespace_threshold ) || AmountFree < minreq ) { // free space less than threshold or not enough to serve cur request 989 1020 990 1021 extend( this, max( CurrSize, minreq ) ); // extend the heap … … 1033 1064 1034 1065 CurrSize += size > InitSize ? size : InitSize; // minimum extension, initial size 1035 StartVbyte = EndVbyte = alloc(CurrSize);1066 StartVbyte = EndVbyte = TEMP_ALLOC(char, CurrSize); 1036 1067 ExtVbyte = (void *)( StartVbyte + CurrSize ); 1037 1068 compaction(this); // copy from old heap to new & adjust pointers to new heap
Note: See TracChangeset
for help on using the changeset viewer.