Merge #7656: Improve EncodeBase58 performance
3252208
Improve EncodeBase58 performance (João Barbosa)
This commit is contained in:
commit
7b832d286b
1 changed files with 8 additions and 3 deletions
|
@ -68,26 +68,31 @@ std::string EncodeBase58(const unsigned char* pbegin, const unsigned char* pend)
|
||||||
{
|
{
|
||||||
// Skip & count leading zeroes.
|
// Skip & count leading zeroes.
|
||||||
int zeroes = 0;
|
int zeroes = 0;
|
||||||
|
int length = 0;
|
||||||
while (pbegin != pend && *pbegin == 0) {
|
while (pbegin != pend && *pbegin == 0) {
|
||||||
pbegin++;
|
pbegin++;
|
||||||
zeroes++;
|
zeroes++;
|
||||||
}
|
}
|
||||||
// Allocate enough space in big-endian base58 representation.
|
// 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.
|
// Process the bytes.
|
||||||
while (pbegin != pend) {
|
while (pbegin != pend) {
|
||||||
int carry = *pbegin;
|
int carry = *pbegin;
|
||||||
|
int i = 0;
|
||||||
// Apply "b58 = b58 * 256 + ch".
|
// 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);
|
carry += 256 * (*it);
|
||||||
*it = carry % 58;
|
*it = carry % 58;
|
||||||
carry /= 58;
|
carry /= 58;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(carry == 0);
|
assert(carry == 0);
|
||||||
|
length = i;
|
||||||
pbegin++;
|
pbegin++;
|
||||||
}
|
}
|
||||||
// Skip leading zeroes in base58 result.
|
// 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)
|
while (it != b58.end() && *it == 0)
|
||||||
it++;
|
it++;
|
||||||
// Translate the result into a string.
|
// Translate the result into a string.
|
||||||
|
|
Loading…
Reference in a new issue