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

Memoize subdivision YAML loading #510

Merged
merged 1 commit into from
Mar 15, 2018

Conversation

mdehoog
Copy link
Contributor

@mdehoog mdehoog commented Mar 15, 2018

Currently the subdivision YAML files are being loaded for every new Country instance. This PR memoizes the loaded YAML file on a class instance variable.

Also replaced the inject -> merge pattern with a more performant version.

Performance changes:
From:

pry(main)> Benchmark.realtime { 10000.times { Country['US'].subdivisions } }
=> 17.20118099998217

To:

pry(main)> Benchmark.realtime { 10000.times { Country['US'].subdivisions } }
=> 0.6878289999440312

@mdehoog mdehoog force-pushed the memoize-subdivision-yaml branch 4 times, most recently from bb1843e to 2422b9a Compare March 15, 2018 07:34
@mdehoog mdehoog force-pushed the memoize-subdivision-yaml branch from 2422b9a to a239baa Compare March 15, 2018 07:53
@rposborne
Copy link
Collaborator

@mdehoog What are the memory benchmarks on this? But this is awesome.

@mdehoog
Copy link
Contributor Author

mdehoog commented Mar 15, 2018

Before:

pry(main)> Benchmark.memory { |x| x.report { Country.all.each(&:subdivisions) } }
Calculating -------------------------------------
                        16.663M memsize (     0.000  retained)
                       130.895k objects (     0.000  retained)
                        50.000  strings (     0.000  retained)

After:

pry(main)> Benchmark.memory { |x| x.report { Country.all.each(&:subdivisions) } }
Calculating -------------------------------------
                         8.122M memsize (     4.710M retained)
                       121.002k objects (    64.401k retained)
                        50.000  strings (    50.000  retained)

More memory is retained (~4.7mb), but interestingly not as much is allocated.

@rposborne
Copy link
Collaborator

And for clarity these only loaded if you call subdivisions? So no performance change if you are not using subdivisions?

@mdehoog
Copy link
Contributor Author

mdehoog commented Mar 15, 2018

Correct, the subdivisions are still loaded lazily, but are now memoized at a class level rather than an instance level.

@rposborne
Copy link
Collaborator

Well hell yea, this is great.

@rposborne rposborne merged commit e226cb1 into countries:master Mar 15, 2018
@mdehoog mdehoog deleted the memoize-subdivision-yaml branch March 15, 2018 17:51
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.

2 participants