-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Changes from 3 commits
e0f800d
22db5f2
2f53bb6
9055ce1
8be6af1
33429ae
711c480
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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; | ||
|
||
public static final StringLiteralDeduplicator INSTANCE = new StringLiteralDeduplicator(); | ||
arteam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private final Map<String, String> map = ConcurrentCollections.newConcurrentMapWithAggressiveConcurrency(); | ||
|
||
private 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
|
||
boolean cleared = false; | ||
synchronized (this) { | ||
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. I think exceeding the 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. Sure if we only use this in a limited case ++ to that, simplified as requested :) |
||
if (map.size() > MAX_SIZE) { | ||
map.clear(); | ||
cleared = true; | ||
} | ||
} | ||
if (cleared) { | ||
logger.debug("clearing intern cache"); | ||
} | ||
} | ||
map.put(interned, interned); | ||
return interned; | ||
} | ||
} |
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