Permalink
Browse files

Base64: Pre-allocate strings when encoding/decoding

  • Loading branch information...
garbear committed Oct 14, 2012
1 parent a2040c7 commit 4e02fe92b80c9ef218d22d72d31be1338f5331b7
Showing with 3 additions and 0 deletions.
  1. +3 −0 xbmc/utils/Base64.cpp
View
@@ -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

This comment has been minimized.

Show comment
Hide comment
@garbear

garbear Oct 14, 2012

Member

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

Member

garbear replied Oct 14, 2012

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

@jmarshallnz

This comment has been minimized.

Show comment
Hide comment
@jmarshallnz

jmarshallnz Oct 14, 2012

Member

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.

Member

jmarshallnz replied Oct 14, 2012

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.