Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Modification of ketmar's patch to fix AA byKey/byValue to avoid perfo…
Browse files Browse the repository at this point in the history
…rmance hits for normal AA calls.

Fixes: https://issues.dlang.org/show_bug.cgi?id=13410
  • Loading branch information
schveiguy committed Oct 1, 2014
1 parent 6e929bc commit 50a79f1
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/rt/aaA.d
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ struct Impl
{
Entry*[] buckets;
size_t nodes; // total number of entries
size_t firstUsedBucket; // starting index for first used bucket.
TypeInfo _keyti;
Entry*[4] binit; // initial value of buckets[]

Expand Down Expand Up @@ -200,6 +201,10 @@ body
//printf("rehash\n");
_aaRehash(aa,keyti);
}
else
{
if (i < aa.impl.firstUsedBucket) aa.impl.firstUsedBucket = i;
}
}

Lret:
Expand Down Expand Up @@ -398,6 +403,7 @@ body

newImpl.nodes = oldImpl.nodes;
newImpl._keyti = oldImpl._keyti;
newImpl.firstUsedBucket = 0; // cache is not initialized yet

*paa.impl = newImpl;
}
Expand All @@ -406,6 +412,7 @@ body
if (paa.impl.buckets.ptr != paa.impl.binit.ptr)
GC.free(paa.impl.buckets.ptr);
paa.impl.buckets = paa.impl.binit[];
paa.impl.firstUsedBucket = 0; // cache is not initialized yet
}
}
return (*paa).impl;
Expand Down Expand Up @@ -586,6 +593,7 @@ Impl* _d_assocarrayliteralTX(const TypeInfo_AssociativeArray ti, void[] keys, vo
}
auto len = prime_list[i];
result.buckets = newBuckets(len);
result.firstUsedBucket = size_t.max-1;

size_t keytsize = aligntsize(keysize);

Expand All @@ -597,6 +605,7 @@ Impl* _d_assocarrayliteralTX(const TypeInfo_AssociativeArray ti, void[] keys, vo
auto key_hash = keyti.getHash(pkey);
//printf("hash = %d\n", key_hash);
i = key_hash % len;
if (i < result.firstUsedBucket) result.firstUsedBucket = i;
auto pe = &result.buckets[i];
while (1)
{
Expand Down Expand Up @@ -864,11 +873,13 @@ Range _aaRange(AA aa) pure nothrow @nogc
return res;

res.impl = aa.impl;
foreach (entry; aa.impl.buckets)
foreach (i; aa.impl.firstUsedBucket .. aa.impl.buckets.length )
{
auto entry = aa.impl.buckets[i];
if (entry !is null)
{
res.current = entry;
aa.impl.firstUsedBucket = i; // update cache
break;
}
}
Expand Down

0 comments on commit 50a79f1

Please sign in to comment.