Skip to content

Commit

Permalink
[balance-forecast] refactor to avoid xaccAccountGetBalanceAsOfDate
Browse files Browse the repository at this point in the history
previous will call gnc:account-get-comm-balance-at-date which calls
xaccAccountGetBalanceAsOfDate for every account at every date
point. The xaccAccountGetBalanceAsOfDate is an expensive function
because it scans the account's whole splitlist from the start every
time. use gnc:account-get-balances-at-dates instead which scans an
account only once to generate a balancelist.

this should be a much faster chart.
  • Loading branch information
christopherlam committed Mar 14, 2019
1 parent 1ade6fe commit 6e76cb3
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions gnucash/report/standard-reports/balance-forecast.scm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

(use-modules (gnucash gnc-module))
(use-modules (gnucash gettext))
(use-modules (srfi srfi-1))

(gnc:module-load "gnucash/report/report-system" 0)

Expand Down Expand Up @@ -142,6 +143,13 @@
(intervals (gnc:make-date-interval-list
from-date to-date (gnc:deltasym-to-delta interval)))
(accum (gnc:make-commodity-collector))
(accounts-balancelist
(map
(lambda (acc)
(gnc:account-get-balances-at-dates acc (map cadr intervals)))
accounts))
(accounts-balancelist-transposed
(if (null? accounts) '() (apply zip accounts-balancelist)))
(balances #f)
)

Expand All @@ -162,23 +170,23 @@

; Calculate balances
(set! balances
(map (lambda (date) (let* (
(map (lambda (date accounts-balance) (let* (
(start-date (car date))
(end-date (cadr date))
(balance (gnc:make-commodity-collector))
(exchange-fn (gnc:case-exchange-fn price currency end-date))
(sx-value (gnc-sx-all-instantiate-cashflow-all start-date end-date))
)
(for-each (lambda (account)
(for-each (lambda (account account-balance)
(accum 'add (xaccAccountGetCommodity account)
(hash-ref sx-value (gncAccountGetGUID account) 0))
(balance 'merge
(gnc:account-get-comm-balance-at-date account end-date #f) #f)
) accounts)
(balance 'add (gnc:gnc-monetary-commodity account-balance)
(gnc:gnc-monetary-amount account-balance))
) accounts accounts-balance)
(balance 'merge accum #f)
(gnc:gnc-monetary-amount
(gnc:sum-collector-commodity balance currency exchange-fn))
)) intervals))
)) intervals accounts-balancelist-transposed))

; Minimum line
(when show-minimum
Expand Down

0 comments on commit 6e76cb3

Please sign in to comment.