Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email 198 performance in image html email #239

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

seanfabs
Copy link

@seanfabs seanfabs commented Apr 19, 2024

There are several fixes and improvements to the regex's here:

  1. The regex could run with exponential time in the \\s*[^>]*?\\s+ part, this is because the \s, [^>], and \\s+ parts can all match whitespace and so when excessive whitespace is encountered it will attempt all combinations to find a match. This is a fairly well documented phenomenon https://blog.codinghorror.com/regex-performance/
  2. The <script> tag matching did not match multiple scripts.
  3. Fix the edge case where tags could match which started with img or script e.g. <imgx ...>
  4. Use a greedy matcher for the src url [^\"']+ which gives a slight performance boost - we will always want to grab the whole url.

One question is should I also update the similar class in the javax package?

Benchmark results

VM version: JDK 17.0.10, OpenJDK 64-Bit Server VM, 17.0.10+8-b1207.12

Result for old regex (<[Ii][Mm][Gg]\s*[^>]*?\s+[Ss][Rr][Cc]\s*=\s*["'])([^"']+?)(["'])

32.339 ±(99.9%) 4.394 ops/s [Average]
(min, avg, max) = (20.590, 32.339, 40.601), stdev = 5.866
CI (99.9%): [27.945, 36.733] (assumes normal distribution)

Result for new regex - candidate non greedy url (<[Ii][Mm][Gg](?=\s)[^>]*?\s[Ss][Rr][Cc]\s*=\s*["'])([^"']+?)(["'])

1244.602 ±(99.9%) 225.819 ops/s [Average]
(min, avg, max) = (518.410, 1244.602, 1512.089), stdev = 301.462
CI (99.9%): [1018.783, 1470.420] (assumes normal distribution)

Result for new regex (<[Ii][Mm][Gg](?=\s)[^>]*?\s[Ss][Rr][Cc]\s*=\s*["'])([^"']+)(["'])

1480.002 ±(99.9%) 160.408 ops/s [Average]
(min, avg, max) = (986.366, 1480.002, 1651.095), stdev = 214.140
CI (99.9%): [1319.594, 1640.409] (assumes normal distribution)

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @seanfabs
Thank you for your PR. Please see my comments.

@garydgregory
Copy link
Member

Hello @seanfabs
I can't reproduce these results unless a benchmark run executes tests with old and new code. You can usually do this by copying old code into a benchmark test method so that you have two methods: One tests the new patched main code and the old runs the old code now living in the benchmark; in this case, it's not much code since these are REs. Please rebase on git master to pick up the new Maven I added to facilitate running benchmarks from the command line like mvn -P benchmark -Dbenchmark=ImageHtmlEmailBenchmark -pl commons-email2-jakarta.
TY!

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seanfabs
Please see my comment.

@seanfabs
Copy link
Author

Thanks for the suggestion, changes made

@seanfabs seanfabs force-pushed the email-198-Performance-in-ImageHtmlEmail branch from 67a88f4 to 59a9c16 Compare May 9, 2024 11:39
@seanfabs seanfabs requested a review from garydgregory May 9, 2024 11:59
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.71%. Comparing base (0bd7e34) to head (59a9c16).
Report is 140 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #239      +/-   ##
============================================
+ Coverage     74.34%   76.71%   +2.36%     
- Complexity      353      635     +282     
============================================
  Files            22       39      +17     
  Lines          1072     1915     +843     
  Branches        126      233     +107     
============================================
+ Hits            797     1469     +672     
- Misses          216      345     +129     
- Partials         59      101      +42     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants