Skip to content

Commit 48dc53f

Browse files
Make PathTrieIterator a Little more Memory Efficient (elastic#44951) (elastic#45070)
* There's no need to have the trie iterator hold another reference to the request object (which could be huge, see elastic#44564) * Also removed unused boolean field from trie node
1 parent 187f1e7 commit 48dc53f

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

server/src/main/java/org/elasticsearch/common/path/PathTrie.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ enum TrieMatchingMode {
5757
WILDCARD_NODES_ALLOWED
5858
}
5959

60-
static EnumSet<TrieMatchingMode> EXPLICIT_OR_ROOT_WILDCARD =
60+
private static final EnumSet<TrieMatchingMode> EXPLICIT_OR_ROOT_WILDCARD =
6161
EnumSet.of(TrieMatchingMode.EXPLICIT_NODES_ONLY, TrieMatchingMode.WILDCARD_ROOT_NODES_ALLOWED);
6262

6363
public interface Decoder {
@@ -79,17 +79,15 @@ public PathTrie(Decoder decoder) {
7979
public class TrieNode {
8080
private transient String key;
8181
private transient T value;
82-
private boolean isWildcard;
8382
private final String wildcard;
8483

8584
private transient String namedWildcard;
8685

8786
private Map<String, TrieNode> children;
8887

89-
public TrieNode(String key, T value, String wildcard) {
88+
private TrieNode(String key, T value, String wildcard) {
9089
this.key = key;
9190
this.wildcard = wildcard;
92-
this.isWildcard = (key.equals(wildcard));
9391
this.value = value;
9492
this.children = emptyMap();
9593
if (isNamedWildcard(key)) {
@@ -99,7 +97,7 @@ public TrieNode(String key, T value, String wildcard) {
9997
}
10098
}
10199

102-
public void updateKeyWithNamedWildcard(String key) {
100+
private void updateKeyWithNamedWildcard(String key) {
103101
this.key = key;
104102
namedWildcard = key.substring(key.indexOf('{') + 1, key.indexOf('}'));
105103
}
@@ -110,7 +108,7 @@ private void addInnerChild(String key, TrieNode child) {
110108
children = unmodifiableMap(newChildren);
111109
}
112110

113-
public synchronized void insert(String[] path, int index, T value) {
111+
private synchronized void insert(String[] path, int index, T value) {
114112
if (index >= path.length)
115113
return;
116114

@@ -145,7 +143,7 @@ public synchronized void insert(String[] path, int index, T value) {
145143
node.insert(path, index + 1, value);
146144
}
147145

148-
public synchronized void insertOrUpdate(String[] path, int index, T value, BiFunction<T, T, T> updater) {
146+
private synchronized void insertOrUpdate(String[] path, int index, T value, BiFunction<T, T, T> updater) {
149147
if (index >= path.length)
150148
return;
151149

@@ -357,14 +355,14 @@ public Iterator<T> retrieveAll(String path, Supplier<Map<String, String>> paramS
357355
return new PathTrieIterator<>(this, path, paramSupplier);
358356
}
359357

360-
class PathTrieIterator<T> implements Iterator<T> {
358+
private static class PathTrieIterator<T> implements Iterator<T> {
361359

362360
private final List<TrieMatchingMode> modes;
363361
private final Supplier<Map<String, String>> paramSupplier;
364362
private final PathTrie<T> trie;
365363
private final String path;
366364

367-
PathTrieIterator(PathTrie trie, String path, Supplier<Map<String, String>> paramSupplier) {
365+
PathTrieIterator(PathTrie<T> trie, String path, Supplier<Map<String, String>> paramSupplier) {
368366
this.path = path;
369367
this.trie = trie;
370368
this.paramSupplier = paramSupplier;

server/src/main/java/org/elasticsearch/rest/RestController.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,12 @@ Iterator<MethodHandlers> getAllHandlers(final RestRequest request) {
363363
// Between retrieving the correct path, we need to reset the parameters,
364364
// otherwise parameters are parsed out of the URI that aren't actually handled.
365365
final Map<String, String> originalParams = new HashMap<>(request.params());
366+
final Map<String, String> requestParamsRef = request.params();
366367
return handlers.retrieveAll(getPath(request), () -> {
367368
// PathTrie modifies the request, so reset the params between each iteration
368-
request.params().clear();
369-
request.params().putAll(originalParams);
370-
return request.params();
369+
requestParamsRef.clear();
370+
requestParamsRef.putAll(originalParams);
371+
return requestParamsRef;
371372
});
372373
}
373374

0 commit comments

Comments
 (0)