cpr icon indicating copy to clipboard operation
cpr copied to clipboard

Optimize Base64Decode function

Open Tina-1300 opened this issue 2 months ago • 5 comments

This PR replaces the existing Base64Decode implementation with a more optimized version :

  • Pre-allocates the output string to avoid dynamic reallocations.
  • Decodes 4 Base64 characters into 3 bytes per iteration.
  • Branchless main loop for better CPU performance.
  • Handles padding '=' efficiently after the main loop.
  • Uses a static lookup table initialized once, reducing overhead on repeated calls.

Performance improvements are noticeable for large Base64 inputs.

Tina-1300 avatar Oct 25 '25 07:10 Tina-1300

@Tina-1300 thanks for submitting! I like this. Please take a look at the compilation errors. Also please use "modern" C++ features where possible like std::array.

COM8 avatar Nov 01 '25 18:11 COM8

I'll take a look. I thought the errors weren't coming from me; I thought they were from the project, that some errors had slipped through the cracks. But apparently, I must have broken something while trying to improve performance. How do I modify this PR, or rather, the code for this PR? Or do I need to create a new PR?

Tina-1300 avatar Nov 01 '25 18:11 Tina-1300

It's great to see you again. I thought the project was dead, so I'm glad @COM8 replied. How do you come up with such good library ideas? I've been racking my brain, but I can't find a problem to solve, and I'd love to be the creator of a well-known library like you, or (the creator of Laravel). I admire you a lot.

Tina-1300 avatar Nov 01 '25 18:11 Tina-1300

I'll take a look. I thought the errors weren't coming from me; I thought they were from the project, that some errors had slipped through the cracks. But apparently, I must have broken something while trying to improve performance. How do I modify this PR, or rather, the code for this PR? Or do I need to create a new PR?

Variant 1: Push another commit to your branch: https://github.com/Tina-1300/cpr/tree/perf/base64-decode Variant 2: Head over to here and press the pen icon. This allows you to edit the code. More details: https://docs.github.com/en/repositories/working-with-files/managing-files/editing-files

COM8 avatar Nov 02 '25 04:11 COM8

It's great to see you again. I thought the project was dead, so I'm glad @COM8 replied. How do you come up with such good library ideas? I've been racking my brain, but I can't find a problem to solve, and I'd love to be the creator of a well-known library like you, or (the creator of Laravel). I admire you a lot.

Thanks 😅. I have to be open with you. I did not invent this lib. I just took it over a few years ago when it basically was not maintained any more and needed some large changes.

COM8 avatar Nov 02 '25 04:11 COM8