This closes #2121, use mmap to open spreadsheet file#2331
Open
artur-chopikian wants to merge 1 commit into
Open
This closes #2121, use mmap to open spreadsheet file#2331artur-chopikian wants to merge 1 commit into
artur-chopikian wants to merge 1 commit into
Conversation
Replace os.Open with golang.org/x/exp/mmap.Open in OpenFile so the operating system page-maps the .xlsx into the process address space. Subsequent zip section reads served by archive/zip are zero-copy from the page cache instead of being read through buffered syscalls into Go heap memory, reducing RSS growth and GC pressure when opening large spreadsheet files.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2331 +/- ##
==========================================
- Coverage 99.60% 99.60% -0.01%
==========================================
Files 32 32
Lines 26794 26793 -1
==========================================
- Hits 26688 26687 -1
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:
|
xuri
reviewed
May 21, 2026
Member
xuri
left a comment
There was a problem hiding this comment.
Thanks for your PR. Any benchmark data on the performance impact of using golang.org/x/exp/mmap instead of os? Specifically, how much memory is saved, and any speed impact? I suggest test with excelize-benchmark for performance related changes.
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.
Description
Replace
os.Openwithgolang.org/x/exp/mmap.OpeninOpenFile. The returned*mmap.ReaderAtimplementsio.ReaderAt, so it is passed unchanged to the existingopenReaderAtpath — no downstream changes are required, and the publicOpenFilesignature and behaviour stay the same.With memory mapping, the operating system page-maps the
.xlsxfile into the process address space on demand.archive/zipthen serves section reads directly from the kernel page cache instead of copying through bufferedreadsyscalls into Go heap buffers. The mapping is released byr.Close()beforeOpenFilereturns, matching the lifetime of the previous*os.File.Changes:
excelize.go: usemmap.Openand*mmap.ReaderAtinOpenFile; close the mapping on success and on error fromopenReaderAt.go.mod/go.sum: addgolang.org/x/expfor themmapsubpackage.Related Issue
Closes #2121 — Use mmap if possible (can save memory with big files).
Motivation and Context
OpenFileis the dominant entry point for reading spreadsheets from disk. Even after recent work to read the unencrypted workbook lazily throughio.ReaderAt, every section access still went through a bufferedreadinto a Go heap buffer, so opening a large.xlsxproduced RSS growth and GC churn proportional to the file size touched byarchive/zip.Memory mapping avoids both costs: the kernel page cache backs the mapping, reads are zero-copy, pages are reclaimable under memory pressure, and unrelated processes opening the same file share physical memory. The behaviour requested in #2121 is achieved without changing any caller code.
How Has This Been Tested
go build ./...— clean.go test -run 'TestOpenFile|TestOpenReader' -count=1 ./...— pass.go test -count=1 ./...— full suite passes (ok github.com/xuri/excelize/v2 37.000s).No new tests are added because the change is a transparent substitution behind an existing
io.ReaderAt: the entire existingOpenFiletest coverage exercises the mmap-backed path unchanged.Types of changes
Checklist