Skip to content

Commit

Permalink
Small allocation optimization in StateTable.Builder.ToImmutable (#72364)
Browse files Browse the repository at this point in the history
Small performance optimization around in StateTable.Builder.ToImmutable to no longer allocate an array
  • Loading branch information
ToddGrun authored Mar 21, 2024
1 parent a66551a commit 9baba7e
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.Collections;

namespace Microsoft.CodeAnalysis
Expand Down Expand Up @@ -51,10 +47,13 @@ public sealed class Builder
public StateTableStore ToImmutable()
{
// we can cache the tables at this point, as we'll no longer be using them to determine current state
var keys = _tableBuilder.Keys.ToArray();
foreach (var key in keys)
foreach (var kvp in _tableBuilder)
{
_tableBuilder[key] = _tableBuilder[key].AsCached();
var cachedValue = kvp.Value.AsCached();
if (cachedValue != kvp.Value)
{
SegmentedCollectionsMarshal.GetValueRefOrNullRef(_tableBuilder, kvp.Key) = cachedValue;
}
}

return new StateTableStore(_tableBuilder.ToImmutable());
Expand Down

0 comments on commit 9baba7e

Please sign in to comment.