Skip to content
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

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.util.StringLiteralDeduplicator;

import java.io.IOException;
import java.util.Map;
Expand All @@ -21,6 +22,8 @@
*/
public class IndexFieldCapabilities implements Writeable {

private static final StringLiteralDeduplicator typeStringDeduplicator = new StringLiteralDeduplicator();

private final String name;
private final String type;
private final boolean isMetadatafield;
Expand Down Expand Up @@ -50,7 +53,7 @@ public class IndexFieldCapabilities implements Writeable {

IndexFieldCapabilities(StreamInput in) throws IOException {
this.name = in.readString();
this.type = in.readString();
this.type = typeStringDeduplicator.deduplicate(in.readString());
this.isMetadatafield = in.readBoolean();
this.isSearchable = in.readBoolean();
this.isAggregatable = in.readBoolean();
Expand Down
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;
Copy link
Contributor

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 capabilities type field by making a private static instance in the IndexFieldCapabilities class? If other needs arise for this deduplicator, those would thereby be separated completely.

Copy link
Member Author

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


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();
if (map.size() > MAX_SIZE) {
map.clear();
logger.debug("clearing intern cache");
}
map.put(interned, interned);
return interned;
}
}