laminas / laminas-mime

Create and parse MIME messages and parts
https://docs.laminas.dev/laminas-mime/
BSD 3-Clause "New" or "Revised" License
29 stars 23 forks source link

Speed up the quoted printable encoding of big strings by avoiding the… #10

Closed pinfake closed 4 years ago

pinfake commented 4 years ago

… copy of the whole string on each iteration of the loop.

Q A
Documentation no
Bugfix no
BC Break no
New Feature no
RFC no
QA yes

Description

When you try to "encodeQuotedPrintable()" of some real BIG string (our test case was 26 Megabytes long), the loop takes forever to end because it is copying the whole thing except the already processed lines on every iteration (the substr thing at the end). We could not even make it end (running for hours). What I've done here is avoiding this copy and just use indexes where appropriate, only taking line chunks every time. My English may not be so good so, hopefully you can understand what I'm talking about in this discussion: https://codereview.stackexchange.com/questions/7220/php-substr-slow, as it is the same exact situation.

The code itself maybe subject to improvements, could have been written better... Hope it helps.

Regards.

michalbundyra commented 4 years ago

@pinfake thanks for your contribution.

We would need to add some test case, so we know that all these changes are good, and nobody change it back to "worst version" in the future.

Do you have any test we can add here? I think string 26Mb long is not what we really want here ;-)

pinfake commented 4 years ago

Ok, this is the best I could do :) As you may see it's highly dependand on raw computer power, but i think it is fair enough. I mean the whole test suite running with the "fixed" code took 269 ms on my i5. Took 2.67 minutes to fail with the original code.

Hope thats ok, if you thought of a different approach for the test, let me know!. Thanks a lot for your prompt response!

michalbundyra commented 4 years ago

Thanks, @pinfake!

pinfake commented 4 years ago

Thank you! @michalbundyra