内存与哈希工具

基本信息

属性 值
哈希包 com.gtnewhorizon.gtnhlib.hash(2 个类)
ByteBuf 包 com.gtnewhorizon.gtnhlib.bytebuf(10 个类)
来源 主要移植自 LWJGL 3 源码(bytebuf/package-info.java)
外部依赖 it.unimi.dsi:fastutil:8.5.18(由 GTNHExtLib 提供)

bytebuf/package-info.java 的说明:

Utilities for more efficient ByteBuffer management, mostly ported over from LWJGL 3 sources. Intended for static wildcard import usage, so you can use e.g. stackPush() unqualified.

即该包设计为静态通配符导入使用,方法名不带类名前缀。

FNV-1a 哈希

hash/ 下两个类提供 32 位与 64 位 FNV-1a:

类 偏移基 质数
Fnv1a32 OFFSET_BASIS32 = 0x811C_9DC5 PRIME32 = 0x100_0193
Fnv1a64 OFFSET_BASIS64 = 0xCBF2_9CE4_8422_2325L PRIME64 = 0x100_0000_01B3L

两者 API 完全对称,各 20 个 public static 成员(2 个常量 + initialState + 17 个 hashStep 重载):

hashStep 重载 数量 覆盖类型
标量 8 byte、boolean、char、short、int、long、float、double
数组 8 byte[]、boolean[]、char[]、short[]、int[]、long[]、float[]、double[]
字符串 1 CharSequence

用法是流式的 —— 每步返回新的 prevState:

int hash = Fnv1a32.initialState();
hash = Fnv1a32.hashStep(hash, hostDimensionId);
hash = Fnv1a32.hashStep(hash, namespace);
hash = Fnv1a32.hashStep(hash, subId);

[虚拟子世界注册表](../api/world-context-registry) 的 WorldAddress.hashCode() 即按此模式组合三个字段(用 Fnv1a32),因此本页是那条目的直接依赖。

MemoryStack

bytebuf/MemoryStack.java(1000+ 行)是本包的主体,提供线程局部栈式分配:

常量 值
DEFAULT_STACK_SIZE 64 * 1024(64 KiB)
DEFAULT_STACK_FRAMES 8
DEBUG_STACK 由系统属性 org.lwjgl.util.DebugStack 决定

工厂与栈操作:

方法 作用
create() / create(int) / create(ByteBuffer) / ncreate(long, int) 创建实例
stackGet() 取当前线程栈
stackPush() / stackPop() 压栈 / 弹栈
stackMalloc(int) / stackCalloc(int) 分配 / 清零分配
nstackMalloc(int, int) / nstackCalloc(int, int, int) 带对齐的分配
stackBytes(byte…) 直接写入 1–4 个 byte 或 varargs

其余类:APIUtil、MemoryManage、MemoryUtilities、MultiReleaseMemCopy、MultiReleaseTextDecoding、Pointer、CheckIntrinsics、Checks、StackWalkUtil。

MultiRelease* 两个类对应多版本 JAR 支持;CheckIntrinsics 在启动时校验 JVM intrinsics 可用性,不可用则抛 RuntimeException(CheckIntrinsics.java:54-56)。MemoryUtilities 同样把 Throwable 包成 UnsupportedOperationException 重抛(MemoryUtilities.java:186-188),不静默。

数值

数值名 值
hash 包类数 2
Fnv1a32 / Fnv1a64 公开静态成员 各 20(2 常量 + initialState + 17 hashStep)
bytebuf 包类数 10
MemoryStack 默认栈大小 65536 字节
MemoryStack 默认栈帧数 8
stackBytes 重载 5(1/2/3/4 个 byte + varargs)

交互

FastUtil shadow 由 GTNHExtLib 提供(README.md Utilities 节末行):

FastUtil shadow via https://github.com/GTNewHorizons/GTNHExtLib

即 dependencies.gradle 的 api("com.github.GTNewHorizons:GTNHExtLib:1.0.4"),配合 usesShadowedDependencies = true 与 relocateShadowedDependencies = true 打入 jar。因此 it.unimi.dsi.fastutil 类型在运行时可用而无需单独安装 FastUtil。

@NotNullByDefault 标注在 bytebuf 包上(package-info.java:6),该包内所有参数与返回值默认非空。

相关条目