-
Notifications
You must be signed in to change notification settings - Fork 1.6k
GH-3879: Add cache to optimize header match performance. #3934
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
import org.springframework.core.log.LogAccessor; | ||
import org.springframework.messaging.MessageHeaders; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.ConcurrentLruCache; | ||
import org.springframework.util.ObjectUtils; | ||
import org.springframework.util.PatternMatchUtils; | ||
|
||
|
@@ -67,6 +68,8 @@ public abstract class AbstractKafkaHeaderMapper implements KafkaHeaderMapper { | |
|
||
private final List<HeaderMatcher> multiValueHeaderMatchers = new ArrayList<>(); | ||
|
||
private final HeaderPatternMatchCache headerMatchedCache = new HeaderPatternMatchCache(); | ||
|
||
private final Map<String, Boolean> rawMappedHeaders = new HashMap<>(); | ||
|
||
{ | ||
|
@@ -272,11 +275,22 @@ protected Object headerValueToAddOut(String key, Object value) { | |
* @since 4.0 | ||
*/ | ||
protected boolean doesMatchMultiValueHeader(String headerName) { | ||
if (this.headerMatchedCache.isMultiValuePattern(headerName)) { | ||
return true; | ||
} | ||
|
||
if (this.headerMatchedCache.isSingleValuePattern(headerName)) { | ||
return false; | ||
} | ||
|
||
for (HeaderMatcher headerMatcher : this.multiValueHeaderMatchers) { | ||
if (headerMatcher.matchHeader(headerName)) { | ||
this.headerMatchedCache.cacheAsMultiValueHeader(headerName); | ||
return true; | ||
} | ||
} | ||
|
||
this.headerMatchedCache.cacheAsSingleValueHeader(headerName); | ||
return false; | ||
} | ||
|
||
|
@@ -427,4 +441,33 @@ public boolean isNegated() { | |
|
||
} | ||
|
||
/** | ||
* A Cache that remembers whether a header name matches the multi-value pattern. | ||
*/ | ||
class HeaderPatternMatchCache { | ||
|
||
private static final int MAX_SIZE = 1000; | ||
|
||
private final ConcurrentLruCache<String, Boolean> multiValueHeaderPatternMatchCache = new ConcurrentLruCache<>(MAX_SIZE, key -> Boolean.TRUE); | ||
|
||
private final ConcurrentLruCache<String, Boolean> singleValueHeaderPatternMatchCache = new ConcurrentLruCache<>(MAX_SIZE, key -> Boolean.TRUE); | ||
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. Thanks for the update . 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. The current implementation is designed to cache the result (either single-value or multi-value) for any header name that has undergone pattern matching at least once. If we only store the result for multi-value headers, then even for header names that were already evaluated as single-value, the pattern matching logic would still be executed repeatedly each time. Initially, I intended to use a single IMHO, for an effective pattern match cache, both outcomes—single-value and multi-value—should be stored. Currently, each |
||
|
||
public boolean isMultiValuePattern(String headerName) { | ||
return this.multiValueHeaderPatternMatchCache.contains(headerName); | ||
} | ||
|
||
public boolean isSingleValuePattern(String headerName) { | ||
return this.singleValueHeaderPatternMatchCache.contains(headerName); | ||
} | ||
|
||
public void cacheAsSingleValueHeader(String headerName) { | ||
this.singleValueHeaderPatternMatchCache.get(headerName); | ||
} | ||
|
||
public void cacheAsMultiValueHeader(String headerName) { | ||
this.multiValueHeaderPatternMatchCache.get(headerName); | ||
} | ||
|
||
} | ||
|
||
} |
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 is cool solution!
No days without learning something new.
Thank you! 😄
So, this has to
private static
to optimize memory consumption.And all the method not
public
.