mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-26 02:19:19 +00:00
Originally vanilla logic is to use stream, and Mojang switched it to Guava's Collections2 since 1.21.4. It is much faster than using stream or manually adding to a new ArrayList. Manually adding to a new ArrayList requires allocating a new object array. However, the Collections2 lazy handles filter condition on iteration, so much better.
62 lines
2.6 KiB
Diff
62 lines
2.6 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Taiyou06 <kaandindar21@gmail.com>
|
|
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<ContextKey<?>> set = Sets.difference(this.params.keySet(), contextKeySet.allowed());
|
|
- if (!set.isEmpty()) {
|
|
- throw new IllegalArgumentException("Parameters not allowed in this parameter set: " + set);
|
|
- } else {
|
|
- Set<ContextKey<?>> 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<ContextKey<?>> allowed = contextKeySet.allowed();
|
|
+ Set<ContextKey<?>> 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<ContextKey<?>> required = contextKeySet.required();
|
|
+ Set<ContextKey<?>> 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
|
|
}
|
|
}
|
|
}
|