From ef8f8bdcd79a78ca4cd87e7c05eb546a585b2abd Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sat, 26 Dec 2020 23:47:07 +0800 Subject: [PATCH] Reports: don't use gnc:html-table-append-column! This function is inefficient. Each column appended will require scanning every row of existing html-table, scanning the row's elements and appending the desired data. It is much more efficient to build a html-table by append rows instead -- html-table rows are stored in reverse, and each appended row is built using (cons newrow existing-rows). --- .../report/reports/example/hello-world.scm | 6 ++-- .../reports/standard/cashflow-barchart.scm | 31 +++++++++---------- .../report/reports/standard/net-charts.scm | 31 ++++++++----------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/gnucash/report/reports/example/hello-world.scm b/gnucash/report/reports/example/hello-world.scm index 59783c3afa..9e73e77716 100644 --- a/gnucash/report/reports/example/hello-world.scm +++ b/gnucash/report/reports/example/hello-world.scm @@ -427,8 +427,10 @@ new, totally cool report, consult the mailing list ~a.") (if (not (null? list-val)) (let ((table (gnc:make-html-table))) - (gnc:html-table-append-column! - table (map symbol->string list-val)) + (for-each + (lambda (cell) + (gnc:html-table-append-row! table (list (symbol->string cell)))) + list-val) (gnc:html-table-set-style! table "table" 'attribute (list "style" "width:200px")) (gnc:html-table-set-caption! table diff --git a/gnucash/report/reports/standard/cashflow-barchart.scm b/gnucash/report/reports/standard/cashflow-barchart.scm index 9f0e0bf1b3..ad9d12b535 100644 --- a/gnucash/report/reports/standard/cashflow-barchart.scm +++ b/gnucash/report/reports/standard/cashflow-barchart.scm @@ -35,6 +35,7 @@ (use-modules (gnucash app-utils)) (use-modules (gnucash reports cash-flow-calc)) (use-modules (gnucash report)) +(use-modules (srfi srfi-26)) (define reportname (N_ "Cash Flow Barchart")) @@ -327,6 +328,17 @@ (if (and non-zeros show-table?) (let* ((table (gnc:make-html-table))) + + (define (add-row date in out net) + (gnc:html-table-append-row! + table + (cons date + (map (cut gnc:make-html-table-cell/markup "number-cell" <>) + (append + (if show-in? (list in) '()) + (if show-out? (list out) '()) + (if show-net? (list net) '())))))) + (gnc:html-table-set-col-headers! table (append (list (G_ "Date")) (if show-in? (list (G_ "Money In")) '()) @@ -335,22 +347,9 @@ (gnc:html-document-add-object! doc (gnc:make-html-text (gnc:html-markup-h3 (G_ "Overview:")))) - (gnc:html-table-append-column! table (append date-string-list (list "Total"))) - - (if show-in? - (gnc:html-table-append-column! table (append in-list (list total-in)))) - (if show-out? - (gnc:html-table-append-column! table (append out-list (list total-out)))) - (if show-net? - (gnc:html-table-append-column! table (append net-list (list total-net)))) - - ;; set numeric columns to align right - (for-each - (lambda (col) - (gnc:html-table-set-col-style! - table col "td" - 'attribute (list "class" "number-cell"))) - '(1 2 3)) + + (for-each add-row date-string-list in-list out-list net-list) + (add-row (G_ "Total") total-in total-out total-net) (gnc:html-document-add-object! doc table)))) diff --git a/gnucash/report/reports/standard/net-charts.scm b/gnucash/report/reports/standard/net-charts.scm index 71d8b38b36..1d765bb541 100644 --- a/gnucash/report/reports/standard/net-charts.scm +++ b/gnucash/report/reports/standard/net-charts.scm @@ -229,11 +229,7 @@ ;; This exchanges the commodity-collector 'c' to one single ;; 'report-currency' according to the exchange-fn. Returns a gnc:monetary (define (collector->monetary c date) - (if (not (number? date)) - (throw 'wrong)) - (gnc:sum-collector-commodity - c report-currency - (lambda (a b) (exchange-fn a b date)))) + (gnc:sum-collector-commodity c report-currency (cut exchange-fn <> <> date))) ;; gets an account alist balances ;; output: (list acc bal0 bal1 bal2 ...) @@ -425,21 +421,20 @@ (list (G_ "Net Profit")) (list (G_ "Net Worth"))) '()))) - (gnc:html-table-append-column! table date-string-list) - (when show-sep? - (gnc:html-table-append-column! table minuend-balances) - (gnc:html-table-append-column! table subtrahend-balances)) - (if show-net? - (gnc:html-table-append-column! table difference-balances)) - - ;; set numeric columns to align right (for-each - (lambda (col) - (gnc:html-table-set-col-style! - table col "td" - 'attribute (list "class" "number-cell"))) - '(1 2 3)) + (lambda (date minuend subtrahend difference) + (gnc:html-table-append-row! + table + (cons date + (map + (cut gnc:make-html-table-cell/markup "number-cell" <>) + (append (if show-sep? (list minuend subtrahend) '()) + (if show-net? (list difference) '())))))) + date-string-list + minuend-balances + subtrahend-balances + difference-balances) (gnc:html-document-add-object! document table)))