Enhance bufferedWriter with bufio.Writer, scratch space, and configurable thresholds (B1)#2321
Open
AdamDrewsTR wants to merge 3 commits into
Open
Enhance bufferedWriter with bufio.Writer, scratch space, and configurable thresholds (B1)#2321AdamDrewsTR wants to merge 3 commits into
AdamDrewsTR wants to merge 3 commits into
Conversation
…able thresholds - Add bufio.Writer layer after temp file threshold is crossed, reducing disk write syscalls from per-Write to per-buffer-full (default 128 KiB) - Add scratch [24]byte field for strconv.Append* to avoid heap allocations in WriteInt, WriteUint, and WriteFloat helper methods - Track total bytes written for offset-based operations (WriteAt) - Add WriteAt method for updating data at specific offsets in both in-memory and temp file modes - Add CopyTo method with large read buffer to minimize Pread syscalls when copying data to ZIP writers - Add Bytes method for direct access to in-memory buffer contents - Add Reset method for clearing state without closing the temp file - Add StreamingChunkSize option to control when streaming spills to disk (0 = default 16 MiB, -1 = never spill, keeps all data in memory) - Add StreamingBufSize option to control bufio.Writer size aft- Add StreamingBufSize option to control bufio.Writer size aft- Add StreamingBufSize option to control bufio.Writer size aft- Add Streaminak memory usage during streaming writes
This was referenced May 12, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2321 +/- ##
=======================================
Coverage 99.60% 99.60%
=======================================
Files 32 32
Lines 26791 26876 +85
=======================================
+ Hits 26685 26770 +85
Misses 55 55
Partials 51 51
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Cover WriteInt, WriteUint, WriteFloat, Bytes, WriteAt, CopyTo, and Reset methods in both in-memory and temp-file (bio) code paths. Test NewStreamWriter with custom StreamingChunkSize (-1, positive) and StreamingBufSize options.
Add tests for IO error scenarios when the underlying temp file is closed or broken: - TestBufferedWriterWriteAtFlushError: Flush fails in WriteAt - TestBufferedWriterCopyToFlushError: Flush fails in CopyTo - TestBufferedWriterCopyToSeekError: Seek fails in CopyTo - TestBufferedWriterSyncWriteToError: WriteTo fails in Sync This brings diff coverage from 95.56% to 100%.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enhances the internal
bufferedWriterused byStreamWriterto reduce syscalls and heap allocations during streaming worksheet generation. Adds two newOptionsfields to give callers control over when and how streaming data spills to disk.Changes
bufio.Writerlayer after thresholdOnce the in-memory buffer reaches the threshold, the old code flushed raw
bytes.Bufferto the temp file on everySync()call. The new code switches to abufio.Writerwrapping the temp file, so disk writes happen only when the bufio buffer is full (default 128 KiB). This reduces write syscalls by ~100x for large worksheets (e.g. 100 MB XML goes from ~3000 syscalls to ~400).After the switch, the
bytes.Bufferbacking array is released entirely (bw.buf = bytes.Buffer{}) to avoid holding two large buffers simultaneously.Scratch space for numeric formatting
A
[24]bytescratch field eliminates heap allocations in the newWriteInt,WriteUint, andWriteFloatmethods by usingstrconv.AppendInt/AppendUint/AppendFloatdirectly into stack memory.Offset tracking and
WriteAtThe
writtenfield tracks total bytes written.WriteAtallows updating data at specific offsets — in-memory mode modifies the buffer directly, temp file mode flushes bufio first then usespwrite.CopyTofor efficient streaming to ZIPCopyTocopies all buffered data to anio.Writerusing a large read buffer (256 KiB minimum, orbioSizeif larger) to minimize Pread syscalls when writing sheet data into the ZIP archive.Configurable options
StreamingChunkSize: Controls when streaming spills to a temp file. Default isStreamChunkSize(16 MiB). Set to-1to keep all data in memory (eliminates disk I/O when sufficient RAM is available).StreamingBufSize: Controls thebufio.Writersize after spill. Default isStreamingBufSizeDefault(128 KiB), which is the measured inflection point on both NVMe and HDD.Compatibility
All existing tests pass. The enhanced
bufferedWriteris a strict superset of the old one — existing callers that only useWrite,WriteString,Reader,Sync,Flush, andCloseare unaffected. The new methods (WriteInt,WriteUint,WriteFloat,WriteAt,CopyTo,Bytes,Reset) are additive.