-
Notifications
You must be signed in to change notification settings - Fork 3k
Micro allocations optimisation in ArC #25835
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ContextDataMap.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package io.quarkus.arc.impl; | ||
|
|
||
| import io.quarkus.arc.ArcInvocationContext; | ||
| import java.lang.annotation.Annotation; | ||
| import java.util.AbstractMap; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import javax.interceptor.InvocationContext; | ||
|
|
||
| /** | ||
| * This Map implementation is specifically optimised to implement | ||
| * {@link InvocationContext#getContextData()}. | ||
| * We try to leverage the fact that in most use cases, while there is a high | ||
| * chance for integration points to need to read the content of this map, | ||
| * write operations are less likely. So we only allocate an actual underlying | ||
| * HashMap on a write operation. | ||
| * In addition, an {@link ArcInvocationContext} always allows to return the set of | ||
| * interceptor bindings via {@link ArcInvocationContext#KEY_INTERCEPTOR_BINDINGS}; | ||
| * this is implemented while avoiding a write operation on the underlying Map. | ||
| * While one goal is efficiency, another goal is safety: while the CDI specification | ||
| * allows any interceptor to make changes to this Map, we specifically disallow | ||
| * 3rd party code to make changes to the interceptor bindings of the platform. | ||
| */ | ||
| final class ContextDataMap implements Map<String, Object> { | ||
|
|
||
| private final Set<Annotation> interceptorBindings; | ||
| private HashMap<String, Object> delegate; //important to lazily initialize this | ||
|
|
||
| ContextDataMap(Set<Annotation> interceptorBindings) { | ||
| this.interceptorBindings = Objects.requireNonNull(interceptorBindings); | ||
| } | ||
|
|
||
| private Map<String, Object> getDelegateForRead() { | ||
| if (delegate == null) { | ||
| return Collections.EMPTY_MAP; | ||
| } else { | ||
| return delegate; | ||
| } | ||
| } | ||
|
|
||
| private Map<String, Object> getDelegateForWrite(final int sizeHint) { | ||
| if (delegate == null) { | ||
| this.delegate = new HashMap<>(sizeHint, 1.0f); | ||
| } | ||
| return delegate; | ||
| } | ||
|
|
||
| // ** Implement methods from the Map interface ** | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| throw new UnsupportedOperationException("Not allowed to clear the context data map"); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean containsKey(Object key) { | ||
| if (ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS.equals(key)) { | ||
| return true; | ||
| } | ||
| return getDelegateForRead().containsKey(key); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean containsValue(Object value) { | ||
| if (interceptorBindings.equals(value)) { | ||
| return true; | ||
| } | ||
| return getDelegateForRead().containsValue(value); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<Map.Entry<String, Object>> entrySet() { | ||
| final AbstractMap.SimpleImmutableEntry<String, Object> firstEntry = new AbstractMap.SimpleImmutableEntry<>( | ||
| ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS, interceptorBindings); | ||
| if (delegate == null) { | ||
| return Collections.singleton(firstEntry); | ||
| } else { | ||
| HashSet entries = new HashSet(delegate.size() + 1); | ||
| entries.addAll(delegate.entrySet()); | ||
| entries.add(firstEntry); | ||
| return entries; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Object get(Object key) { | ||
| if (ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS.equals(key)) { | ||
| return interceptorBindings; | ||
| } | ||
| return getDelegateForRead().get(key); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEmpty() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> keySet() { | ||
| if (delegate == null) { | ||
| return Collections.singleton(ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS); | ||
| } else { | ||
| HashSet set = new HashSet(delegate.size() + 1); | ||
| set.addAll(delegate.keySet()); | ||
| set.add(ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS); | ||
| return set; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void putAll(Map m) { | ||
| if (m.containsKey(ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS)) { | ||
| throw new IllegalArgumentException( | ||
| "Not allowed to put key '" + ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS + "' in the context data map"); | ||
| } | ||
| getDelegateForWrite(m.size()).putAll(m); | ||
| } | ||
|
|
||
| @Override | ||
| public Object remove(Object key) { | ||
| if (ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS.equals(key)) { | ||
| throw new IllegalArgumentException("Not allowed to remove key '" + ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS | ||
| + "' from the context data map"); | ||
| } else if (delegate == null) { | ||
| return null; | ||
| } else { | ||
| return delegate.remove(key); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return getDelegateForRead().size() + 1; | ||
| } | ||
|
|
||
| @Override | ||
| public Collection values() { | ||
Sanne marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to use so many raw types in this class? I get a lot of useless warnings in my IDE ;-) |
||
| if (delegate == null) { | ||
| return Collections.singleton(interceptorBindings); | ||
| } else { | ||
| ArrayList list = new ArrayList(delegate.size() + 1); | ||
| list.addAll(delegate.values()); | ||
| list.add(interceptorBindings); | ||
| return list; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Object put(String key, Object value) { | ||
| if (ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS.equals(key)) { | ||
manovotn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new IllegalArgumentException( | ||
| "Not allowed to put key '" + ArcInvocationContext.KEY_INTERCEPTOR_BINDINGS + "' in the context data map"); | ||
| } | ||
| return getDelegateForWrite(1).put(key, value); | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be lazily allocated (and reused) as well, given that users cannot modify it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes it could - but I don't think it's worth it: hopefully nobody is using
entrySetheavily on this map.