Jan,

On Thu, Jun 23, 2011 at 9:51 PM, <jan@uos.de> wrote:
Hi there!

Instawallet has enjoyed steady growth and I'm running into a bottleneck
now with "getbalance <someaccounthere>" taking quite some time to
complete. My understanding is, that this is because bitcoind runs
through all relevant transactions each time anew to compute the balance.
I was hoping the list could give me some pointers/ideas on how I can
improve this.

I think the easiest way to speed this up would be to scan the wallet every time a block comes in or something else changes in the block chain (or, if you prefer, some pre-set interval of N minutes). Then go over the entire wallet and the accumulate balances for all accounts. This could be done in amortized linear time using a hash_map.

1) This reduces the time the API takes to return the balance for an account to a predictable, very short time. Just the time to look up the balance in the hash table (and return 0 on miss). The number crunching happens in the network thread, not while you're waiting on the API.

2) Less bug-prone than "incremental caching" as you propose, and doesn't require determining which accounts are influenced by a new block

3) Block chain reorgs are no problem.

JS