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

Update persistence.rb #1720

Open
wants to merge 1 commit into
base: 11
Choose a base branch
from
Open

Conversation

RicardoTrindade
Copy link
Contributor

Tiny change to props method that hopefully speeds it up.

This pull introduces/changes:

  • Speeds up ActiveGraph::Shared::Persistence#props method by calling compact instead of looping through values with reject
require "benchmark/ips"
require "active_support/core_ext/hash/keys"

h = {"a" => 1, "b" => nil}

def compact_symbol(h)
  h.compact.transform_keys(&:to_sym)
end

def reject_symb(h)
  h.reject { |_, v| v.nil? }.symbolize_keys
end

Benchmark.ips do |x|
  x.report("with compact + transform keys") { compact_symbol(h) }
  x.report("reject + symbolize keys") { reject_symb(h) }
  x.compare!
end


ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin22]
Warming up --------------------------------------
with compact + transform keys
                       531.162k i/100ms
reject + symbolize keys
                       385.490k i/100ms
Calculating -------------------------------------
with compact + transform keys
                          5.238M (± 0.7%) i/s -     26.558M in   5.070284s
reject + symbolize keys
                          3.892M (± 1.0%) i/s -     19.660M in   5.051196s

Comparison:
with compact + transform keys:  5238259.6 i/s
reject + symbolize keys:  3892499.0 i/s - 1.35x  slower

compact is faster than looping with `reject` and achieves the same result
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.

None yet

1 participant