Skip to content

Commit 2d824ba

Browse files
Avoid extra allocations in RestGetAliasesAction
When no explicit aliases are provided in the call there is no need to collect the index names or aliases into HashSets if they won't be used. Also fixed where the index name was being added for each loop of the alias list.
1 parent b563145 commit 2d824ba

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestGetAliasesAction.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,28 @@ static RestResponse buildRestResponse(
8484
) throws Exception {
8585
final Set<String> indicesToDisplay = new HashSet<>();
8686
final Set<String> returnedAliasNames = new HashSet<>();
87-
for (final Map.Entry<String, List<AliasMetadata>> cursor : responseAliasMap.entrySet()) {
88-
for (final AliasMetadata aliasMetadata : cursor.getValue()) {
89-
if (aliasesExplicitlyRequested) {
90-
// only display indices that have aliases
91-
indicesToDisplay.add(cursor.getKey());
87+
if (aliasesExplicitlyRequested || requestedAliases.length > 0) {
88+
for (final Map.Entry<String, List<AliasMetadata>> cursor : responseAliasMap.entrySet()) {
89+
final var aliases = cursor.getValue();
90+
if (aliases.isEmpty() == false) {
91+
if (aliasesExplicitlyRequested) {
92+
// only display indices that have aliases
93+
indicesToDisplay.add(cursor.getKey());
94+
}
95+
if (requestedAliases.length > 0) {
96+
for (final AliasMetadata aliasMetadata : aliases) {
97+
returnedAliasNames.add(aliasMetadata.alias());
98+
}
99+
}
92100
}
93-
returnedAliasNames.add(aliasMetadata.alias());
94101
}
95102
}
96-
dataStreamAliases.entrySet()
97-
.stream()
98-
.flatMap(entry -> entry.getValue().stream())
99-
.forEach(dataStreamAlias -> returnedAliasNames.add(dataStreamAlias.getName()));
103+
if (requestedAliases.length > 0) {
104+
dataStreamAliases.entrySet()
105+
.stream()
106+
.flatMap(entry -> entry.getValue().stream())
107+
.forEach(dataStreamAlias -> returnedAliasNames.add(dataStreamAlias.getName()));
108+
}
100109

101110
// compute explicitly requested aliases that have are not returned in the result
102111
final SortedSet<String> missingAliases = new TreeSet<>();

0 commit comments

Comments
 (0)