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
end
def selected
if @payload['params']['id'] && payload_class.exists?(id: @payload['params']['id'])
result = saved_only
def exists?
return if @payload['params']['id'].blank?
@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)
end
end
def selected
if exists?
overwritten
else
selected_only
end
end
def saved_only
return if @payload['params']['id'].blank?
def saved_only(dump: false)
return if !exists?
# dont use lookup here because the cache will not
# know about new attributes and make crashes
@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
# detect changes for the rails object
Marshal.load(Marshal.dump(@saved_only))