Skip to content

Commit 9c70b5d

Browse files
authored
Restore performance by returning bool instead of object in TryGetMergeCellsAsync (#813)
1 parent 0f1774e commit 9c70b5d

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,14 @@ internal async IAsyncEnumerable<IDictionary<string, object>> InternalQueryRangeA
159159
// TODO: need to optimize performance
160160
// Q. why need 3 times openstream merge one open read? A. no, zipstream can't use position = 0
161161

162-
var mergeCellsResult = await TryGetMergeCellsAsync(sheetEntry, cancellationToken).ConfigureAwait(false);
163-
if (_config.FillMergedCells && !mergeCellsResult.IsSuccess)
162+
var mergeCellsContext = new MergeCellsContext { };
163+
164+
if (_config.FillMergedCells && !await TryGetMergeCellsAsync(sheetEntry, mergeCellsContext, cancellationToken).ConfigureAwait(false))
164165
{
165166
yield break;
166167
}
167168

168-
_mergeCells = mergeCellsResult.MergeCells;
169+
_mergeCells = mergeCellsContext.MergeCells;
169170

170171
var maxRowColumnIndexResult = await TryGetMaxRowColumnIndexAsync(sheetEntry, cancellationToken).ConfigureAwait(false);
171172
if (!maxRowColumnIndexResult.IsSuccess)
@@ -698,7 +699,7 @@ private async Task<CellAndColumn> ReadCellAndSetColumnIndexAsync(XmlReader reade
698699
}
699700
else if (XmlReaderHelper.IsStartElement(reader, "is", _ns))
700701
{
701-
var rawValue = await StringHelper.ReadStringItemAsync(reader,cancellationToken).ConfigureAwait(false);
702+
var rawValue = await StringHelper.ReadStringItemAsync(reader, cancellationToken).ConfigureAwait(false);
702703
if (!string.IsNullOrEmpty(rawValue))
703704
ConvertCellValue(rawValue, aT, xfIndex, out value);
704705
}
@@ -1076,25 +1077,13 @@ internal static async Task<GetMaxRowColumnIndexResult> TryGetMaxRowColumnIndexAs
10761077
return new GetMaxRowColumnIndexResult(true, withoutCR, maxRowIndex, maxColumnIndex);
10771078
}
10781079

1079-
internal class MergeCellsResult
1080+
internal class MergeCellsContext
10801081
{
1081-
public bool IsSuccess { get; }
1082-
public MergeCells MergeCells { get; }
1083-
1084-
public MergeCellsResult(bool isSuccess)
1085-
{
1086-
IsSuccess = isSuccess;
1087-
}
1088-
1089-
public MergeCellsResult(bool isSuccess, MergeCells mergeCells)
1090-
: this (isSuccess)
1091-
{
1092-
MergeCells = mergeCells;
1093-
}
1082+
public MergeCells MergeCells { get; set; }
10941083
}
10951084

10961085
[Zomp.SyncMethodGenerator.CreateSyncVersion]
1097-
internal static async Task<MergeCellsResult> TryGetMergeCellsAsync(ZipArchiveEntry sheetEntry, CancellationToken cancellationToken = default)
1086+
internal static async Task<bool> TryGetMergeCellsAsync(ZipArchiveEntry sheetEntry, MergeCellsContext mergeCellsContext, CancellationToken cancellationToken = default)
10981087
{
10991088
cancellationToken.ThrowIfCancellationRequested();
11001089

@@ -1110,7 +1099,7 @@ internal static async Task<MergeCellsResult> TryGetMergeCellsAsync(ZipArchiveEnt
11101099
using (XmlReader reader = XmlReader.Create(sheetStream, xmlSettings))
11111100
{
11121101
if (!XmlReaderHelper.IsStartElement(reader, "worksheet", _ns))
1113-
return new MergeCellsResult(false);
1102+
return false;
11141103
while (await reader.ReadAsync().ConfigureAwait(false))
11151104
{
11161105
if (!XmlReaderHelper.IsStartElement(reader, "mergeCells", _ns))
@@ -1119,7 +1108,7 @@ internal static async Task<MergeCellsResult> TryGetMergeCellsAsync(ZipArchiveEnt
11191108
}
11201109

11211110
if (!await XmlReaderHelper.ReadFirstContentAsync(reader, cancellationToken).ConfigureAwait(false))
1122-
return new MergeCellsResult(false);
1111+
return false;
11231112

11241113
while (!reader.EOF)
11251114
{
@@ -1155,7 +1144,9 @@ internal static async Task<MergeCellsResult> TryGetMergeCellsAsync(ZipArchiveEnt
11551144
}
11561145
}
11571146
}
1158-
return new MergeCellsResult(true, mergeCells);
1147+
1148+
mergeCellsContext.MergeCells = mergeCells;
1149+
return true;
11591150
}
11601151
}
11611152

0 commit comments

Comments
 (0)