From 17ac7f36dae43856cb02b9da30a5835128b4bddb Mon Sep 17 00:00:00 2001 From: Petrik Date: Mon, 19 Jan 2026 21:23:23 +0100 Subject: [PATCH] [ruby/sequel] Use setter methods instead of Hash Creating a model from a hash calls Sequel::Model::InstanceMethods#set_restricted which checks if the keys are defined as setter methods. Calling the setter methods directly perform better. --- frameworks/Ruby/rack-app/app.rb | 10 ++++++---- frameworks/Ruby/rack-sequel/hello_world.rb | 10 ++++++---- .../app/controllers/benchmarks_controller.rb | 6 +++++- frameworks/Ruby/roda-sequel/hello_world.rb | 10 ++++++---- frameworks/Ruby/sinatra-sequel/hello_world.rb | 10 ++++++---- frameworks/Ruby/sinatra/hello_world.rb | 8 ++++---- 6 files changed, 33 insertions(+), 21 deletions(-) diff --git a/frameworks/Ruby/rack-app/app.rb b/frameworks/Ruby/rack-app/app.rb index 49ed344c7e0..af4b7d20b27 100644 --- a/frameworks/Ruby/rack-app/app.rb +++ b/frameworks/Ruby/rack-app/app.rb @@ -23,10 +23,12 @@ class App < Rack::App helpers do def fortunes fortunes = Fortune.all - fortunes << Fortune.new( - id: 0, - message: "Additional fortune added at request time." - ) + + fortune = Fortune.new + fortune.id = 0 + fortune.message = "Additional fortune added at request time." + fortunes << fortune + fortunes.sort_by!(&:message) end end diff --git a/frameworks/Ruby/rack-sequel/hello_world.rb b/frameworks/Ruby/rack-sequel/hello_world.rb index 491b9634e3c..4b5e64f2507 100644 --- a/frameworks/Ruby/rack-sequel/hello_world.rb +++ b/frameworks/Ruby/rack-sequel/hello_world.rb @@ -48,10 +48,12 @@ def queries(env) def fortunes fortunes = Fortune.all - fortunes << Fortune.new( - id: 0, - message: 'Additional fortune added at request time.' - ) + + fortune = Fortune.new + fortune.id = 0 + fortune.message = "Additional fortune added at request time." + fortunes << fortune + fortunes.sort_by!(&:message) html = String.new(<<~'HTML') diff --git a/frameworks/Ruby/rage-sequel/app/controllers/benchmarks_controller.rb b/frameworks/Ruby/rage-sequel/app/controllers/benchmarks_controller.rb index 4765293093d..a1c091bbeae 100644 --- a/frameworks/Ruby/rage-sequel/app/controllers/benchmarks_controller.rb +++ b/frameworks/Ruby/rage-sequel/app/controllers/benchmarks_controller.rb @@ -26,7 +26,11 @@ def queries def fortunes records = Fortune.all - records << Fortune.new(id: 0, message: "Additional fortune added at request time.") + fortune = Fortune.new + fortune.id = 0 + fortune.message = "Additional fortune added at request time." + records << fortune + records.sort_by!(&:message) render plain: FORTUNES_TEMPLATE.result(binding) diff --git a/frameworks/Ruby/roda-sequel/hello_world.rb b/frameworks/Ruby/roda-sequel/hello_world.rb index 3c3845187f2..ec00dd518cf 100644 --- a/frameworks/Ruby/roda-sequel/hello_world.rb +++ b/frameworks/Ruby/roda-sequel/hello_world.rb @@ -67,10 +67,12 @@ def rand1 r.is "fortunes" do response[CONTENT_TYPE] = HTML_TYPE @fortunes = Fortune.all - @fortunes << Fortune.new( - id: 0, - message: "Additional fortune added at request time." - ) + + fortune = Fortune.new + fortune.id = 0 + fortune.message = "Additional fortune added at request time." + @fortunes << fortune + @fortunes.sort_by!(&:message) view :fortunes end diff --git a/frameworks/Ruby/sinatra-sequel/hello_world.rb b/frameworks/Ruby/sinatra-sequel/hello_world.rb index cf6cfac54cc..a4254b52fd8 100644 --- a/frameworks/Ruby/sinatra-sequel/hello_world.rb +++ b/frameworks/Ruby/sinatra-sequel/hello_world.rb @@ -58,10 +58,12 @@ class HelloWorld < Sinatra::Base # Test type 4: Fortunes get '/fortunes' do @fortunes = Fortune.all - @fortunes << Fortune.new( - id: 0, - message: 'Additional fortune added at request time.' - ) + + fortune = Fortune.new + fortune.id = 0 + fortune.message = "Additional fortune added at request time." + @fortunes << fortune + @fortunes.sort_by!(&:message) render_html :fortunes diff --git a/frameworks/Ruby/sinatra/hello_world.rb b/frameworks/Ruby/sinatra/hello_world.rb index 628ce12a6d0..917459090ad 100644 --- a/frameworks/Ruby/sinatra/hello_world.rb +++ b/frameworks/Ruby/sinatra/hello_world.rb @@ -65,10 +65,10 @@ class HelloWorld < Sinatra::Base @fortunes = ActiveRecord::Base.with_connection do Fortune.all end.to_a - @fortunes << Fortune.new( - id: 0, - message: 'Additional fortune added at request time.' - ) + fortune = Fortune.new + fortune.id = 0 + fortune.message = "Additional fortune added at request time." + @fortunes << fortune @fortunes.sort_by!(&:message) render_html :fortunes