Skip to content

Optimize writing to StreamWriter#1681

Open
apetukhov wants to merge 2 commits intonissl-lab:masterfrom
apetukhov:write_format_strings_directly
Open

Optimize writing to StreamWriter#1681
apetukhov wants to merge 2 commits intonissl-lab:masterfrom
apetukhov:write_format_strings_directly

Conversation

@apetukhov
Copy link
Contributor

writing the formatted string directly to the StreamWriter, using an existing method that has the same semantics as string.Format
fix .cs files encoding to UTF-8 with BOM
fix used culture in TestCellFormat tests

fix files encoding
fix current culture in TestCellFormat tests
@tonyqus
Copy link
Member

tonyqus commented Feb 6, 2026

Since this is a performance optimzation PR, let's go further.

Direct Write (Fastest): Using streamWriter.Write(string) sends text directly to the internal buffer, requiring minimal CPU overhead.
Formatted Write (Slower): Using streamWriter.Write("Value: {0}", value) forces the system to parse the string, perform string conversion, and then write, increasing CPU time.

How about getting rid of the formatted write so that it can be faster?

For example

        sw.Write("<c:{0}", nodeName);

To

        sw.Write("<c:");
        sw.Write(nodeName);

@tonyqus
Copy link
Member

tonyqus commented Feb 6, 2026

In #1095, it has some similar optimization

sw.Write(string.Format("</w:{0}>", nodeName));

To

sw.WriteEndW(nodeName);

And

sw.Write(string.Format("</{0}>", nodeName));

To

sw.WriteEndElement(nodeName);

Please run the following beachmarks to see if there is real optimization as #1095 did.

NPOI.Benchmarks.LargeExcelFileBenchmark
NPOI.Benchmarks.RangeValuesBenchmark

@tonyqus tonyqus added this to the NPOI 2.7.6 milestone Feb 6, 2026
@tonyqus
Copy link
Member

tonyqus commented Feb 6, 2026

@lahma Do you have any further suggestion?

@lahma
Copy link
Collaborator

lahma commented Feb 6, 2026

agree with removing the format altogether, should be easy regex or AI work, also instead of sw.Write(">"); write using the char overload sw.Write('>');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments