Skip to content

RUM-6256: Optimize MD5 byte array to hex string conversion#2273

Merged
ambushwork merged 1 commit into
developfrom
yl/opt/md5-generate
Sep 20, 2024
Merged

RUM-6256: Optimize MD5 byte array to hex string conversion#2273
ambushwork merged 1 commit into
developfrom
yl/opt/md5-generate

Conversation

@ambushwork

Copy link
Copy Markdown
Member

What does this PR do?

This PR changes the way to convert MD5 bytes to HEX string.

Motivation

When we broke down the steps of generating image wireframe, we noticed that joinToString function takes a huge amount of time when generating the MD5 resource id:

Screenshot 2024-09-20 at 11 48 11

Suspicious reasons:

  • String Concatenation Overhead:
    joinToString internally uses StringBuilder, but string concatenation inside the lambda (using "%02x".format(it)) could be inefficient. If format() is invoked for each byte, it involves repeated function calls, which adds to overhead.

  • Repeated Memory Allocations:
    "%02x".format(it) creates a new string for each byte, which leads to multiple temporary objects being created. This puts pressure on the garbage collector.

Outcome

After using the new function to convert the byte arrays, we can see the duration optimisation rate is up to 99%

Span list

joinToString convertToHexString
image image

Time series

Screenshot 2024-09-20 at 11 46 55

Review checklist (to be filled by reviewers)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)

@codecov-commenter

codecov-commenter commented Sep 20, 2024

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.00%. Comparing base (a6bd6a5) to head (eca5f4d).
Report is 4 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2273      +/-   ##
===========================================
+ Coverage    69.95%   70.00%   +0.05%     
===========================================
  Files          731      732       +1     
  Lines        27182    27204      +22     
  Branches      4573     4576       +3     
===========================================
+ Hits         19013    19043      +30     
+ Misses        6885     6878       -7     
+ Partials      1284     1283       -1     
Files with missing lines Coverage Δ
...com/datadog/android/internal/utils/ByteArrayExt.kt 100.00% <100.00%> (ø)
...ay/internal/recorder/resources/MD5HashGenerator.kt 53.33% <100.00%> (ø)

... and 37 files with indirect coverage changes

@ambushwork
ambushwork marked this pull request as ready for review September 20, 2024 12:29
@ambushwork
ambushwork requested review from a team as code owners September 20, 2024 12:29

@mariusc83 mariusc83 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice job overall just a small comment regarding the tests.

ExtendWith(ForgeExtension::class)
)
@MockitoSettings(strictness = Strictness.LENIENT)
class ByteArrayExtTest {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Note

Since the goal of this is to optimize the performance of converting a bytearray to string, shouldn't we add a performance test ?

  • Make a large byte array
  • convert it to hex string using this new function, and joinToString
  • compare the performance of both methods

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Interesting point, However I am not sure the performance test should be part of the unit test, since unit test is supposed to verify correctness. Also I am afraid of performance test can be flaky, it depends also on the running environment, the test can fail even though we know in general toHexString is better than joinToString.

So do you have any suggestion how we should proceed this performance test?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I will merge it since I got enough approval, don't hesitate to add anything here, we can do it in another PR!

@ambushwork
ambushwork merged commit ce17163 into develop Sep 20, 2024
@ambushwork
ambushwork deleted the yl/opt/md5-generate branch September 20, 2024 14:55
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.

5 participants