Skip to content

HelperInjector optimization#1126

Merged
randomanderson merged 1 commit into
masterfrom
landerson/helper-injector-optimization
Dec 5, 2019
Merged

HelperInjector optimization#1126
randomanderson merged 1 commit into
masterfrom
landerson/helper-injector-optimization

Conversation

@randomanderson
Copy link
Copy Markdown
Contributor

This pull request makes some changes to HelperInjector to reduce memory usage, increase performance, and decrease the chance of deadlocks.

Removes helperMap to save memory

The helperMap was an injector-local cache of the byte[] representation of an instrumentation's helper classes. Since each instrumentation has its own HelperInjector, each instrumentation had its own helperMap. These arrays were never reclaimable by GC.

The performance impact of removing this cache should be negligible because:

  • the helper classes are still only loaded once per target classloader
  • the DatadogClassloader has its own internal class cache

Module logic improvement

There is only one unnamed module per classloader. The new code takes advantage of this fact.

Removes synchronization block

Internally, bytebuddy locks the classloader being injected. The synchronization block meant there was potential for deadlock:

  • Thread A: Has HelperInjector lock, is waiting for Classloader lock
  • Thread B: Has Classloader lock, is waiting for HelperInjector lock

While unlikely because of when injection usually happens, it was still possible. Additionally, synchronization was only necessary because of the HelperMap caching. As the cache was removed, synchronization could also be removed.

Using injectRaw() instead of inject()

Internal to ByteBuddy, inject() calls injectRaw by copying the classmap. Unintentionally, some instrumentation code paths converted this map 3-4 times. Using injectRaw() avoids this problem.

@randomanderson randomanderson requested a review from a team as a code owner December 4, 2019 22:34
@randomanderson randomanderson added this to the 0.39.0 milestone Dec 5, 2019
@randomanderson randomanderson added the tag: performance Performance related changes label Dec 5, 2019
@randomanderson randomanderson merged commit f77cce0 into master Dec 5, 2019
@randomanderson randomanderson deleted the landerson/helper-injector-optimization branch December 6, 2019 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tag: performance Performance related changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants