aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorLars Kanis <lars@greiz-reinsdorf.de>2015-03-21 21:52:35 +0100
committerLars Kanis <lars@greiz-reinsdorf.de>2015-03-25 15:05:23 +0100
commit9f2f268666e2f5d861905f4740943c85ea70f76e (patch)
tree7c1f5a33704bdb5b6d503bb1d13a0cfdec170b95 /activerecord/lib/active_record
parent1d8d5a74b81b8aab1f5e6d233d509a92525ed4e1 (diff)
downloadrails-9f2f268666e2f5d861905f4740943c85ea70f76e.tar.gz
rails-9f2f268666e2f5d861905f4740943c85ea70f76e.tar.bz2
rails-9f2f268666e2f5d861905f4740943c85ea70f76e.zip
PostgreSQL, Add input type casts for primitive types.
Ruby-pg's default way to serialize values for transmission to the database is to call #to_s . This however creates a temporary String object for each value. Setting a class based type map avoids the allocation of this additional String. The performance benefit is measurable in microbenchmarks, but not with the overhead of activerecord. However it's free to use and has no drawback.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 37bf4d77f8..5a304e6ec5 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -278,6 +278,7 @@ module ActiveRecord
@table_alias_length = nil
connect
+ add_pg_encoders
@statements = StatementPool.new @connection,
self.class.type_cast_config_to_integer(config.fetch(:statement_limit) { 1000 })
@@ -803,6 +804,15 @@ module ActiveRecord
end
end
+ def add_pg_encoders
+ map = PG::TypeMapByClass.new
+ map[Integer] = PG::TextEncoder::Integer.new
+ map[TrueClass] = PG::TextEncoder::Boolean.new
+ map[FalseClass] = PG::TextEncoder::Boolean.new
+ map[Float] = PG::TextEncoder::Float.new
+ @connection.type_map_for_queries = map
+ end
+
def add_pg_decoders
coders_by_name = {
'int2' => PG::TextDecoder::Integer,