Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Base64: Pre-allocate strings when encoding/decoding

  • Loading branch information...
commit 4e02fe92b80c9ef218d22d72d31be1338f5331b7 1 parent a2040c7
@garbear garbear authored
Showing with 3 additions and 0 deletions.
  1. +3 −0  xbmc/utils/Base64.cpp
View
3  xbmc/utils/Base64.cpp
@@ -35,6 +35,7 @@ void Base64::Encode(const char* input, unsigned int length, std::string &output)
long l;
output.clear();
+ output.reserve(((length + 2) / 3) * 4);
for (unsigned int i = 0; i < length; i += 3)
{
@@ -98,6 +99,8 @@ void Base64::Decode(const char* input, unsigned int length, std::string &output)
}
}
+ output.reserve(length - ((length + 2) / 4));
+
for (unsigned int i = 0; i < length; i += 4)
{
l = ((((unsigned long) m_characters.find(input[i])) & 0x3F) << 18);

2 comments on commit 4e02fe9

@garbear
Collaborator

The maths were from the for loops and can be verified by stepping different length inputs in, if that makes sense

@jmarshallnz

Math looks fine from a general 6<->8 bit encoding (3/4).

I suspect more optimisation could come from taking the conditionals out of the loops (i.e. first loop running up to length - 2), plus the second routine should probably run backwards through the string to find the length. Ofcourse, only after careful profiling that it actually makes a difference, ideally on i386 and arm.

Please sign in to comment.
Something went wrong with that request. Please try again.