forked from Sk1erLLC/Patcher
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Gradle to 8.10, added ResourceLocation string deduplication (…
…from CaffeineMC/hydrogen-fabric) Signed-off-by: xtrm <[email protected]>
- Loading branch information
Showing
6 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/club/sk1er/patcher/mixins/performance/ResourceLocation_Deduplicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package club.sk1er.patcher.mixins.performance; | ||
|
||
import net.minecraft.util.ResourceLocation; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Mutable; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
/** | ||
* From <a href="https://github.com/CaffeineMC/hydrogen-fabric/blob/95da36046c2f55617a76ebb8ea1e1f2a330330e5/src/main/java/me/jellysquid/mods/hydrogen/mixin/util/MixinIdentifier.java">CaffeineMC/hydrogen-fabric</a> under GNU LGPL v3.0 | ||
*/ | ||
@Mixin(ResourceLocation.class) | ||
public class ResourceLocation_Deduplicate { | ||
@Shadow | ||
@Final | ||
@Mutable | ||
protected String resourceDomain; | ||
|
||
@Shadow | ||
@Final | ||
@Mutable | ||
protected String resourcePath; | ||
|
||
@Inject(method = "<init>(I[Ljava/lang/String;)V", at = @At("RETURN")) | ||
private void reinit(int what, String[] id, CallbackInfo ci) { | ||
this.resourceDomain = IdentifierCaches.NAMESPACES.deduplicate(this.resourceDomain); | ||
this.resourcePath = IdentifierCaches.PATH.deduplicate(this.resourcePath); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/me/jellysquid/mods/hydrogen/common/dedup/DeduplicationCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package me.jellysquid.mods.hydrogen.common.dedup; | ||
|
||
import it.unimi.dsi.fastutil.Hash; | ||
import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet; | ||
|
||
import java.util.Objects; | ||
|
||
public class DeduplicationCache<T> { | ||
private final ObjectOpenCustomHashSet<T> pool; | ||
|
||
private int attemptedInsertions = 0; | ||
private int deduplicated = 0; | ||
|
||
public DeduplicationCache(Hash.Strategy<T> strategy) { | ||
this.pool = new ObjectOpenCustomHashSet<>(strategy); | ||
} | ||
|
||
public DeduplicationCache() { | ||
this.pool = new ObjectOpenCustomHashSet<>(new Hash.Strategy<T>() { | ||
@Override | ||
public int hashCode(T o) { | ||
return Objects.hashCode(o); | ||
} | ||
|
||
@Override | ||
public boolean equals(T a, T b) { | ||
return Objects.equals(a, b); | ||
} | ||
}); | ||
} | ||
|
||
public synchronized T deduplicate(T item) { | ||
this.attemptedInsertions++; | ||
|
||
T result = this.pool.addOrGet(item); | ||
|
||
if (result != item) { | ||
this.deduplicated++; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public synchronized void clearCache() { | ||
this.attemptedInsertions = 0; | ||
this.deduplicated = 0; | ||
|
||
this.pool.clear(); | ||
} | ||
|
||
@Override | ||
public synchronized String toString() { | ||
return String.format("DeduplicationCache ( %d/%d de-duplicated, %d pooled )", | ||
this.deduplicated, this.attemptedInsertions, this.pool.size()); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/me/jellysquid/mods/hydrogen/common/dedup/IdentifierCaches.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package me.jellysquid.mods.hydrogen.common.dedup; | ||
|
||
public class IdentifierCaches { | ||
public static final DeduplicationCache<String> NAMESPACES = new DeduplicationCache<>(); | ||
public static final DeduplicationCache<String> PATH = new DeduplicationCache<>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters