Skip to content

Commit 16edb13

Browse files
committed
Improve the Sequel benchmark to avoid many singleton classes
* These singleton classes are expensive to create and also cause a lot of uncached method lookups. * Both `Post.create` and `Post.where(id: i).first` create a new singleton class per call (through #clone of a dataset object which has a singleton class). * With this commit, no singleton classes are created during insertion and in `run_benchmark`. And therefore also no uncached method lookup.
1 parent a6e3350 commit 16edb13

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

benchmarks/sequel/benchmark.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,21 @@ class Post < Sequel::Model
4646
end
4747

4848
10000.times {
49-
Post.create(title: Random.alphanumeric(30),
50-
type_name: Random.alphanumeric(10),
51-
key: Random.alphanumeric(10),
52-
body: Random.alphanumeric(100),
53-
upvotes: rand(30),
54-
author_id: rand(30))
49+
DB[:posts].insert(
50+
title: Random.alphanumeric(30),
51+
type_name: Random.alphanumeric(10),
52+
key: Random.alphanumeric(10),
53+
body: Random.alphanumeric(100),
54+
upvotes: rand(30),
55+
author_id: rand(30))
5556
}
5657

5758
# heat any caches
58-
Post.where(id: 1).first.title
59+
Post[1].title
5960

6061
run_benchmark(10) do
6162
1.upto(1000) do |i|
62-
post = Post.where(id: i).first
63+
post = Post[i]
6364
"#{post.title}\n#{post.body}" \
6465
"type: #{post.type_name}, votes: #{post.upvotes}, updated on: #{post.updated_at}"
6566
end

0 commit comments

Comments
 (0)