Skip to content

perf: optimize copy on write attributes #74

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yuzawa-san
Copy link

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

Related issues

none

Describe the solution you've provided

i have modified the copy on write mechanism for custom attributes to avoid having to copy the whole attribute map. instead i chain to the parent map. this does not change the public API or outwards behavior. the implementation was complicated because of the need to support the "remove" operation. additionally, i leveraged the equals and hashCode of Map. note: hashCode from Map is not order dependent since it uses addition across all of the entries and addition is commutative.

Describe alternatives you've considered

i was also playing around with a lazy LDValue provider such that we do not need to create all of the attributes if we do not use them but that seemed like a lot of rework and hard to do in a codebase currently targeting jdk 7.

Additional context

i was doing flame graph analysis and found that there were a lot of copies of the hash maps going on, so i thought i could try to eliminate those copies. my use case involves the case in the new test case i added, but it is located in a hotspot in my codebase. the parent context has dozens of attributes and is shared across multiple evaluations. however the child contexts add on only a handful of attributes. the current implementation was copying over all of the attributes for each new build since the parent context was tainted. this root/parent chaining and reuse is a common paradigm over the codebase

controller(){
 LDContext rootContext = LDContext.builder(key).set(...).set(...)......build();
 ...
 if (ldClient.boolVariation(rootContext, ...)) { ... }
 ...
 for(a lot of iterations) {
  LDContext innerContext = LDContext.builderFromContext(rootContext).set(....).build()
  if(ldClient.boolVariation(innerContext, ...)) { ... }
  // LDContext.builderFromContext(innerContext) may be called as well
  ...
 }
 ...
}

@yuzawa-san yuzawa-san requested a review from a team as a code owner June 18, 2025 01:41
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.

1 participant