diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2016-03-21 12:03:50 +0100 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2016-03-21 12:03:59 +0100 |
commit | 7b832d286bd1606c6fa54d6bf09a994cb0d34fb5 (patch) | |
tree | fee4197e652f11870190fe40bd6b2e92aedce3dc /src | |
parent | 29e1131c4642459349a0ed4737121fb1902f8a47 (diff) | |
parent | 3252208cb10be645bae415c90fb2ed8217838490 (diff) |
Merge #7656: Improve EncodeBase58 performance
3252208 Improve EncodeBase58 performance (João Barbosa)
Diffstat (limited to 'src')
-rw-r--r-- | src/base58.cpp | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/base58.cpp b/src/base58.cpp index 5e26cf8d47..d81c26092c 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -68,26 +68,31 @@ std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend) { // Skip & count leading zeroes. int zeroes = 0; + int length = 0; while (pbegin != pend && *pbegin == 0) { pbegin++; zeroes++; } // Allocate enough space in big-endian base58 representation. - std::vector<unsigned char> b58((pend - pbegin) * 138 / 100 + 1); // log(256) / log(58), rounded up. + int size = (pend - pbegin) * 138 / 100 + 1; // log(256) / log(58), rounded up. + std::vector<unsigned char> b58(size); // Process the bytes. while (pbegin != pend) { int carry = *pbegin; + int i = 0; // Apply "b58 = b58 * 256 + ch". - for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); it != b58.rend(); it++) { + for (std::vector<unsigned char>::reverse_iterator it = b58.rbegin(); (carry != 0 || i < length) && (it != b58.rend()); it++, i++) { carry += 256 * (*it); *it = carry % 58; carry /= 58; } + assert(carry == 0); + length = i; pbegin++; } // Skip leading zeroes in base58 result. - std::vector<unsigned char>::iterator it = b58.begin(); + std::vector<unsigned char>::iterator it = b58.begin() + (size - length); while (it != b58.end() && *it == 0) it++; // Translate the result into a string. |