Repository navigation
Fix method_missing performance regression for virtual SELECT alias attributes - #57226
Merged
skipkayhil merged 1 commit intoAug 13, 2026
Merged
skipkayhil merged 1 commit into
skipkayhil merged 1 commit into
Conversation
skipkayhil
force-pushed
the
fix-method-missing-name-error-perf-regression
branch
from
August 13, 2026 03:06
420f63e to
8104d00
Compare
…tributes Guard `public_instance_method` call with `public_method_defined?` to avoid raising and rescuing `NameError` for attributes not backed by real columns (e.g. `SELECT 42 AS virtual_attr`). The `NameError` exception allocation + backtrace capture costs ~9μs per call, causing a ~3x regression introduced in rails#53890. This restores performance by using a cheap boolean check on the common path. Fixes rails#57183.
skipkayhil
force-pushed
the
fix-method-missing-name-error-perf-regression
branch
from
August 13, 2026 03:10
8104d00 to
56f67b9
Compare
Contributor
|
More thorough benchmark: require "benchmark/ips"
require "active_record"
STAGE = ENV.fetch("STAGE", "after")
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.lease_connection.create_table(:posts) { |t| t.string :title }
class Post < ActiveRecord::Base; end
row = Post.create!(title: "x")
extra = Post.select("id, title, 10 as tenderlove").first
CASES = {
"title" => -> { row.title }, # attribute method
"tenderlove" => -> { extra.tenderlove }, # dynamic attribute
}
CASES.each do |label, block|
puts
Benchmark.ips do |x|
x.config(time: 3, warmup: 1)
x.save!("/tmp/bench_#{label}.hold")
x.report("#{label} (#{STAGE})") { block.call }
x.compare!
end
endThis theoretically could slow down the first caller of a column if attribute methods aren't defined, but once defined this only improves the performance of virtual columns 👍 |
skipkayhil
added a commit
that referenced
this pull request
Aug 13, 2026
…r-perf-regression Fix method_missing performance regression for virtual SELECT alias attributes
skipkayhil
added a commit
that referenced
this pull request
Aug 13, 2026
…r-perf-regression Fix method_missing performance regression for virtual SELECT alias attributes
skipkayhil
added a commit
that referenced
this pull request
Aug 13, 2026
…r-perf-regression Fix method_missing performance regression for virtual SELECT alias attributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation / Background
Fixes #57183.
PR #53890 (shipped in 7.2.3) changed
method_missinginActiveRecord::AttributeMethodsto callself.class.public_instance_method(name), which raises and rescuesNameErrorfor virtual SELECT alias attributes (not backed by real columns). Exception allocation + backtrace capture costs ~9μs per call, causing a ~3x regression in virtual attribute access.Solution
Guard
public_instance_methodwithpublic_method_defined?— a zero-allocation boolean check — so theNameErrorpath is never hit for virtual attributes. The thread-safety properties introduced in #53890 are fully preserved sincepublic_method_defined?is a simple lookup on the method table thatdefine_attribute_methodsjust populated.Benchmark
Checklist