Skip to content

Commit

Permalink
Avoid allocating a column_type Hash when unnecessary
Browse files Browse the repository at this point in the history
Only the Postgres provide these types, so no point allocating
a hash every time for other adapters.
  • Loading branch information
byroot committed May 6, 2024
1 parent f6fd15c commit 743763b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ def build_statement_pool
#
# This is an internal hook to make possible connection adapters to build
# custom result objects with connection-specific data.
def build_result(columns:, rows:, column_types: {})
def build_result(columns:, rows:, column_types: nil)
ActiveRecord::Result.new(columns, rows, column_types)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: fa
fmod = result.fmod i
types[fname] = types[i] = get_oid_type(ftype, fmod, fname)
end
build_result(columns: fields, rows: result.values, column_types: types)
build_result(columns: fields, rows: result.values, column_types: types.freeze)
end
end

Expand Down
10 changes: 7 additions & 3 deletions activerecord/lib/active_record/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def self.empty(async: false) # :nodoc:
end
end

def initialize(columns, rows, column_types = {})
def initialize(columns, rows, column_types = nil)
@columns = columns
@rows = rows
@hash_rows = nil
@column_types = column_types
@column_types = column_types || EMPTY_HASH
end

# Returns true if this result set includes the column named +name+
Expand Down Expand Up @@ -192,7 +192,11 @@ def hash_rows
end
end

EMPTY = new([].freeze, [].freeze, {}.freeze).freeze
empty_array = [].freeze
EMPTY_HASH = {}.freeze
private_constant :EMPTY_HASH

EMPTY = new(empty_array, empty_array, EMPTY_HASH).freeze
private_constant :EMPTY

EMPTY_ASYNC = FutureResult.wrap(EMPTY).freeze
Expand Down

0 comments on commit 743763b

Please sign in to comment.