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

Avoid expensive tracking objects for prepared statements #37200

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -10,7 +10,6 @@
require "arel/collectors/composite"
require "arel/collectors/sql_string"
require "arel/collectors/substitute_binds"
require "concurrent/atomic/thread_local_var"

module ActiveRecord
module ConnectionAdapters # :nodoc:
Expand Down Expand Up @@ -92,10 +91,10 @@ def initialize(connection, logger = nil, config = {}) # :nodoc:
@lock = ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new

if self.class.type_cast_config_to_boolean(config.fetch(:prepared_statements) { true })
@prepared_statement_status = Concurrent::ThreadLocalVar.new(true)
@prepared_statements_default = true
@visitor.extend(DetermineIfPreparableVisitor)
else
@prepared_statement_status = Concurrent::ThreadLocalVar.new(false)
@prepared_statements_default = false
end

@advisory_locks_enabled = self.class.type_cast_config_to_boolean(
Expand Down Expand Up @@ -139,7 +138,10 @@ def schema_migration # :nodoc:
end

def prepared_statements
@prepared_statement_status.value
if @prepared_statements_default
hash = Thread.current[:ar_prepared_statements]
!(hash && hash[self.object_id] == false)
end
end

class Version
Expand Down Expand Up @@ -226,7 +228,14 @@ def seconds_idle # :nodoc:
end

def unprepared_statement
@prepared_statement_status.bind(false) { yield }
if @prepared_statements_default
hash = Thread.current[:ar_prepared_statements] ||= {}
hash[self.object_id] = false
end

yield
ensure
hash.delete(self.object_id) if hash
end

# Returns the human-readable name of the adapter. Use mixed case - one
Expand Down