Fast NBT 让 Minecraft 频繁进行的 NBT 操作成本更低:读取标签、将其转换为游戏对象,以及从区块、数据包、方块状态和物品栈中加载它们。
CompoundTag
- 各种 get 方法。 在原版游戏中,它首先调用
contains 检查键是否存在,然后调用 get —— 这需要两次地图查找。Fast NBT 将这两个操作合并为一个并应用内联,从而大大提高了查找速度。
- 迭代。 使用
keySet 配合 get 进行迭代的方式已被替换为同时遍历键和值的方法,因此写入和合并复合标签时不再需要对每个条目进行查找。
- 相关类。 其他相关类中使用这些 get 方法的优化也得到了改进。
ItemStack、方块状态与 NBT I/O
- ItemStack 加载。 从 NBT 读取
ItemStack 时,直接从后备地图中提取 tag 和 ForgeCaps,而不是通过两次 contains 调用探测;getOrCreateTagElement、getTagElement 和 removeTagKey 只需一次查找即可解析。
- 方块状态。
NbtUtils.readBlockState 和 getDataVersion 直接读取 Name 和 Properties,而不是经过原版的 contains / getString 链式调用。
- 区块调色板(Chunk section palettes)。 每个调色板条目通常都要经过完整的 DFU 编解码器链 —— 分发编解码器、注册表查找、每个属性一个
MapCodec,以及 DataResult / Either / Pair / Optional 的分配 —— 这在每次加载和保存区块时都会发生。Fast NBT 直接从地图中解析标准的 {Name, Properties} 标签。这是本模组最大的性能提升点。
- NBT I/O。 无限制读取器 —— 包括每次区块加载,以及 level.dat 和 playerdata —— 跳过了一个结果被丢弃的字节记账过程。
因此,收益主要出现在批量解码 NBT 的地方:世界和区块加载、服务器自动保存(这会重新编码每个区块),以及携带带标签 ItemStack 的物品栏、方块实体和实体载荷。调色板路径上临时对象的减少也意味着更小的 GC 压力。
这不是一款通用的性能优化模组:它不触碰 NBT 之外的任何内容,也无法帮助受限于磁盘或网络的工作负载。节省程度取决于整合包解码了多少 NBT,在纯原版世界中效果最小。
配置
config/fastnbt.toml 会在首次启动时生成,并为每项优化提供独立的开关:
enabled = true # 下方所有功能的总开关
[features.itemStack]
enabled = true
[features.blockState]
enabled = true
[features.blockStateCodec]
enabled = true
[features.nbtIo]
enabled = true
CompoundTag 的重写没有单独的开关;全局 enabled 标志控制它们。
安全性保障
- 仅标准数据走快速路径。 非标准数据会被报告为解码错误,而不是被猜测处理;如果
Properties 中的键是方块所不具备的,则会被忽略,这与原版行为完全一致。对于区块调色板,调用者会将失败的条目转为空气,因此引用了未安装模组中方块的世界仍然可以加载 —— 每个条目会输出一行日志。
- 从不替换编码过程。 调色板优化仅加速了解码过程,因此保存的区块和网络数据包保持字节级一致。
- 注入冲突。 这些优化替换或重定向了原版方法,且不附带非破坏性变体。因此,如果另一个模组针对相同的方法进行了修改,最后应用的模组将获胜。建议在发布大型整合包前进行测试。
根据 GNU LGPL v3.0 或更高版本授权。
Fast NBT makes the NBT work Minecraft does constantly cheaper: reading tags, turning them into game objects, and loading them from chunks, packets, block states and ItemStacks.
CompoundTag
- All kinds of get methods. In the vanilla game it first calls
contains to check whether the key exists, then invokes get — two map lookups. Fast NBT merges those two operations into one and applies inlining, which greatly improves the lookup speed.
- Iteration. Iterating with
keySet paired with get is replaced by an approach that walks keys and values simultaneously, so writing and merging a compound no longer does a lookup for every entry.
- Related classes. Operations that use those get methods in other related classes are optimized too.
ItemStack, block states and NBT I/O
- ItemStack loading. Reading an
ItemStack from NBT takes tag and ForgeCaps straight from the backing map instead of probing with two contains calls, and getOrCreateTagElement, getTagElement and removeTagKey resolve in a single lookup.
- Block states.
NbtUtils.readBlockState and getDataVersion read Name and Properties directly, instead of going through the vanilla contains / getString chain.
- Chunk section palettes. Each palette entry normally goes through the full DFU codec chain — dispatch codec, registry lookup, one
MapCodec per property, plus DataResult / Either / Pair / Optional allocations — on every chunk load and every save. Fast NBT resolves the canonical {Name, Properties} tag straight from the map. This is the largest win in the mod.
- NBT I/O. Unlimited readers — every chunk load, plus level.dat and playerdata — skip a byte-accounting pass whose result is thrown away.
So the gains land where NBT is decoded in bulk: world and chunk loading, server autosaves (which re-encode every chunk), and the inventory, block entity and entity payloads that carry tagged ItemStacks. Fewer temporary objects on the palette path also means less GC pressure.
This is not a general performance mod: it touches nothing outside NBT, and it cannot help a workload that is bounded by disk or network. The savings scale with how much NBT a pack decodes, and are smallest in a plain vanilla world.
Configuration
config/fastnbt.toml is generated on first launch and gives each optimization its own switch:
enabled = true # master switch for everything below
[features.itemStack]
enabled = true
[features.blockState]
enabled = true
[features.blockStateCodec]
enabled = true
[features.nbtIo]
enabled = true
The CompoundTag rewrites have no separate switch; the global enabled flag controls them.
How it stays safe
- Only canonical data takes a fast path. Anything that is not canonical is reported as a decode error instead of being guessed at, and a key in
Properties that the block does not have is ignored, exactly as in vanilla. For chunk palettes the caller turns a failed entry into air, so a world referencing blocks from an uninstalled mod still loads — with one log line per entry.
- Encoding is never replaced. The palette optimization only speeds up decoding, so saved chunks and network packets stay byte-identical.
- Injection conflicts. The optimizations replace or redirect vanilla methods and ship no non-destructive variant, so if another mod targets the same method, the last one applied wins. Test large packs before shipping them.
Licensed under the GNU LGPL v3.0 or later.
评论 0
登录后参与评分
还没有评论,来抢沙发吧