Core: String middle tier optimised (some routines moved to allocator to inline MemoryAlloc32 / MemoryFree32)

git-svn-id: svn://ultimatepp.org/upp/trunk@15553 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2020-11-30 11:21:43 +00:00
parent b9c3271c79
commit bbb9fcad46
4 changed files with 129 additions and 110 deletions

View file

@ -51,6 +51,7 @@ file
String.h,
AString.hpp,
StringFind.cpp,
StringMem.i highlight cpp,
String.cpp,
WString.cpp,
StrUtil.cpp,

View file

@ -13,33 +13,14 @@ void String0::Dsyn()
String0::Rc String0::voidptr[2];
void String0::LSet(const String0& s)
{
w[2] = s.w[2];
w[3] = s.w[3];
if(s.IsRef()) {
ptr = s.ptr;
if(ptr != (char *)(voidptr + 1))
AtomicInc(s.Ref()->refcount);
}
else {
ptr = (char *)MemoryAlloc32();
memcpy(qptr, s.qptr, 32); // optimizes to movups
}
}
#ifndef UPP_HEAP
void String0::LFree()
{
if(IsRef()) {
if(ptr != (char *)(voidptr + 1)) {
Rc *rc = Ref();
ASSERT(rc->refcount > 0);
if(AtomicDec(rc->refcount) == 0) MemoryFree(rc);
}
}
else
MemoryFree32(ptr);
}
inline void *MemoryAlloc32_i() { return new byte[32]; }
inline void MemoryFree32_i(void *ptr) { delete[] (byte *)ptr; }
#include "StringMem.i"
#endif
bool String0::LEq(const String0& s) const
{
@ -78,20 +59,6 @@ int String0::CompareL(const String0& s) const
return q ? q : SgnCompare(la, lb);
}
char *String0::Alloc(int count, char& kind)
{
if(count < 32) {
kind = MEDIUM;
return (char *)MemoryAlloc32();
}
size_t sz = sizeof(Rc) + count + 1;
Rc *rc = (Rc *)MemoryAllocSz(sz);
rc->alloc = count == INT_MAX ? INT_MAX : (int)sz - sizeof(Rc) - 1;
rc->refcount = 1;
kind = min(rc->alloc, 255);
return rc->GetPtr();
}
char *String0::Insert(int pos, int count, const char *s)
{
ASSERT(pos >= 0 && count >= 0 && pos <= GetCount());
@ -186,29 +153,6 @@ void String0::Trim(int pos)
Dsyn();
}
void String0::LCat(int c)
{
if(IsSmall()) {
qword *x = (qword *)MemoryAlloc32();
x[0] = q[0];
x[1] = q[1];
LLen() = SLen();
SLen() = 15;
chr[KIND] = MEDIUM;
qptr = x;
}
int l = LLen();
if(IsRef() ? !IsShared() && l < (int)Ref()->alloc : l < 31) {
ptr[l] = c;
ptr[LLen() = l + 1] = 0;
}
else {
char *s = Insert(l, 1, NULL);
s[0] = c;
s[1] = 0;
}
}
void String0::Cat(const char *s, int len)
{
if(IsSmall()) {
@ -327,33 +271,6 @@ String::String(StringBuffer& b)
// DLOG(sprintf(h, "String(StringBuffer) end2 %p (%p)", ptr, this));
}
char *StringBuffer::Alloc(int count, int& alloc)
{
if(count <= 31) {
char *s = (char *)MemoryAlloc32();
alloc = 31;
return s;
}
else {
size_t sz = sizeof(Rc) + count + 1;
Rc *rc = (Rc *)MemoryAlloc(sz);
alloc = rc->alloc = (int)min((size_t)INT_MAX, sz - sizeof(Rc) - 1);
rc->refcount = 1;
return (char *)(rc + 1);
}
}
void StringBuffer::Free()
{
if(pbegin == buffer)
return;
int all = (int)(limit - pbegin);
if(all == 31)
MemoryFree32(pbegin);
if(all > 31)
MemoryFree((Rc *)pbegin - 1);
}
void StringBuffer::Realloc(dword n, const char *cat, int l)
{
int al;
@ -416,24 +333,6 @@ void StringBuffer::ReallocL(const char *s, int l)
Realloc(max(GetLength(), l) + GetLength(), s, l);
}
void StringBuffer::Set(String& s)
{
s.UnShare();
int l = s.GetLength();
if(s.GetAlloc() == 14) {
pbegin = (char *)MemoryAlloc32();
limit = pbegin + 31;
memcpy8(pbegin, s.Begin(), l);
pend = pbegin + l;
}
else {
pbegin = s.ptr;
pend = pbegin + l;
limit = pbegin + s.GetAlloc();
}
s.Zero();
}
String TrimLeft(const String& str)
{
const char *s = str;

111
uppsrc/Core/StringMem.i Normal file
View file

@ -0,0 +1,111 @@
// These are String methods which are best inlined in heap allocator
void String0::LSet(const String0& s)
{
w[2] = s.w[2];
w[3] = s.w[3];
if(s.IsRef()) {
ptr = s.ptr;
if(ptr != (char *)(voidptr + 1))
AtomicInc(s.Ref()->refcount);
}
else {
ptr = (char *)MemoryAlloc32_i();
memcpy(qptr, s.qptr, 32); // optimizes to movups
}
}
void String0::LFree()
{
if(IsRef()) {
if(ptr != (char *)(voidptr + 1)) {
Rc *rc = Ref();
ASSERT(rc->refcount > 0);
if(AtomicDec(rc->refcount) == 0) MemoryFree(rc);
}
}
else
MemoryFree32_i(ptr);
}
char *String0::Alloc(int count, char& kind)
{
if(count < 32) {
kind = MEDIUM;
return (char *)MemoryAlloc32_i();
}
size_t sz = sizeof(Rc) + count + 1;
Rc *rc = (Rc *)MemoryAllocSz(sz);
rc->alloc = count == INT_MAX ? INT_MAX : (int)sz - sizeof(Rc) - 1;
rc->refcount = 1;
kind = min(rc->alloc, 255);
return rc->GetPtr();
}
void String0::LCat(int c)
{
if(IsSmall()) {
qword *x = (qword *)MemoryAlloc32_i();
x[0] = q[0];
x[1] = q[1];
LLen() = SLen();
SLen() = 15;
chr[KIND] = MEDIUM;
qptr = x;
}
int l = LLen();
if(IsRef() ? !IsShared() && l < (int)Ref()->alloc : l < 31) {
ptr[l] = c;
ptr[LLen() = l + 1] = 0;
}
else {
char *s = Insert(l, 1, NULL);
s[0] = c;
s[1] = 0;
}
}
char *StringBuffer::Alloc(int count, int& alloc)
{
if(count <= 31) {
char *s = (char *)MemoryAlloc32_i();
alloc = 31;
return s;
}
else {
size_t sz = sizeof(Rc) + count + 1;
Rc *rc = (Rc *)MemoryAlloc(sz);
alloc = rc->alloc = (int)min((size_t)INT_MAX, sz - sizeof(Rc) - 1);
rc->refcount = 1;
return (char *)(rc + 1);
}
}
void StringBuffer::Set(String& s)
{
s.UnShare();
int l = s.GetLength();
if(s.GetAlloc() == 14) {
pbegin = (char *)MemoryAlloc32_i();
limit = pbegin + 31;
memcpy8(pbegin, s.Begin(), l);
pend = pbegin + l;
}
else {
pbegin = s.ptr;
pend = pbegin + l;
limit = pbegin + s.GetAlloc();
}
s.Zero();
}
void StringBuffer::Free()
{
if(pbegin == buffer)
return;
int all = (int)(limit - pbegin);
if(all == 31)
MemoryFree32_i(pbegin);
if(all > 31)
MemoryFree((Rc *)pbegin - 1);
}

View file

@ -575,7 +575,8 @@ void *MemoryAlloc32_2()
return LogAlloc(MakeHeap()->Alloc32(), 32);
}
void *MemoryAlloc32()
force_inline
void *MemoryAlloc32_i()
{
LTIMING("MemoryAlloc32");
Heap *heap = heap_tls__;
@ -584,6 +585,8 @@ void *MemoryAlloc32()
return MemoryAlloc32_2();
}
void *MemoryAlloc32() { return MemoryAlloc32_i(); }
never_inline
void MemoryFree32_2(void *ptr)
{
@ -591,7 +594,8 @@ void MemoryFree32_2(void *ptr)
MakeHeap()->Free32(ptr);
}
void MemoryFree32(void *ptr)
force_inline
void MemoryFree32_i(void *ptr)
{
LTIMING("MemoryFree32");
LogFree(ptr);
@ -602,6 +606,8 @@ void MemoryFree32(void *ptr)
MemoryFree32_2(ptr);
}
void MemoryFree32(void *ptr) { MemoryFree32_i(ptr); }
void *MemoryAlloc48()
{
LTIMING("MemoryAlloc48");
@ -656,6 +662,8 @@ void MemoryDumpHuge()
CurrentHeap()->DumpHuge();
}
#include "StringMem.i"
#endif
}