Eager load all API specs and reduce memory usage #285
+4,165
−4,180
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.
Previously this attempted to lazy load specs to avoid wasting memory, however this unfortunately had the opposite effect.
In production we want to load as much as possible as early as possible, as after booting the application we fork 16 workers from a parent process. Memory from the parent process can be shared with the children via the operating system's Copy on Write mechanism (not everything gets shared, I've been using 2/3 as a rule of thumb). Even if nothing was shared, it would be desirable so that we don't incur the expense of initialization and extra GC costs while in the middle of serving a request to the user.
We've been looking at heap dumps from production and elastomer client is one of the top contributors to "objects made after boot which persist for the life of the application". As far as I can tell at least two of these specs are loaded. This both wastes memory and slows down GC during application boot.
The first commit in this PR accomplishes eager loading, which uses an extra ~6.5MB of RAM. I think this would be desirable on its own. (better to waste that once than waste half of it 16 times).
The rest of the commits reduce memory usage by removing unnecessary data from the files, using arrays instead of hashes, and adding
frozen_string_literal: true
.If we wanted to go further we could load the specs from JSON (which is cheaper than loading Ruby, it's a simpler language) and also de-duplicate between the versions, but I think this is a decent improvement for now.