Skip to content

Commit

Permalink
Remove an unnecessary use of ImmutableArray<T> in DataCenterSegmented…
Browse files Browse the repository at this point in the history
…SimpleRegion.
  • Loading branch information
alexrp committed Nov 23, 2022
1 parent 3a3c06d commit f361a14
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ namespace Vezel.Novadrop.Data.Serialization.Regions;
sealed class DataCenterSegmentedSimpleRegion<T>
where T : unmanaged, IDataCenterItem
{
public ImmutableArray<DataCenterSimpleRegion<T>> Segments { get; }
public IReadOnlyList<DataCenterSimpleRegion<T>> Segments { get; }

public DataCenterSegmentedSimpleRegion(int count)
{
var segs = ImmutableArray.CreateBuilder<DataCenterSimpleRegion<T>>(count);
var segs = new List<DataCenterSimpleRegion<T>>(count);

for (var i = 0; i < count; i++)
segs.Add(new DataCenterSimpleRegion<T>(false));

Segments = segs.ToImmutable();
Segments = segs;
}

public async ValueTask ReadAsync(StreamBinaryReader reader, CancellationToken cancellationToken)
Expand All @@ -31,10 +31,10 @@ public async ValueTask WriteAsync(StreamBinaryWriter writer, CancellationToken c

public T GetElement(DataCenterAddress address)
{
return address.SegmentIndex < Segments.Length
return address.SegmentIndex < Segments.Count
? Segments[address.SegmentIndex].GetElement(address.ElementIndex)
: throw new InvalidDataException(
$"Region segment index {address.SegmentIndex} is out of bounds (0..{Segments.Length}).");
$"Region segment index {address.SegmentIndex} is out of bounds (0..{Segments.Count}).");
}

public void SetElement(DataCenterAddress address, T value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public async ValueTask ReadAsync(bool strict, StreamBinaryReader reader, Cancell

last = hash;

var bucket = (hash ^ hash >> 16) % (uint)_strings.Segments.Length;
var bucket = (hash ^ hash >> 16) % (uint)_strings.Segments.Count;

if (i != bucket)
throw new InvalidDataException($"String bucket {i} does not match expected bucket {bucket}.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public DataCenterRawString AddString(string value)
Address = addr,
};

_strings.Segments[(int)((hash ^ hash >> 16) % (uint)_strings.Segments.Length)].Elements.Add(raw);
_strings.Segments[(int)((hash ^ hash >> 16) % (uint)_strings.Segments.Count)].Elements.Add(raw);

_cache.Add(value, raw);
}
Expand Down

0 comments on commit f361a14

Please sign in to comment.