Skip to content

Commit 82535da

Browse files
committed
Replace mixin.feature.disable_unihex_font with mixin.perf.compress_unihex_font
The font still takes up some memory, but less than before
1 parent f70fb21 commit 82535da

File tree

5 files changed

+141
-38
lines changed

5 files changed

+141
-38
lines changed

common/src/main/java/org/embeddedt/modernfix/common/mixin/feature/disable_unihex_font/UnihexProviderDefinitionMixin.java

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.embeddedt.modernfix.common.mixin.perf.compress_unihex_font;
2+
3+
import com.llamalad7.mixinextras.sugar.Local;
4+
import it.unimi.dsi.fastutil.bytes.ByteList;
5+
import net.minecraft.client.gui.font.providers.UnihexProvider;
6+
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
7+
import org.embeddedt.modernfix.render.font.CompactUnihexContents;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.injection.At;
10+
import org.spongepowered.asm.mixin.injection.Inject;
11+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
12+
13+
@Mixin(targets = {"net/minecraft/client/gui/font/providers/UnihexProvider$ByteContents"})
14+
@ClientOnlyMixin
15+
public class UnihexProviderByteContentsMixin {
16+
@Inject(method = "read", at = @At(value = "NEW", target = "([B)Lnet/minecraft/client/gui/font/providers/UnihexProvider$ByteContents;"), cancellable = true)
17+
private static void useCompactIfPossible(int index, ByteList byteList, CallbackInfoReturnable<UnihexProvider.LineData> cir, @Local(ordinal = 0) byte[] contents) {
18+
if (contents.length == 16) {
19+
cir.setReturnValue(new CompactUnihexContents.Bytes(contents));
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.embeddedt.modernfix.common.mixin.perf.compress_unihex_font;
2+
3+
import com.llamalad7.mixinextras.sugar.Local;
4+
import it.unimi.dsi.fastutil.bytes.ByteList;
5+
import net.minecraft.client.gui.font.providers.UnihexProvider;
6+
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
7+
import org.embeddedt.modernfix.render.font.CompactUnihexContents;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.injection.At;
10+
import org.spongepowered.asm.mixin.injection.Inject;
11+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
12+
13+
@Mixin(targets = {"net/minecraft/client/gui/font/providers/UnihexProvider$ShortContents"})
14+
@ClientOnlyMixin
15+
public class UnihexProviderShortContentsMixin {
16+
@Inject(method = "read", at = @At(value = "NEW", target = "([S)Lnet/minecraft/client/gui/font/providers/UnihexProvider$ShortContents;"), cancellable = true)
17+
private static void useCompactIfPossible(int index, ByteList byteList, CallbackInfoReturnable<UnihexProvider.LineData> cir, @Local(ordinal = 0) short[] contents) {
18+
if (contents.length == 16) {
19+
cir.setReturnValue(new CompactUnihexContents.Shorts(contents));
20+
}
21+
}
22+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.embeddedt.modernfix.render.font;
2+
3+
import net.minecraft.client.gui.font.providers.UnihexProvider;
4+
5+
/**
6+
* Implements more compact storage for LineData contents.
7+
*
8+
* Credit for the idea of using flattened fields rather than a backing array goes to @AnAwesomGuy.
9+
*/
10+
public class CompactUnihexContents {
11+
private static long extract8Bytes(byte[] arr, int off) {
12+
long l = 0;
13+
for (int i = 0; i < 8; i++) {
14+
l |= ((long)arr[off + i] << (i * 8));
15+
}
16+
return l;
17+
}
18+
19+
private static byte extractByte(long compressed, int off) {
20+
return (byte)((compressed >> (off * 8)) & 0xFF);
21+
}
22+
23+
private static long extract4Shorts(short[] arr, int off) {
24+
long l = 0;
25+
for (int i = 0; i < 4; i++) {
26+
l |= ((long)arr[off + i] << (i * 16));
27+
}
28+
return l;
29+
}
30+
31+
private static short extractShort(long compressed, int off) {
32+
return (byte)((compressed >> (off * 16)) & 0xFF);
33+
}
34+
35+
public static class Bytes implements UnihexProvider.LineData {
36+
private final long b0;
37+
private final long b8;
38+
39+
public Bytes(byte[] contents) {
40+
this.b0 = extract8Bytes(contents, 0);
41+
this.b8 = extract8Bytes(contents, 8);
42+
}
43+
44+
@Override
45+
public int line(int index) {
46+
if (index < 0 || index >= 16) {
47+
throw new ArrayIndexOutOfBoundsException();
48+
}
49+
if (index < 8) {
50+
return extractByte(b0, index) << 24;
51+
} else {
52+
return extractByte(b8, index - 8) << 24;
53+
}
54+
}
55+
56+
@Override
57+
public int bitWidth() {
58+
return 8;
59+
}
60+
}
61+
62+
public static class Shorts implements UnihexProvider.LineData {
63+
private final long b0;
64+
private final long b4;
65+
private final long b8;
66+
private final long b12;
67+
68+
public Shorts(short[] contents) {
69+
this.b0 = extract4Shorts(contents, 0);
70+
this.b4 = extract4Shorts(contents, 4);
71+
this.b8 = extract4Shorts(contents, 8);
72+
this.b12 = extract4Shorts(contents, 12);
73+
}
74+
75+
@Override
76+
public int line(int index) {
77+
if (index < 0 || index >= 16) {
78+
throw new ArrayIndexOutOfBoundsException();
79+
}
80+
if (index < 4) {
81+
return extractShort(b0, index) << 16;
82+
} else if (index < 8) {
83+
return extractShort(b4, index - 4) << 16;
84+
} else if (index < 12) {
85+
return extractShort(b8, index - 8) << 16;
86+
} else {
87+
return extractShort(b12, index - 12) << 16;
88+
}
89+
}
90+
91+
@Override
92+
public int bitWidth() {
93+
return 16;
94+
}
95+
}
96+
}

common/src/main/resources/assets/modernfix/lang/en_us.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
"modernfix.option.mixin.perf.twilightforest.structure_spawn_fix": "Fixes lag caused by Twilight Forest worldgen checking structures very inefficiently",
118118
"modernfix.option.mixin.perf.fast_forge_dummies": "Speeds up Forge registry freezing during launch by using a faster code path",
119119
"modernfix.option.mixin.perf.tag_id_caching": "Speeds up uses of tag entries by caching the location object instead of recreating it every time",
120-
"modernfix.option.mixin.feature.disable_unihex_font": "Remove the Unicode font, saves 10MB but causes special characters to no longer render",
120+
"modernfix.option.mixin.perf.compress_unihex_font": "Stores the glyphs for the Unicode font more efficiently. Kudos to @AnAwesomGuy for the trick.",
121121
"modernfix.option.mixin.bugfix.world_leaks": "Reduces the memory usage of old client-side worlds that aren't needed after switching dimensions. These are normally garbage collected in vanilla, but mods sometimes retain references to them.",
122122
"modernfix.option.mixin.perf.compact_mojang_registries": "(Fabric) Experimental option that reduces the memory usage of registries by roughly 50%. Not useful in most modpacks unless they contain millions of blocks and items.",
123123
"modernfix.option.mixin.perf.dynamic_block_codecs": "Avoids storing a codec for every block(state) and instead generates and caches it on the fly when needed. Generally not worth enabling unless you have a million blocks/items.",

0 commit comments

Comments
 (0)