Browse Source

Remove needless capturing lambda in hot loop in AuthorizationService (#104856)

The spot that is touched by iterating over bulk item requests is very hot
for large responses and caused almost 1% of all allocations during Rally's http_logs
indexing run.
Armin Braun 1 year ago
parent
commit
90e83b97da

+ 3 - 15
x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authz/AuthorizationService.java

@@ -778,7 +778,7 @@ public class AuthorizationService {
                     }
                     return resolved;
                 });
-                actionToIndicesMap.compute(itemAction, (ignore, resolvedIndicesSet) -> addToOrCreateSet(resolvedIndicesSet, resolvedIndex));
+                actionToIndicesMap.computeIfAbsent(itemAction, k -> new HashSet<>()).add(resolvedIndex);
             }
 
             final ActionListener<Collection<Tuple<String, IndexAuthorizationResult>>> bulkAuthzListener = ActionListener.wrap(
@@ -800,15 +800,9 @@ public class AuthorizationService {
                         final String resolvedIndex = resolvedIndexNames.get(item.index());
                         final String itemAction = getAction(item);
                         if (actionToIndicesAccessControl.get(itemAction).hasIndexPermissions(resolvedIndex)) {
-                            actionToGrantedIndicesMap.compute(
-                                itemAction,
-                                (ignore, resolvedIndicesSet) -> addToOrCreateSet(resolvedIndicesSet, resolvedIndex)
-                            );
+                            actionToGrantedIndicesMap.computeIfAbsent(itemAction, ignore -> new HashSet<>()).add(resolvedIndex);
                         } else {
-                            actionToDeniedIndicesMap.compute(
-                                itemAction,
-                                (ignore, resolvedIndicesSet) -> addToOrCreateSet(resolvedIndicesSet, resolvedIndex)
-                            );
+                            actionToDeniedIndicesMap.computeIfAbsent(itemAction, ignore -> new HashSet<>()).add(resolvedIndex);
                             item.abort(
                                 resolvedIndex,
                                 actionDenied(
@@ -876,12 +870,6 @@ public class AuthorizationService {
         }, listener::onFailure));
     }
 
-    private static Set<String> addToOrCreateSet(Set<String> set, String item) {
-        final Set<String> localSet = set != null ? set : new HashSet<>(4);
-        localSet.add(item);
-        return localSet;
-    }
-
     private static String resolveIndexNameDateMath(BulkItemRequest bulkItemRequest) {
         final ResolvedIndices resolvedIndices = IndicesAndAliasesResolver.resolveIndicesAndAliasesWithoutWildcards(
             getAction(bulkItemRequest),