Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/MiniExcel.Core/Helpers/CellReferenceConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public static class CellReferenceConverter
private static readonly ConcurrentDictionary<int, string> IntMappingToAlphabet = new();
private static readonly ConcurrentDictionary<string, int> AlphabetMappingToInt = new();

// ConcurrentDictionary.Count takes every internal lock, too slow for a per-cell check.
// Entries are only added, contiguously from 0, so a cached count is safe to read.
private static volatile int _mappedCount;

static CellReferenceConverter()
{
EnsureMappingsUpTo(GeneralColumnIndex);
Expand All @@ -33,7 +37,7 @@ public static int GetNumericalIndex(string? columnName)

private static void EnsureMappingsUpTo(int columnIndex)
{
if (columnIndex < IntMappingToAlphabet.Count)
if (columnIndex < _mappedCount)
return;

if (columnIndex > MaxColumnNumber - 1)
Expand All @@ -46,6 +50,8 @@ private static void EnsureMappingsUpTo(int columnIndex)
IntMappingToAlphabet.TryAdd(i, name);
AlphabetMappingToInt.TryAdd(name, i);
}

_mappedCount = IntMappingToAlphabet.Count;
}

private static string GetColumnFromIndex(int value)
Expand Down
Loading