-
Notifications
You must be signed in to change notification settings - Fork 25.1k
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
Intern IndexFieldCapabilities Type String on Read #76405
Merged
original-brownbear
merged 7 commits into
elastic:master
from
original-brownbear:intern-index-field-caps-type-on-deserialization
Sep 15, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e0f800d
Intern IndexFieldCapabilities Type String on Read
original-brownbear 22db5f2
Merge remote-tracking branch 'elastic/master' into intern-index-field…
original-brownbear 2f53bb6
more sophisticated approach
original-brownbear 9055ce1
Merge remote-tracking branch 'elastic/master' into intern-index-field…
original-brownbear 8be6af1
shorter singleton
original-brownbear 33429ae
Merge remote-tracking branch 'elastic/master' into intern-index-field…
original-brownbear 711c480
CR: simpler
original-brownbear 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 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
45 changes: 45 additions & 0 deletions
45
server/src/main/java/org/elasticsearch/common/util/StringLiteralDeduplicator.java
This file contains 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,45 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
package org.elasticsearch.common.util; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.common.util.concurrent.ConcurrentCollections; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* A cache in front of Java's string interning. This method assumes that it is only called with strings that are already part of the | ||
* JVM's string pool so that interning them does not grow the pool. Calling it with strings not in the interned string pool is not | ||
* advisable as its performance may deteriorate to slower than outright calls to {@link String#intern()}. | ||
*/ | ||
public final class StringLiteralDeduplicator { | ||
|
||
private static final Logger logger = LogManager.getLogger(StringLiteralDeduplicator.class); | ||
|
||
private static final int MAX_SIZE = 1000; | ||
|
||
private final Map<String, String> map = ConcurrentCollections.newConcurrentMapWithAggressiveConcurrency(); | ||
|
||
public StringLiteralDeduplicator() { | ||
} | ||
|
||
public String deduplicate(String string) { | ||
final String res = map.get(string); | ||
if (res != null) { | ||
return res; | ||
} | ||
final String interned = string.intern(); | ||
arteam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (map.size() > MAX_SIZE) { | ||
arteam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
map.clear(); | ||
logger.debug("clearing intern cache"); | ||
} | ||
map.put(interned, interned); | ||
return interned; | ||
} | ||
} |
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 will start thrashing once we exceed 1000 strings. I worry about the reuse of this
INSTANCE
for other purposes. Can we instead make a single instance that is used solely for the field capabilitiestype
field by making a private static instance in theIndexFieldCapabilities
class? If other needs arise for this deduplicator, those would thereby be separated completely.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.
++ made it a private static cache for now just limited to this class