From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Taiyou06 Date: Sat, 22 Mar 2025 00:08:11 +0100 Subject: [PATCH] Optimize ContextMap.create diff --git a/net/minecraft/util/context/ContextMap.java b/net/minecraft/util/context/ContextMap.java index cc8d67df5e074beb8c41bb6ce4f519f963e59d0d..64a48913868d9b8c0a4bb3f97458a64c95e99034 100644 --- a/net/minecraft/util/context/ContextMap.java +++ b/net/minecraft/util/context/ContextMap.java @@ -72,17 +72,41 @@ public class ContextMap { } public ContextMap create(ContextKeySet contextKeySet) { - Set> set = Sets.difference(this.params.keySet(), contextKeySet.allowed()); - if (!set.isEmpty()) { - throw new IllegalArgumentException("Parameters not allowed in this parameter set: " + set); - } else { - Set> set1 = Sets.difference(contextKeySet.required(), this.params.keySet()); - if (!set1.isEmpty()) { - throw new IllegalArgumentException("Missing required parameters: " + set1); - } else { - return new ContextMap(this.params); + // Leaf start - Optimize ContextMap.create + Set> allowed = contextKeySet.allowed(); + Set> invalid = null; + + // Check for any parameters that are not allowed + for (ContextKey key : this.params.keySet()) { + if (!allowed.contains(key)) { + if (invalid == null) { + invalid = new java.util.HashSet<>(); + } + invalid.add(key); } } + if (invalid != null) { + throw new IllegalArgumentException("Parameters not allowed in this parameter set: " + invalid); + } + + Set> required = contextKeySet.required(); + Set> missing = null; + + // Check for any required parameters that are missing + for (ContextKey reqKey : required) { + if (!this.params.containsKey(reqKey)) { + if (missing == null) { + missing = new java.util.HashSet<>(); + } + missing.add(reqKey); + } + } + if (missing != null) { + throw new IllegalArgumentException("Missing required parameters: " + missing); + } + + return new ContextMap(this.params); + // Leaf end - Optimize ContextMap.create } } }