Load contents of source files only when needed #559
Merged
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.
When compiling a big sketch, I noticed that
arduino-builder
took up a lot of RAM (over 1G, sometimes it even went over 1.7G and crashed with an out of memory error).Investigating, it turns out that this is because
arduino-builder
andarduino-cli
load all source file contents into memory when loading the sketch. In practice, only the .ino file contents are actually used, so the rest is just dead weight. For sketches that have a lot of extra baggage (we have libraries, a core and some host-system tools in the sketch directory), this can take up a lot of unneeded space.This PR modifies
sketch.Item
to remove theSource
string, which is then no longer filled at sketch load time. Instead, theGetSourceStr()
method loads the contents on demand (which is really only when merging the sketch files).This PR moves around some error handling (since I/O operations have moved), but I've managed to split most of the trivial code movements into separate commits.
To reproduce this problem, you can simply create an empty sketch and add some baggage (https://github.com/vancegroup/arduino-boost adds some 300MB of source files and is good for testing):
This uses
time
to show that the maximum resident memory size is nearly 400MB.With the patch applied, this is reduced to 70MB:
This still seems rather much. When running
arduino-builder
on the same sketch, the memory usage drops from 375MB to 37MB, so it seems thatarduino-builder
is a bit more efficient even (and on our much bigger production sketch things improved from 1.1G to just 27M, which is unexpectedly even less). But I guess this is something for another time.