Performance: Improve exists call function caching and make sure that expensive marschal object copies are reduced where it is not needed.

This commit is contained in:
Rolf Schmidt 2021-12-13 13:11:32 +01:00
parent ecd29123c2
commit 827c18b471

View file

@ -56,22 +56,42 @@ class CoreWorkflow::Attributes
result result
end end
def selected def exists?
if @payload['params']['id'] && payload_class.exists?(id: @payload['params']['id']) return if @payload['params']['id'].blank?
result = saved_only
@exists ||= payload_class.exists?(id: @payload['params']['id'])
end
def overwritten
# params loading and preparing is very expensive so cache it
checksum = Digest::MD5.hexdigest(Marshal.dump(@payload['params']))
return @overwritten[checksum] if @overwritten.present? && @overwritten[checksum]
@overwritten = {}
@overwritten[checksum] = begin
result = saved_only(dump: true)
overwrite_selected(result) overwrite_selected(result)
end
end
def selected
if exists?
overwritten
else else
selected_only selected_only
end end
end end
def saved_only def saved_only(dump: false)
return if @payload['params']['id'].blank? return if !exists?
# dont use lookup here because the cache will not # dont use lookup here because the cache will not
# know about new attributes and make crashes # know about new attributes and make crashes
@saved_only ||= payload_class.find_by(id: @payload['params']['id']) @saved_only ||= payload_class.find_by(id: @payload['params']['id'])
return @saved_only if !dump
# we use marshal here because clone still uses references and dup can't # we use marshal here because clone still uses references and dup can't
# detect changes for the rails object # detect changes for the rails object
Marshal.load(Marshal.dump(@saved_only)) Marshal.load(Marshal.dump(@saved_only))