Skip to content

Performance#1269

Merged
lgebhardt merged 5 commits intomasterfrom
performance
Jul 19, 2019
Merged

Performance#1269
lgebhardt merged 5 commits intomasterfrom
performance

Conversation

@lgebhardt
Copy link
Copy Markdown
Contributor

A set of tweaks to reduce memory allocation in the worst spots. In general I've been trying to avoid premature optimizations, but I think in this case they are clear enough, and have enough benefit, to make it worth the minor added complexity.

Results from using the Peeps app with rack-mini-profiler:

gem 'rack-mini-profiler'
gem 'memory_profiler'

http://localhost:3000/contacts?include=phone-numbers&pp=profile-memory

Without:

Total allocated: 7562865 bytes (83291 objects)
Total retained:  543442 bytes (3826 objects)

allocated memory by gem
-----------------------------------
   2889655  jsonapi-resources/lib
   2001415  activesupport-5.2.3
    647682  activerecord-5.2.3
    561114  activemodel-5.2.3
    551528  set
    526434  json
    112432  bootsnap-1.4.4
     74306  sqlite3-1.4.1
     48232  tzinfo-1.2.5
     33544  arel-9.0.0
     29805  actionpack-5.2.3
     29399  digest
     23319  csv
      8973  rack-2.0.7
      6932  actionview-5.2.3
      4136  logger
      4104  concurrent-ruby-1.1.5
      3720  ipaddr
      2872  web-console-3.7.0
      1754  railties-5.2.3
       677  securerandom
       360  uri
       320  monitor
        72  mutex_m
        40  i18n-1.6.0
        40  rack-mini-profiler-1.0.2

And with this PR:

Total allocated: 6481135 bytes (71271 objects)
Total retained:  545970 bytes (3837 objects)

allocated memory by gem
-----------------------------------
   2001077  activesupport-5.2.3
   1808263  jsonapi-resources/lib
    647682  activerecord-5.2.3
    561114  activemodel-5.2.3
    551528  set
    526434  json
    112432  bootsnap-1.4.4
     74306  sqlite3-1.4.1
     48232  tzinfo-1.2.5
     33544  arel-9.0.0
     29805  actionpack-5.2.3
     29399  digest
     23319  csv
      8973  rack-2.0.7
      6932  actionview-5.2.3
      4136  logger
      4104  concurrent-ruby-1.1.5
      3720  ipaddr
      2872  web-console-3.7.0
      1754  railties-5.2.3
       677  securerandom
       360  uri
       320  monitor
        72  mutex_m
        40  i18n-1.6.0
        40  rack-mini-profiler-1.0.2

Timings run against a fresh development instance with no warm up show about a 6-7% improvement as well. The request is returning 50 primary and 100 related resources.

All Submissions:

  • I've checked to ensure there aren't other open Pull Requests for the same update/change.
  • I've submitted a ticket for my issue if one did not already exist.
  • My submission passes all tests. (Please run the full test suite locally to cut down on noise from travis failures.)
  • I've used Github auto-closing keywords in the commit message or the description.
  • I've added/updated tests for this change.

New Feature Submissions:

  • I've submitted an issue that describes this feature, and received the go ahead from the maintainers.
  • My submission includes new tests.
  • My submission maintains compliance with JSON:API.

Bug fixes and Changes to Core Features:

  • I've included an explanation of what the changes do and why I'd like you to include them.
  • I've provided test(s) that fails without the change.

Test Plan:

Reviewer Checklist:

  • Maintains compliance with JSON:API
  • Adequate test coverage exists to prevent regressions

Merging the default and the attribute specific options was resulting in
a lot of memory allocations
This hash only needs to be computed once, and repeatedly building it
results in a lot of memory allocations.
Reset the cache if the relationships or the attributes change
Optimization for memory allocation
@lgebhardt lgebhardt merged commit defc701 into master Jul 19, 2019
@lgebhardt lgebhardt deleted the performance branch July 19, 2019 14:53
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