From 3946e7a6f510ec0b13ce524c708ab73d1375d9af Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 9 May 2023 15:24:10 +0100 Subject: [PATCH] Memoize presenters in OrderInvoiceComparator This is now twice as fast and triggers half the number of database queries if both comparison methods get called --- app/services/order_invoice_comparator.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/services/order_invoice_comparator.rb b/app/services/order_invoice_comparator.rb index e6ec6cf38e..8420ad0471 100644 --- a/app/services/order_invoice_comparator.rb +++ b/app/services/order_invoice_comparator.rb @@ -13,14 +13,13 @@ class OrderInvoiceComparator # We'll use a recursive BFS algorithm to find if the invoice is outdated # the root will be the order # On each node, we'll a list of relevant attributes that will be used on the comparison - different?(current_state_invoice.presenter, latest_invoice.presenter, - invoice_generation_selector) + different?(current_state_invoice, latest_invoice, invoice_generation_selector) end def can_update_latest_invoice? return false if invoices.empty? - different?(current_state_invoice.presenter, latest_invoice.presenter, invoice_update_selector) + different?(current_state_invoice, latest_invoice, invoice_update_selector) end def different?(node1, node2, attributes_selector) @@ -65,12 +64,12 @@ class OrderInvoiceComparator private def current_state_invoice - Invoice.new( + @current_state_invoice ||= Invoice.new( order: order, data: serialize_for_invoice, date: Time.zone.today, number: invoices.count + 1 - ) + ).presenter end def invoices @@ -78,7 +77,7 @@ class OrderInvoiceComparator end def latest_invoice - invoices.last + @latest_invoice ||= invoices.last.presenter end def serialize_for_invoice