mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 00:41:02 +00:00
Avoid memory allocation in NSString -hash.
Use the incremental hash function for long strings.
This commit is contained in:
parent
7274cbaa55
commit
847c1a54b7
1 changed files with 12 additions and 7 deletions
|
@ -2944,17 +2944,22 @@ GSICUCollatorOpen(NSStringCompareOptions mask, NSLocale *locale)
|
|||
|
||||
if (len > 0)
|
||||
{
|
||||
unichar buf[64];
|
||||
unichar *ptr = (len <= 64) ? buf :
|
||||
NSZoneMalloc(NSDefaultMallocZone(), len * sizeof(unichar));
|
||||
static const int buf_size = 64;
|
||||
unichar buf[buf_size];
|
||||
int idx = 0;
|
||||
uint32_t s0 = 0;
|
||||
uint32_t s1 = 0;
|
||||
|
||||
[self getCharacters: ptr range: NSMakeRange(0,len)];
|
||||
ret = GSPrivateHash(0, (const void*)ptr, len * sizeof(unichar));
|
||||
if (ptr != buf)
|
||||
while (idx < len)
|
||||
{
|
||||
NSZoneFree(NSDefaultMallocZone(), ptr);
|
||||
int l = MIN(len-idx, buf_size);
|
||||
[self getCharacters: buf range: NSMakeRange(idx,l)];
|
||||
GSPrivateIncrementalHash(&s0, &s1, buf, l * sizeof(unichar));
|
||||
idx += l;
|
||||
}
|
||||
|
||||
ret = GSPrivateFinishHash(s0, s1, len * sizeof(unichar));
|
||||
|
||||
/*
|
||||
* The hash caching in our concrete string classes uses zero to denote
|
||||
* an empty cache value, so we MUST NOT return a hash of zero.
|
||||
|
|
Loading…
Reference in a new issue