Skip to content

Commit

Permalink
Fixes issue #10
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmueller committed Jan 24, 2019
1 parent 79fce74 commit 2273b28
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
8 changes: 4 additions & 4 deletions Collections.Pooled/Collections.Pooled.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<PackageProjectUrl>https://github.com/jtmueller/Collections.Pooled</PackageProjectUrl>
<RepositoryUrl>https://github.com/jtmueller/Collections.Pooled.git</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<AssemblyVersion>1.0.4.0</AssemblyVersion>
<FileVersion>1.0.4.0</FileVersion>
<Version>1.0.4</Version>
<PackageReleaseNotes>Using ThrowHelper for improved performance</PackageReleaseNotes>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<Version>1.0.0</Version>
<PackageReleaseNotes>Bug fix in a PooledDictionary performance optimization</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
41 changes: 24 additions & 17 deletions Collections.Pooled/PooledDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,14 +599,13 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior)
}

var entries = _entries;
var buckets = _buckets;
var comparer = _comparer;
var size = _size;

int hashCode = ((comparer == null) ? key.GetHashCode() : comparer.GetHashCode(key)) & 0x7FFFFFFF;

int collisionCount = 0;
ref int bucket = ref buckets[hashCode % size];
ref int bucket = ref _buckets[hashCode % size];
// Value in _buckets is 1-based
int i = bucket - 1;

Expand Down Expand Up @@ -749,9 +748,7 @@ private bool TryInsert(TKey key, TValue value, InsertionBehavior behavior)
{
Resize();
size = _size;
buckets = _buckets;
entries = _entries;
bucket = ref buckets[hashCode % size];
bucket = ref _buckets[hashCode % size];
}
index = count;
_count = count + 1;
Expand Down Expand Up @@ -839,20 +836,27 @@ private void Resize(int newSize, bool forceNewHashCodes)
Debug.Assert(!forceNewHashCodes || default(TKey) == null);
Debug.Assert(newSize >= _size);

int[] buckets;
Entry[] entries;
bool replaceArrays;
int count = _count;

// Because ArrayPool might give us larger arrays than we asked for, see if we can
// use the existing capacity without actually resizing.
if (_buckets.Length >= newSize && _entries.Length >= newSize)
{
_size = newSize;
return;
buckets = _buckets;
entries = _entries;
replaceArrays = false;
}
else
{
buckets = s_bucketPool.Rent(newSize);
Array.Clear(buckets, 0, buckets.Length);
entries = s_entryPool.Rent(newSize);
Array.Copy(_entries, 0, entries, 0, count);
replaceArrays = true;
}

int[] buckets = s_bucketPool.Rent(newSize);
Array.Clear(buckets, 0, buckets.Length);
Entry[] entries = s_entryPool.Rent(newSize);

int count = _count;
Array.Copy(_entries, 0, entries, 0, count);

if (default(TKey) == null && forceNewHashCodes)
{
Expand All @@ -878,9 +882,12 @@ private void Resize(int newSize, bool forceNewHashCodes)
}
}

ReturnArrays();
_buckets = buckets;
_entries = entries;
if (replaceArrays)
{
ReturnArrays();
_buckets = buckets;
_entries = entries;
}
_size = newSize;
}

Expand Down

0 comments on commit 2273b28

Please sign in to comment.