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

Performance and memory usage fixes #2309

Merged
merged 4 commits into from
Jan 29, 2019

Conversation

krzysiek1507
Copy link
Contributor

Purpose

Performance and memory usage fixes.

Changes

  1. {}.select {} is faster than {}.dup.reject because it doesn't duplicate nor create another hash.
  2. {}.keys is always a new array so we can use sort! and save some memory.
  3. 2x {}.select {} with different blocks are faster than one {}.partition { }.map { |h| Hash[h] }
require 'benchmark/ips'
require 'benchmark/memory'
require 'set'

hsh = { include: 1, fields: 2, meta: 3, links: 4, e: 5 }.freeze
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links, :serialization_context, :key_transform]).freeze

[:ips, :memory].each do |type|
  Benchmark.public_send(type) do |x|
    x.report('map') { adapter_options, serializer_options = hsh.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] } }
    x.report('select') do
      adapter_options = hsh.select { |k, _| ADAPTER_OPTION_KEYS.include? k }
      serializer_options = hsh.reject { |k, _| ADAPTER_OPTION_KEYS.include? k }
    end

    x.compare!
  end

  [].sort
end


Warming up --------------------------------------
                 map    34.293k i/100ms
              select    47.284k i/100ms
Calculating -------------------------------------
                 map    415.582k (± 2.4%) i/s -      2.092M in   5.036739s
              select    529.549k (± 5.0%) i/s -      2.695M in   5.107481s

Comparison:
              select:   529548.5 i/s
                 map:   415581.6 i/s - 1.27x  slower

Calculating -------------------------------------
                 map   904.000  memsize (     0.000  retained)
                        11.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)
              select   384.000  memsize (     0.000  retained)
                         2.000  objects (     0.000  retained)
                         0.000  strings (     0.000  retained)

Comparison:
              select:        384 allocated
                 map:        904 allocated - 2.35x more

@bf4
Copy link
Member

bf4 commented Jan 24, 2019

Looks like we need a pr to fix ci :(

@bf4
Copy link
Member

bf4 commented Jan 29, 2019

rebase, add a note to the changelog, and we can merge

@krzysiek1507
Copy link
Contributor Author

@bf4 all green!

@bf4 bf4 merged commit 209834d into rails-api:0-10-stable Jan 29, 2019
@krzysiek1507 krzysiek1507 deleted the peformance-fixes branch January 30, 2019 11:48
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