Recently, in looking at profiling data from two different  
applications, I found that something like 5% of the application's time  
was being spent in converting strings to times on database fetches.   
Digging in, I found that the "fast" routine for doing this only worked  
if there was no timezone in the string.  I ended up monkeypatching  
this in:

  # Rails has a "fast" time parsing shortcut when the database returns
  # a timestamp string without timezone.  Unfortunately, ours does  
return a
  # timezone, but it's always UTC ("+00").  Hack so we can use the
  # fast routine still
  module ConnectionAdapters
    class Column
      module Format
        ISO_DATETIME_TZ = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d) 
(\.\d+)?(\+\d\d)?\z/
      end

      class << self
        protected
        def fast_string_to_time(string)
          if string =~ Format::ISO_DATETIME_TZ
            tz = $8
            if tz && !(tz == "+00" && Base.default_timezone == :utc)
              return nil
            end

            microsec = ($7.to_f * 1_000_000).to_i
            new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i,  
$6.to_i, microsec
          end
        end
      end
    end
  end


I don't know how generally applicable this patch would be.  Clearly,  
it only helps if both your DB and your Rails app are configured to run  
in UTC.  We use PostgreSQL, and I don't know if the same formating is  
used by other databases.  But, I thought I would throw it out for  
comments.


-kevin

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-core+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to