Skip to content

Commit 40d2baa

Browse files
virzakmichelebastione
authored andcommitted
Avoid per-cell allocations in XmlReaderHelper and TryParseCellReference
IsStartElement and GetAttribute allocated a LINQ closure on every call, and TryParseCellReference allocated a substring for every row number.
1 parent e68503c commit 40d2baa

3 files changed

Lines changed: 47 additions & 7 deletions

File tree

‎src/MiniExcel.Core/Helpers/CellReferenceConverter.cs‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,21 @@ public static bool TryParseCellReference(string? value, out int column, out int
118118
return false;
119119
}
120120

121-
if (position == 0)
121+
if (position == 0 || position == value.Length)
122122
return false;
123123

124-
return int.TryParse(value[position..], NumberStyles.None, CultureInfo.InvariantCulture, out row)
125-
&& row is > 0 and <= MaxRowNumber;
124+
// Parsed by hand rather than with int.TryParse on a substring, which allocated for every cell
125+
for (; position < value.Length; position++)
126+
{
127+
var digit = value[position] - '0';
128+
row = row * 10 + digit;
129+
if (digit is < 0 or > 9 || row > MaxRowNumber)
130+
{
131+
row = 0;
132+
return false;
133+
}
134+
}
135+
136+
return row > 0;
126137
}
127138
}

‎src/MiniExcel.OpenXml/Utils/XmlReaderHelper.cs‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,27 @@ public static async Task SkipToNextSiblingAsync(this XmlReader reader, Cancellat
6767
}
6868
}
6969

70+
// Called for every element and cell, so these loop instead of allocating a LINQ closure per call
7071
public static bool IsStartElement(this XmlReader reader, string name, params string[] nss)
7172
{
72-
return nss.Any(s => reader.IsStartElement(name, s));
73+
foreach (var ns in nss)
74+
{
75+
if (reader.IsStartElement(name, ns))
76+
return true;
77+
}
78+
79+
return false;
7380
}
7481

7582
public static string? GetAttribute(this XmlReader reader, string name, params string[] nss)
7683
{
77-
return nss
78-
.Select(ns => reader.GetAttribute(name, ns))
79-
.FirstOrDefault(at => at is not null);
84+
foreach (var ns in nss)
85+
{
86+
if (reader.GetAttribute(name, ns) is { } attribute)
87+
return attribute;
88+
}
89+
90+
return null;
8091
}
8192

8293
// Copied and modified from ExcelDataReader - @MIT License

‎tests/MiniExcel.OpenXml.Tests/Issues/MiniExcelGithubIssuesTests.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ public void CellReferenceConverterEnforcesWorksheetLimits()
3535
Assert.False(CellReferenceConverter.TryParseCellReference("A1048577", out _, out _));
3636
}
3737

38+
[Theory]
39+
[InlineData("B3", 2, 3)]
40+
[InlineData("b03", 2, 3)]
41+
[InlineData("A", 0, 0)]
42+
[InlineData("A0", 0, 0)]
43+
[InlineData("A1x", 0, 0)]
44+
[InlineData("A-1", 0, 0)]
45+
[InlineData("A99999999999", 0, 0)]
46+
public void CellReferenceConverterParsesRowNumbers(string reference, int expectedColumn, int expectedRow)
47+
{
48+
var parsed = CellReferenceConverter.TryParseCellReference(reference, out var column, out var row);
49+
50+
Assert.Equal(expectedRow > 0, parsed);
51+
Assert.Equal(expectedRow, row);
52+
if (parsed)
53+
Assert.Equal(expectedColumn, column);
54+
}
55+
3856
[Fact]
3957
public void QueryRangeAcceptsMaximumWorksheetCoordinates()
4058
{

0 commit comments

Comments
 (0)