Avoid memory allocation in NSString -hash.

Use the incremental hash function for long strings.
This commit is contained in:
David Chisnall 2018-04-08 14:45:25 +01:00 committed by Richard Frith-Macdonald
parent 7274cbaa55
commit 847c1a54b7

View file

@ -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.