From 7848dbd7f76e580760b585eba3f67cf3a4ebaed9 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sat, 26 Jul 2008 23:54:35 -0400 Subject: [PATCH 1/3] Increased binary cache file internal version number. --- binary.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/binary.cc b/binary.cc index 9185f766..295fc1bc 100644 --- a/binary.cc +++ b/binary.cc @@ -11,9 +11,9 @@ namespace ledger { static unsigned long binary_magic_number = 0xFFEED765; #ifdef DEBUG_ENABLED -static unsigned long format_version = 0x0002060b; +static unsigned long format_version = 0x0002060d; #else -static unsigned long format_version = 0x0002060a; +static unsigned long format_version = 0x0002060c; #endif static account_t ** accounts; From 9b7725ee181617b2e0ea13189837b0724ab964b8 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sat, 26 Jul 2008 23:55:06 -0400 Subject: [PATCH 2/3] Added a simple optimization to the way amount strings are parsed. --- amount.cc | 14 ++++++++++---- amount.h | 5 +++-- valexpr.cc | 32 ++++++++++++-------------------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/amount.cc b/amount.cc index 1ac2f407..3a10828c 100644 --- a/amount.cc +++ b/amount.cc @@ -1062,7 +1062,7 @@ void parse_annotations(std::istream& in, amount_t& price, << " tag " << tag); } -void amount_t::parse(std::istream& in, unsigned char flags) +bool amount_t::parse(std::istream& in, unsigned char flags) { // The possible syntax for an amount is: // @@ -1114,8 +1114,12 @@ void amount_t::parse(std::istream& in, unsigned char flags) } } - if (quant.empty()) - throw new amount_error("No quantity specified for amount"); + if (quant.empty()) { + if (flags & AMOUNT_PARSE_SOFT_FAIL) + return false; + else + throw new amount_error("No quantity specified for amount"); + } _init(); @@ -1205,6 +1209,8 @@ void amount_t::parse(std::istream& in, unsigned char flags) if (! (flags & AMOUNT_PARSE_NO_REDUCE)) reduce(); + + return true; } void amount_t::reduce() @@ -1215,7 +1221,7 @@ void amount_t::reduce() } } -void amount_t::parse(const std::string& str, unsigned char flags) +bool amount_t::parse(const std::string& str, unsigned char flags) { std::istringstream stream(str); parse(stream, flags); diff --git a/amount.h b/amount.h index 3df04c1d..ef1d2a3d 100644 --- a/amount.h +++ b/amount.h @@ -263,9 +263,10 @@ class amount_t #define AMOUNT_PARSE_NO_MIGRATE 0x01 #define AMOUNT_PARSE_NO_REDUCE 0x02 +#define AMOUNT_PARSE_SOFT_FAIL 0x04 - void parse(std::istream& in, unsigned char flags = 0); - void parse(const std::string& str, unsigned char flags = 0); + bool parse(std::istream& in, unsigned char flags = 0); + bool parse(const std::string& str, unsigned char flags = 0); void reduce(); amount_t reduced() const { diff --git a/valexpr.cc b/valexpr.cc index 5b5d29ba..4fed821e 100644 --- a/valexpr.cc +++ b/valexpr.cc @@ -772,29 +772,21 @@ value_expr_t * parse_value_term(std::istream& in, scope_t * scope, // When in relaxed parsing mode, we do want to migrate commodity // flags, so that any precision specified by the user updates // the current maximum precision displayed. - try { - pos = (long)in.tellg(); + pos = (long)in.tellg(); - unsigned char parse_flags = 0; - if (flags & PARSE_VALEXPR_NO_MIGRATE) - parse_flags |= AMOUNT_PARSE_NO_MIGRATE; - if (flags & PARSE_VALEXPR_NO_REDUCE) - parse_flags |= AMOUNT_PARSE_NO_REDUCE; + unsigned char parse_flags = 0; + if (flags & PARSE_VALEXPR_NO_MIGRATE) + parse_flags |= AMOUNT_PARSE_NO_MIGRATE; + if (flags & PARSE_VALEXPR_NO_REDUCE) + parse_flags |= AMOUNT_PARSE_NO_REDUCE; - temp.parse(in, parse_flags); - } - catch (amount_error * err) { - // If the amount had no commodity, it must be an unambiguous - // variable reference - if (std::strcmp(err->what(), "No quantity specified for amount") == 0) { - in.clear(); - in.seekg(pos, std::ios::beg); - c = prev_c; - goto parse_ident; - } else { - throw err; - } + if (! temp.parse(in, parse_flags | AMOUNT_PARSE_SOFT_FAIL)) { + in.clear(); + in.seekg(pos, std::ios::beg); + c = prev_c; + goto parse_ident; } + node.reset(new value_expr_t(value_expr_t::CONSTANT)); node->value = new value_t(temp); goto parsed; From e5a8bbf997f14ea2edf52d6dfe733969cc67589b Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sat, 26 Jul 2008 23:55:17 -0400 Subject: [PATCH 3/3] Another quick optimization that cuts down on memory allocation. --- walk.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/walk.h b/walk.h index 3eedbae9..bd6bc2c2 100644 --- a/walk.h +++ b/walk.h @@ -145,9 +145,11 @@ transaction_xdata_t& transaction_xdata(const transaction_t& xact); void add_transaction_to(const transaction_t& xact, value_t& value); inline account_t * xact_account(transaction_t& xact) { - account_t * account = transaction_xdata(xact).account; - if (account) - return account; + if (xact.data) { + account_t * account = transaction_xdata(xact).account; + if (account) + return account; + } return xact.account; }