diff options
author | Jiaxing Wang <hello.wjx@gmail.com> | 2016-09-15 17:39:54 +0800 |
---|---|---|
committer | Jiaxing Wang <hello.wjx@gmail.com> | 2016-09-15 17:39:54 +0800 |
commit | 159ed95f7452ab5454e9d660f6f095b8476b1eed (patch) | |
tree | d9340c4c2ca7347a24458882814dfb6bc0ca39ac /src | |
parent | a82e5d8220bbc8b5d786bed99b0876f530b9b7cc (diff) |
base58: Improve DecodeBase58 performance.
Improve DecodeBase58 performance the same way as commit 3252208 did
for EncodeBase58.
Diffstat (limited to 'src')
-rw-r--r-- | src/base58.cpp | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/base58.cpp b/src/base58.cpp index d1d60a6f1d..405b67c153 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -25,12 +25,14 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch) psz++; // Skip and count leading '1's. int zeroes = 0; + int length = 0; while (*psz == '1') { zeroes++; psz++; } // Allocate enough space in big-endian base256 representation. - std::vector<unsigned char> b256(strlen(psz) * 733 / 1000 + 1); // log(58) / log(256), rounded up. + int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up. + std::vector<unsigned char> b256(size); // Process the characters. while (*psz && !isspace(*psz)) { // Decode base58 character @@ -39,12 +41,14 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch) return false; // Apply "b256 = b256 * 58 + ch". int carry = ch - pszBase58; - for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); it != b256.rend(); it++) { + int i = 0; + for (std::vector<unsigned char>::reverse_iterator it = b256.rbegin(); (carry != 0 || i < length) && (it != b256.rend()); it++, i++) { carry += 58 * (*it); *it = carry % 256; carry /= 256; } assert(carry == 0); + length = i; psz++; } // Skip trailing spaces. @@ -53,7 +57,7 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch) if (*psz != 0) return false; // Skip leading zeroes in b256. - std::vector<unsigned char>::iterator it = b256.begin(); + std::vector<unsigned char>::iterator it = b256.begin() + (size - length); while (it != b256.end() && *it == 0) it++; // Copy result into output vector. |