From 3a0888203031b91a583f8ce35763f8d231467827 Mon Sep 17 00:00:00 2001 From: Andrey Chernih Date: Sun, 25 May 2014 12:55:29 +0400 Subject: Speed-up TimeWithZone to String conversion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I've noticed that `String(model.created_at)` is performing poorly in comparision with other fields. The source of the problem is a way `Kernel#String` works: it first tries to call `to_str` (which causes `NoMethodError` in `method_missing`) and then calls `to_s`. Performance tests: tz = Time.zone.now Benchmark.ips do |x| x.report { String(tz) } end Without this code: Calculating ------------------------------------- 572 i/100ms ------------------------------------------------- 10177.7 (±18.2%) i/s - 48620 in 5.000325s With this code: Calculating ------------------------------------- 1518 i/100ms ------------------------------------------------- 138984.2 (±10.1%) i/s - 677028 in 4.974897s --- activesupport/lib/active_support/time_with_zone.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index f2a2f3c3db..3155c0ab0f 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -353,6 +353,14 @@ module ActiveSupport initialize(variables[0].utc, ::Time.find_zone(variables[1]), variables[2].utc) end + # respond_to_missing? is not called in some cases, such as when type conversion is + # performed with Kernel#String + def respond_to?(sym, include_priv = false) + # ensure that we're not going to throw and rescue from NoMethodError in method_missing which is slow + return false if sym.to_sym == :to_str + super + end + # Ensure proxy class responds to all methods that underlying time instance # responds to. def respond_to_missing?(sym, include_priv) -- cgit v1.2.3