Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion benchmarks/NPOI.Benchmarks/LargeSSTBenchmark.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BenchmarkDotNet.Attributes;
using NPOI.XSSF.Model;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -46,8 +47,17 @@ public void XSSFWorkbookLargeSstOpenDispose()
/// reduced in allocation compared to the old DOM path.
/// </summary>
[Benchmark]
public void XSSFWorkbookLargeSstLoadStrings()
public void XSSFWorkbookLargeSstLoadStringsViaXmlReader()
{
SharedStringsTable.UseXmlReader = true;
using var workbook = new XSSFWorkbook(_largeFileWithSstPath, true);
// Force SST parse
_ = workbook.GetSharedStringSource().Count;
}
[Benchmark]
public void XSSFWorkbookLargeSstLoadStringsViaXmlDocument()
{
SharedStringsTable.UseXmlReader = false;
using var workbook = new XSSFWorkbook(_largeFileWithSstPath, true);
// Force SST parse
_ = workbook.GetSharedStringSource().Count;
Expand Down
42 changes: 22 additions & 20 deletions ooxml/XSSF/Model/SharedStringsTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@ private void EnsureLoaded()
{
try
{
ReadFromStream(stream);
if(UseXmlReader)
ReadFromStreamViaXmlReader(stream);
else
ReadFromStreamViaXmlDocument(stream);
}
catch (XmlException e)
{
Expand Down Expand Up @@ -218,7 +221,24 @@ private void EnsureStmapBuilt()
/// </summary>
private const int TextReadBufferSize = 1024;

private void ReadFromStream(Stream stream)
internal static bool UseXmlReader { get; set; } = true;

public void ReadFromStreamViaXmlDocument(Stream is1)
{
try
{
XmlDocument xml = ConvertStreamToXml(is1);
_sstDoc = SstDocument.Parse(xml, NamespaceManager);
CT_Sst sst = _sstDoc.GetSst();
count = sst.count;
uniqueCount = sst.uniqueCount;
}
catch (XmlException e)
{
throw new IOException("unable to parse shared strings table", e);
}
}
private void ReadFromStreamViaXmlReader(Stream stream)
{
_sstDoc = new SstDocument();
_sstDoc.AddNewSst();
Expand Down Expand Up @@ -509,24 +529,6 @@ private static CT_Color ParseColorAttributes(XmlReader reader)
return color;
}

/// <summary>
/// Read shared strings from a stream. Kept for backward compatibility; internally
/// delegates to the streaming parser.
/// </summary>
public void ReadFrom(Stream is1)
{
try
{
ReadFromStream(is1);
_loaded = true;
_stmapBuilt = false;
}
catch (XmlException e)
{
throw new IOException("unable to parse shared strings table", e);
}
}

private static String GetKey(CT_Rst st)
{
return st.XmlText;
Expand Down
Loading