ICU-136 reduce UnicodeString footprint: remove fHashCode & reduce US_STACKBUF_SIZE to 7; size 32B

X-SVN-Rev: 1118
This commit is contained in:
Markus Scherer 2000-04-12 20:04:41 +00:00
parent 7c2d19d828
commit 9188757ce0
2 changed files with 19 additions and 40 deletions

View file

@ -1813,7 +1813,7 @@ private:
int32_t length);
// calculate hash code
int32_t doHashCode(void);
int32_t doHashCode(void) const;
// get pointer to start of array
inline UChar* getArrayStart(void);
@ -1876,7 +1876,13 @@ private:
// constants
enum {
US_STACKBUF_SIZE=9, // Size of stack buffer for small strings
#if UTF_SIZE==8
US_STACKBUF_SIZE=14, // Size of stack buffer for small strings
#elif UTF_SIZE==16
US_STACKBUF_SIZE=7, // Size of stack buffer for small strings
#else // UTF_SIZE==32
US_STACKBUF_SIZE=3, // Size of stack buffer for small strings
#endif
kInvalidUChar=0xffff, // invalid UChar index
kGrowSize=128, // grow size for this buffer
kInvalidHashCode=0, // invalid hash code
@ -1916,15 +1922,17 @@ private:
* as the first real field.
* The fields should be aligned such that no padding is
* necessary, mostly by having larger types first.
* On 32-bit machines, the size should be 40 bytes,
* on 64-bit machines (8-byte pointers), it should be 48 bytes.
* On 32-bit machines, the size should be 32 bytes,
* on 64-bit machines (8-byte pointers), it should be 40 bytes.
*/
// (implicit) *vtable;
UChar *fArray; // the Unicode data
int32_t fLength; // number characters in fArray
int32_t fCapacity; // sizeof fArray
int32_t fHashCode; // the hash code
uint16_t fFlags; // bit flags: see constants above
#if UTF_SIZE==32
uint16_t fPadding; // padding to align the fStackBuffer for UTF-32
#endif
UChar fStackBuffer [ US_STACKBUF_SIZE ]; // buffer for small strings
public:
@ -2480,8 +2488,7 @@ UnicodeString::length() const
inline int32_t
UnicodeString::hashCode() const
{ return (fHashCode == kInvalidHashCode
? ((UnicodeString*)this)->doHashCode() : fHashCode); }
{ return doHashCode(); }
//========================================
// Write alias methods

View file

@ -87,7 +87,6 @@ UnicodeString::UnicodeString()
: fArray(fStackBuffer),
fLength(0),
fCapacity(US_STACKBUF_SIZE),
fHashCode(kEmptyHashCode),
fFlags(kShortString)
{}
@ -95,7 +94,6 @@ UnicodeString::UnicodeString(int32_t capacity)
: fArray(0),
fLength(0),
fCapacity(US_STACKBUF_SIZE),
fHashCode(kEmptyHashCode),
fFlags(0)
{
allocate(capacity);
@ -105,7 +103,6 @@ UnicodeString::UnicodeString(UChar ch)
: fArray(fStackBuffer),
fLength(1),
fCapacity(US_STACKBUF_SIZE),
fHashCode(kInvalidHashCode),
fFlags(kShortString)
{
fStackBuffer[0] = ch;
@ -115,7 +112,6 @@ UnicodeString::UnicodeString(UChar32 ch, ESignature)
: fArray(fStackBuffer),
fLength(1),
fCapacity(US_STACKBUF_SIZE),
fHashCode(kInvalidHashCode),
fFlags(kShortString)
{
UTextOffset i = 0;
@ -127,7 +123,6 @@ UnicodeString::UnicodeString(const UChar *text)
: fArray(fStackBuffer),
fLength(0),
fCapacity(US_STACKBUF_SIZE),
fHashCode(kEmptyHashCode),
fFlags(kShortString)
{
doReplace(0, 0, text, 0, u_strlen(text));
@ -138,7 +133,6 @@ UnicodeString::UnicodeString(const UChar *text,
: fArray(fStackBuffer),
fLength(0),
fCapacity(US_STACKBUF_SIZE),
fHashCode(kEmptyHashCode),
fFlags(kShortString)
{
doReplace(0, 0, text, 0, textLength);
@ -150,7 +144,6 @@ UnicodeString::UnicodeString(bool_t isTerminated,
: fArray((UChar *)text),
fLength(textLength),
fCapacity(isTerminated ? textLength + 1 : textLength),
fHashCode(kInvalidHashCode),
fFlags(kReadonlyAlias)
{
if(text == 0 || textLength < -1 || textLength == -1 && !isTerminated) {
@ -168,7 +161,6 @@ UnicodeString::UnicodeString(UChar *buff,
: fArray(buff),
fLength(bufLength),
fCapacity(buffCapacity),
fHashCode(kInvalidHashCode),
fFlags(kWriteableAlias)
{
if(buff == 0 || bufLength < 0 || bufLength > buffCapacity) {
@ -181,7 +173,6 @@ UnicodeString::UnicodeString(const char *codepageData,
: fArray(fStackBuffer),
fLength(0),
fCapacity(US_STACKBUF_SIZE),
fHashCode(kEmptyHashCode),
fFlags(kShortString)
{
if(codepageData != 0) {
@ -196,7 +187,6 @@ UnicodeString::UnicodeString(const char *codepageData,
: fArray(fStackBuffer),
fLength(0),
fCapacity(US_STACKBUF_SIZE),
fHashCode(kEmptyHashCode),
fFlags(kShortString)
{
if(codepageData != 0) {
@ -209,7 +199,6 @@ UnicodeString::UnicodeString(const UnicodeString& that)
fArray(fStackBuffer),
fLength(0),
fCapacity(US_STACKBUF_SIZE),
fHashCode(kEmptyHashCode),
fFlags(kShortString)
{
*this = that;
@ -242,7 +231,6 @@ UnicodeString::allocate(int32_t capacity) {
} else {
fArray = 0;
fCapacity = 0;
fHashCode = kInvalidHashCode; // for constructor(capacity) to be correctly bogus
fFlags = kIsBogus;
return FALSE;
}
@ -280,7 +268,6 @@ UnicodeString::operator= (const UnicodeString& src)
// we always copy the length and the hash code
fLength = src.fLength;
fHashCode = src.fHashCode;
switch(src.fFlags) {
case kShortString:
@ -318,7 +305,6 @@ UnicodeString::operator= (const UnicodeString& src)
fArray = 0;
fLength = 0;
fCapacity = 0;
fHashCode = kInvalidHashCode;
fFlags = kIsBogus;
break;
}
@ -651,7 +637,6 @@ UnicodeString::setToBogus()
fArray = 0;
fCapacity = fLength = 0;
fHashCode = kInvalidHashCode;
fFlags = kIsBogus;
}
@ -678,7 +663,6 @@ UnicodeString::setTo(bool_t isTerminated,
}
fCapacity = isTerminated ? fLength + 1 : fLength;
fHashCode = kInvalidHashCode;
fFlags = kReadonlyAlias;
return *this;
}
@ -698,7 +682,6 @@ UnicodeString::setTo(UChar *buffer,
fArray = buffer;
fLength = buffLength;
fCapacity = buffCapacity;
fHashCode = kInvalidHashCode;
fFlags = kWriteableAlias;
return *this;
}
@ -715,7 +698,6 @@ UnicodeString::setCharAt(UTextOffset offset,
}
fArray[offset] = c;
fHashCode = kInvalidHashCode;
}
return *this;
}
@ -822,8 +804,6 @@ UnicodeString::toUpper(const Locale& locale)
}
}
fHashCode = kInvalidHashCode;
return *this;
}
@ -902,8 +882,6 @@ UnicodeString::toLower(const Locale& locale)
}
}
fHashCode = kInvalidHashCode;
return *this;
}
@ -939,7 +917,6 @@ UnicodeString::doReplace(UTextOffset start,
fArray = fStackBuffer;
fLength = 0;
fCapacity = US_STACKBUF_SIZE;
fHashCode = kEmptyHashCode;
fFlags = kShortString;
}
@ -986,7 +963,6 @@ UnicodeString::doReplace(UTextOffset start,
us_arrayCopy(srcChars, srcStart, getArrayStart(), start, srcLength);
fLength = newSize;
fHashCode = kInvalidHashCode;
// delayed delete in case srcChars == fArray when we started, and
// to keep oldArray alive for the above operations
@ -1027,8 +1003,6 @@ UnicodeString::doReverse(UTextOffset start,
*right = swap;
}
fHashCode = kInvalidHashCode;
return *this;
}
@ -1120,15 +1094,15 @@ UnicodeString::trim()
// Hashing
//========================================
int32_t
UnicodeString::doHashCode()
UnicodeString::doHashCode() const
{
/* Delegate hash computation to uhash. This makes UnicodeString
* hashing consistent with UChar* hashing. */
fHashCode = uhash_hashUCharsN(getArrayStart(), fLength);
if (fHashCode == kInvalidHashCode) {
fHashCode = kEmptyHashCode;
int32_t hashCode = uhash_hashUCharsN(getArrayStart(), fLength);
if (hashCode == kInvalidHashCode) {
hashCode = kEmptyHashCode;
}
return fHashCode;
return hashCode;
}
//========================================
@ -1244,8 +1218,6 @@ UnicodeString::doCodepageCreate(const char *codepageData,
return;
}
fHashCode = kInvalidHashCode;
// perform the conversion
if(converter == 0) {
// use the "invariant characters" conversion