From fb91d6f21e341f12d80e95195322837cbda78cf3 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sat, 21 Aug 2004 03:23:17 -0400 Subject: add copy-on-write semantics to amount.cc; cuts object creation by 5x --- amount.h | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'amount.h') diff --git a/amount.h b/amount.h index 2e8e526e..21d5bbfe 100644 --- a/amount.h +++ b/amount.h @@ -13,15 +13,24 @@ class commodity_t; class amount_t { - typedef void * base_type; - void _init(); void _copy(const amount_t& amt); - void _clear(); + void _release(); + void _dup(); void _resize(int prec); + void _clear() { + if (quantity) + _release(); + quantity = NULL; + commodity = NULL; + precision = 0; + } + public: - base_type quantity; // amount, to MAX_PRECISION + struct bigint_t; + + bigint_t * quantity; // amount, to MAX_PRECISION unsigned short precision; commodity_t * commodity; @@ -40,14 +49,14 @@ class amount_t if (amt.quantity) { _copy(amt); } else { - commodity = amt.commodity; - precision = amt.precision; + precision = 0; + commodity = NULL; } } - amount_t(const std::string& value) { + amount_t(const std::string& value) : quantity(NULL) { parse(value); } - amount_t(const char * value) { + amount_t(const char * value) : quantity(NULL) { parse(value); } amount_t(const bool value); @@ -58,7 +67,7 @@ class amount_t // destructor ~amount_t() { if (quantity) - _clear(); + _release(); } // assignment operator -- cgit v1.2.3