From 48dc53f8d2cf67a41efea6073a89f802b8c16711 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Thu, 1 Aug 2019 17:26:08 +0200 Subject: [PATCH] Make PathTrieIterator a Little more Memory Efficient (#44951) (#45070) * There's no need to have the trie iterator hold another reference to the request object (which could be huge, see #44564) * Also removed unused boolean field from trie node --- .../org/elasticsearch/common/path/PathTrie.java | 16 +++++++--------- .../org/elasticsearch/rest/RestController.java | 7 ++++--- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/path/PathTrie.java b/server/src/main/java/org/elasticsearch/common/path/PathTrie.java index 08787cea9df..8f6abc3102f 100644 --- a/server/src/main/java/org/elasticsearch/common/path/PathTrie.java +++ b/server/src/main/java/org/elasticsearch/common/path/PathTrie.java @@ -57,7 +57,7 @@ public class PathTrie { WILDCARD_NODES_ALLOWED } - static EnumSet EXPLICIT_OR_ROOT_WILDCARD = + private static final EnumSet EXPLICIT_OR_ROOT_WILDCARD = EnumSet.of(TrieMatchingMode.EXPLICIT_NODES_ONLY, TrieMatchingMode.WILDCARD_ROOT_NODES_ALLOWED); public interface Decoder { @@ -79,17 +79,15 @@ public class PathTrie { public class TrieNode { private transient String key; private transient T value; - private boolean isWildcard; private final String wildcard; private transient String namedWildcard; private Map children; - public TrieNode(String key, T value, String wildcard) { + private TrieNode(String key, T value, String wildcard) { this.key = key; this.wildcard = wildcard; - this.isWildcard = (key.equals(wildcard)); this.value = value; this.children = emptyMap(); if (isNamedWildcard(key)) { @@ -99,7 +97,7 @@ public class PathTrie { } } - public void updateKeyWithNamedWildcard(String key) { + private void updateKeyWithNamedWildcard(String key) { this.key = key; namedWildcard = key.substring(key.indexOf('{') + 1, key.indexOf('}')); } @@ -110,7 +108,7 @@ public class PathTrie { children = unmodifiableMap(newChildren); } - public synchronized void insert(String[] path, int index, T value) { + private synchronized void insert(String[] path, int index, T value) { if (index >= path.length) return; @@ -145,7 +143,7 @@ public class PathTrie { node.insert(path, index + 1, value); } - public synchronized void insertOrUpdate(String[] path, int index, T value, BiFunction updater) { + private synchronized void insertOrUpdate(String[] path, int index, T value, BiFunction updater) { if (index >= path.length) return; @@ -357,14 +355,14 @@ public class PathTrie { return new PathTrieIterator<>(this, path, paramSupplier); } - class PathTrieIterator implements Iterator { + private static class PathTrieIterator implements Iterator { private final List modes; private final Supplier> paramSupplier; private final PathTrie trie; private final String path; - PathTrieIterator(PathTrie trie, String path, Supplier> paramSupplier) { + PathTrieIterator(PathTrie trie, String path, Supplier> paramSupplier) { this.path = path; this.trie = trie; this.paramSupplier = paramSupplier; diff --git a/server/src/main/java/org/elasticsearch/rest/RestController.java b/server/src/main/java/org/elasticsearch/rest/RestController.java index 1263270d708..4ba5d5ed0df 100644 --- a/server/src/main/java/org/elasticsearch/rest/RestController.java +++ b/server/src/main/java/org/elasticsearch/rest/RestController.java @@ -363,11 +363,12 @@ public class RestController implements HttpServerTransport.Dispatcher { // Between retrieving the correct path, we need to reset the parameters, // otherwise parameters are parsed out of the URI that aren't actually handled. final Map originalParams = new HashMap<>(request.params()); + final Map requestParamsRef = request.params(); return handlers.retrieveAll(getPath(request), () -> { // PathTrie modifies the request, so reset the params between each iteration - request.params().clear(); - request.params().putAll(originalParams); - return request.params(); + requestParamsRef.clear(); + requestParamsRef.putAll(originalParams); + return requestParamsRef; }); }