Avoid slow getHash call when retrieving skins

Co-authored-by: Fury_Phoenix <64714532+Phoenix-Starlight@users.noreply.github.com>
This commit is contained in:
embeddedt 2023-11-05 21:22:17 -05:00
parent d11e9ac190
commit e859ce8eb6
No known key found for this signature in database
GPG Key ID: A69433EC199B5613

View File

@ -0,0 +1,37 @@
package org.embeddedt.modernfix.common.mixin.perf.cache_profile_texture_url;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import net.minecraft.client.resources.SkinManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
@Mixin(SkinManager.class)
public class SkinManagerMixin {
@Unique
private final Cache<MinecraftProfileTexture, String> mfix$hashCache = CacheBuilder.newBuilder()
.expireAfterAccess(60, TimeUnit.SECONDS)
.concurrencyLevel(1)
.build();
@Redirect(method = "registerTexture(Lcom/mojang/authlib/minecraft/MinecraftProfileTexture;Lcom/mojang/authlib/minecraft/MinecraftProfileTexture$Type;Lnet/minecraft/client/resources/SkinManager$SkinTextureCallback;)Lnet/minecraft/resources/ResourceLocation;",
at = @At(value = "INVOKE", target = "Lcom/mojang/authlib/minecraft/MinecraftProfileTexture;getHash()Ljava/lang/String;", remap = false))
private String useCachedHash(MinecraftProfileTexture texture) {
// avoid lambda allocation for common case
String hash = mfix$hashCache.getIfPresent(texture);
if(hash != null)
return hash;
try {
return mfix$hashCache.get(texture, texture::getHash);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
}