Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager load all API specs and reduce memory usage #285

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jhawthorn
Copy link
Member

@jhawthorn jhawthorn commented Dec 1, 2023

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.

$ wc -l api_spec_v8_6.rb api_spec_v8_7.rb
  6533 api_spec_v8_6.rb
  6554 api_spec_v8_7.rb
 13087 total

$ diff api_spec_v8_6.rb api_spec_v8_7.rb | wc -l
73

@look
Copy link
Member

look commented Dec 1, 2023

Silly question, but why are specs being loaded in production at all?

@jhawthorn
Copy link
Member Author

Not sure! I'd love if they weren't. It seems like they're needed for validation and to differentiate which parameters go in the path and which are query strings.

(also looks like I missed some test issues, will fix tomorrow)

Baseline:

    $ ruby -I lib -e 'puts `ps -o rss= -q #{$$}`'
    16256

Before:

    ruby -I lib -e 'require "elastomer_client/client"; puts `ps -o rss= -q #{$$}`'
    20724

After:

    ruby -I lib -e 'require "elastomer_client/client"; puts `ps -o rss= -q #{$$}`'
    27332
Previously this didn't truncate the file, so if the newly generated file
was smaller it would create a syntax error.

This also avoids loading anything except for version_support, since that
was the only part needed. To achieve that I switched to using JSON from
the stdlib instead of MutliJson
This could probably have been fully deleted, since it's only used in
test. This allows us to remove unnecessary and expensive data from the
spec files.
This about halves the memory usage of the gem
@jhawthorn
Copy link
Member Author

Rebased and tests should now be fixed 🤞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants