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

[ruby] Reduce random_id calls in queries and update #9016

Merged
merged 2 commits into from May 16, 2024

Conversation

p8
Copy link
Contributor

@p8 p8 commented May 13, 2024

Calling Array#sample with a size x is faster than calling rand x times.
Depends on: #9027

@p8 p8 force-pushed the ruby/use-sample-for-random branch 3 times, most recently from 76674e9 to 09c92d1 Compare May 15, 2024 09:24
p8 added 2 commits May 15, 2024 22:22
This fixes connection errors:

      /usr/local/bundle/gems/activerecord-7.1.3.1/lib/active_record/connection_adapters/trilogy_adapter.rb:61:in
      `rescue in new_client': trilogy_auth_recv: caching_sha2_password
      requires either TCP with TLS or a unix socket: TRILOGY_UNSUPPORTED
      (ActiveRecord::ConnectionNotEstablished)
Calling `Array#sample` with a size x is faster than calling `rand` x
times.
@p8 p8 force-pushed the ruby/use-sample-for-random branch from 09c92d1 to 04fba54 Compare May 15, 2024 20:28
@NateBrady23
Copy link
Member

That's really interesting. My kneejerk reaction to this is, doesn't .sample just call rand(x) or the equivalent rb_random_int behind the scenes x times?

@NateBrady23 NateBrady23 merged commit 2bbfa55 into TechEmpower:master May 16, 2024
3 checks passed
@p8 p8 deleted the ruby/use-sample-for-random branch May 16, 2024 19:36
@p8
Copy link
Contributor Author

p8 commented May 16, 2024

I don't think I understand the implementation.
It is a bit faster though:

require 'benchmark/ips'

Benchmark.ips do |x|
  # Configure the number of seconds used during
  # the warmup phase (default 2) and calculation phase (default 5)
  x.config(:time => 5, :warmup => 2)

  QUERY_RANGE = (1..10_000).freeze # range of IDs in the Fortune DB
  x.report("rand") do
    Array.new(500) { rand(QUERY_RANGE) }
  end

  ALL_IDS = QUERY_RANGE.to_a # enumeration of all the IDs in fortune DB
  x.report("sample") do
    ALL_IDS.sample(500)
  end

  # Compare the iterations per second of the various reports!
  x.compare!
end
ruby 3.2.4 (2024-04-23 revision af471c0e01) [x86_64-darwin22]
Warming up --------------------------------------
                rand     1.666k i/100ms
              sample     6.395k i/100ms
Calculating -------------------------------------
                rand     16.709k (± 1.4%) i/s -     84.966k in   5.085923s
              sample     65.047k (± 2.7%) i/s -    326.145k in   5.017962s

Comparison:
              sample:    65047.4 i/s
                rand:    16709.4 i/s - 3.89x  slower

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

2 participants