Repository navigation
Description
We upgraded to rails 7.1.3 and at the same time I upgraded our version of creek from 2.5.3 to 2.6.3.
After deploying the new rails version, we noticed our memory usage going up significantly as well as our time to process spreadsheets.
I tested the two versions in irb isolation and saw no difference, with a slight improvement for 2.6.3. However, when in docker the difference is massive.
(See #122 (comment), my testing methodology wasn't great for local Mac and the difference is less but still large. This is not just docker)
[3] [app][development] pry(main)> Creek::VERSION
=> "2.5.3"
[4] [app][development] pry(main)> time = Time.now; creek = Creek::Book.new('performance-test.xlsx'); puts(Time.now - time);
8.105815712
[5] [app][development] pry(main)> time = Time.now; creek.sheets[0].simple_rows.map(&:inspect); puts(Time.now - time);
38.974233879
[6] [app][development] pry(main)>VmPeak: 709908 kB
VmSize: 593128 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 616644 kB
VmRSS: 518568 kB
VmData: 559256 kB
VmStk: 8188 kB
VmExe: 4 kB
VmLib: 24516 kB
VmPTE: 1372 kB
VmSwap: 0 kB
[1] [app][development] pry(main)> Creek::VERSION
=> "2.6.3"
[2] [app][development] pry(main)> time = Time.now; creek = Creek::Book.new('performance-test.xlsx'); puts(Time.now - time);
8.186497337
[3] [app][development] pry(main)> time = Time.now; creek.sheets[0].simple_rows.map(&:inspect); puts(Time.now - time);
66.137462044
[4] [app][development] pry(main)>VmPeak: 3852528 kB
VmSize: 603644 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 3765544 kB
VmRSS: 529308 kB
VmData: 569772 kB
VmStk: 8188 kB
VmExe: 4 kB
VmLib: 24516 kB
VmPTE: 1520 kB
VmSwap: 0 kB
As you can see in the above, the problem appears to be the row generator. Also, the peak memory usage went from 709,000 KB to 3,852,000 KB, 5 times more memory!
Here is the spreadsheet I used for testing (a subset of chicago's public crime data) https://drive.usercontent.google.com/download?id=1zWiCRYCS7Vs9EPZyOqcKr-eT59F7w3dm&export=download (give it a minute to start downloading, google has to virus scan it first and it takes forever)
Activity
azrazalea-debtbook commentedon Feb 21, 2024
More system information:
Ruby 3.2.2
Docker arm64 alpine linux on Mac OS X host via Docker Desktop
azrazalea-debtbook commentedon Feb 21, 2024
Related to #121 but more specific and I was able to find some data I can show you to reproduce it!
[-][RAILS ONLY] Large Memory and Speed performance regression from 2.5.3 to 2.6.3[/-][+][Docker ONLY] Large Memory and Speed performance regression from 2.5.3 to 2.6.3[/+]azrazalea-debtbook commentedon Feb 21, 2024
Found that this is actually docker not rails. I spun up a fresh rails instance and wasn't producing it locally. Then, I used the rails default dockerfile modified to strip it back closer to essentials.
This is on arm64 architecture but I have the same problem in our production amd64 architecture docker images too.
And built it
docker build .Then ran docker
docker run -it <image sha>Finally inside docker I ran irb:
Inside irb I did:
In a separate shell to the same docker container (
docker exec -it <container SHA (NOT image sha> bash) I did:azrazalea-debtbook commentedon Feb 21, 2024
Note: I tried this in an amd_64 version of the same container and got the same result. 3GB RAM usage in 2.6.3, 800MB RAM in 2.5.3. Still on Mac OS X host though
azrazalea-debtbook commentedon Feb 21, 2024
Nevermind, I'm not used to Mac OS X and had trouble figuring out the peak memory usage.
Got
gtimeworking though and amd64 Mac OS X Sonoma 14.3.1 ruby 3.2.2 shows a similar though smaller effect.So this is not docker or rails specific, I just failed at figuring out peak memory usage on Mac and the speed didn't get way slower like it did in docker.
It is better locally at least. Only about 2.6x more memory not 5x.
[-][Docker ONLY] Large Memory and Speed performance regression from 2.5.3 to 2.6.3[/-][+]Large Memory and Speed performance regression from 2.5.3 to 2.6.3[/+]azrazalea-debtbook commentedon Feb 21, 2024
Identified commit 494ed05 as the culprit.
Version 2.6
Commit f8856ee
Commit 494ed05
transoceanic2000 commentedon Feb 18, 2025
The issue appears to be that calling
node.namespaces(which looks for namespaces on this node, and all the way up the document hierarchy) is hugely expensive within nokogiri, rather than strings within creek - although they could be improved.I've run a test using allocation_stats and mem GetProcessMem, reading 10k rows from a spreadsheet where I've observed this problem.
With the code as is results in a large number of hash allocations and lots of allocations within Nokogiri:
If I change the code so that we only do that call if the node appears to have a prefix then the tests still pass, but the memory allocations go right down...
if prefix.empty? && node.prefix && node.namespaces.any?I'm not sure that would work with all spreadsheets that caused this change to be added, but it narrows down the causes.
alx75 commentedon May 26, 2025
Hello,
I've encountered the same issue and this commit was not enough to fix the issue in my case. I try to parse a 33MB xlsx file (500_000 rows) and it consumes 8GB. The culprit is the
node.namespacescall on every row as @transoceanic2000 mentionned.Indeed :
if prefix.empty? && node.prefix && node.namespaces.any?should work but from what I understand, nokogiri supply the node
namespace_uriandprefixso we should be able to skip all node that are not from our namespace of interest and grab the prefix used the first time a node in our namespace is seen. I think it's a bit easier to understand and doesn't require thenamespacese.g:
I'll do some tests when times allow it and supply an MR unless someone has a better idea.
Thanks.
connorshea commentedon Sep 8, 2026
This should be fixed by #138 if it gets merged