From f4cd3a9893e2f4a0b6fffcd6824a422e3d603b96 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Sat, 10 Jan 2015 02:54:22 -0500 Subject: [PATCH] 3021: Do not compute etag for initial model state unless/until actually needed. Profiling shows that making a copy of the attributes hash (which modelinstance.attributes() does) is much faster than md5(attrs.inspect). Given that md5(attrs_when_loaded) is never needed in the common case where a model gets loaded but never changed or written back to the database, it's better to just stash a copy of the attributes hash when loading, and defer computing the md5 until it's time to write a log entry. --- services/api/app/models/arvados_model.rb | 8 ++++---- services/api/lib/kind_and_etag.rb | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/services/api/app/models/arvados_model.rb b/services/api/app/models/arvados_model.rb index a3e8ec6acd..38a46de09a 100644 --- a/services/api/app/models/arvados_model.rb +++ b/services/api/app/models/arvados_model.rb @@ -523,8 +523,8 @@ class ArvadosModel < ActiveRecord::Base end def log_start_state - @old_etag = etag - @old_attributes = logged_attributes + @old_attributes = attributes + @old_logged_attributes = logged_attributes end def log_change(event_type) @@ -543,14 +543,14 @@ class ArvadosModel < ActiveRecord::Base def log_update log_change('update') do |log| - log.fill_properties('old', @old_etag, @old_attributes) + log.fill_properties('old', etag(@old_attributes), @old_logged_attributes) log.update_to self end end def log_destroy log_change('destroy') do |log| - log.fill_properties('old', @old_etag, @old_attributes) + log.fill_properties('old', etag(@old_attributes), @old_logged_attributes) log.update_to nil end end diff --git a/services/api/lib/kind_and_etag.rb b/services/api/lib/kind_and_etag.rb index 89c01ef3a2..04fdca426e 100644 --- a/services/api/lib/kind_and_etag.rb +++ b/services/api/lib/kind_and_etag.rb @@ -14,7 +14,7 @@ module KindAndEtag self.class.kind end - def etag - Digest::MD5.hexdigest(self.inspect).to_i(16).to_s(36) + def etag attrs=nil + Digest::MD5.hexdigest((attrs || self.attributes).inspect).to_i(16).to_s(36) end end -- 2.39.5