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 Down Expand Up @@ -50,7 +51,7 @@ public class IndexFieldCapabilities implements Writeable {

IndexFieldCapabilities(StreamInput in) throws IOException {
this.name = in.readString();
this.type = in.readString();
this.type = StringLiteralDeduplicator.INSTANCE.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,51 @@
/*
* 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 enum StringLiteralDeduplicator {
INSTANCE;

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 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think exceeding the MAX_SIZE is unexpected. I would advocate not synchronizing in that case, since this runs on the transport thread and simply clear the map in all threads that enter this.

Copy link
Member Author

Choose a reason for hiding this comment

The 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;
}
}