Skip to content

Commit 61ea1df

Browse files
authored
[ruby/roda] Reduce random_id calls in update (#8858)
Calling `Array#sample` with a size x is faster than calling `rand` x times.
1 parent 8d5bbc9 commit 61ea1df

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

frameworks/Ruby/roda-sequel/boot.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require "time"
44
require "oj"
55
MAX_PK = 10_000
6+
QUERY_RANGE = (1..MAX_PK).freeze
7+
ALL_IDS = QUERY_RANGE.to_a
68
QUERIES_MIN = 1
79
QUERIES_MAX = 500
810
SEQUEL_NO_ASSOCIATIONS = true

frameworks/Ruby/roda-sequel/hello_world.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ def rand1
3737
r.is "queries" do
3838
worlds =
3939
DB.synchronize do
40-
Array.new(bounded_queries) { World.with_pk(rand1).values }
40+
ALL_IDS.sample(bounded_queries).map do |id|
41+
World.with_pk(id).values
42+
end
4143
end
4244
worlds.to_json
4345
end
@@ -58,8 +60,8 @@ def rand1
5860
r.is "updates" do
5961
worlds =
6062
DB.synchronize do
61-
Array.new(bounded_queries) do
62-
world = World.with_pk(rand1)
63+
ALL_IDS.sample(bounded_queries).map do |id|
64+
world = World.with_pk(id)
6365
new_value = rand1
6466
new_value = rand1 while new_value == world.randomnumber
6567
world.update(randomnumber: new_value)

0 commit comments

Comments
 (0)