Skip to content

Commit 743763b

Browse files
committed
Avoid allocating a column_type Hash when unnecessary
Only the Postgres provide these types, so no point allocating a hash every time for other adapters.
1 parent f6fd15c commit 743763b

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ def build_statement_pool
11821182
#
11831183
# This is an internal hook to make possible connection adapters to build
11841184
# custom result objects with connection-specific data.
1185-
def build_result(columns:, rows:, column_types: {})
1185+
def build_result(columns:, rows:, column_types: nil)
11861186
ActiveRecord::Result.new(columns, rows, column_types)
11871187
end
11881188

activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: fa
7171
fmod = result.fmod i
7272
types[fname] = types[i] = get_oid_type(ftype, fmod, fname)
7373
end
74-
build_result(columns: fields, rows: result.values, column_types: types)
74+
build_result(columns: fields, rows: result.values, column_types: types.freeze)
7575
end
7676
end
7777

activerecord/lib/active_record/result.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def self.empty(async: false) # :nodoc:
4646
end
4747
end
4848

49-
def initialize(columns, rows, column_types = {})
49+
def initialize(columns, rows, column_types = nil)
5050
@columns = columns
5151
@rows = rows
5252
@hash_rows = nil
53-
@column_types = column_types
53+
@column_types = column_types || EMPTY_HASH
5454
end
5555

5656
# Returns true if this result set includes the column named +name+
@@ -192,7 +192,11 @@ def hash_rows
192192
end
193193
end
194194

195-
EMPTY = new([].freeze, [].freeze, {}.freeze).freeze
195+
empty_array = [].freeze
196+
EMPTY_HASH = {}.freeze
197+
private_constant :EMPTY_HASH
198+
199+
EMPTY = new(empty_array, empty_array, EMPTY_HASH).freeze
196200
private_constant :EMPTY
197201

198202
EMPTY_ASYNC = FutureResult.wrap(EMPTY).freeze

0 commit comments

Comments
 (0)