VectorX
VectorX 使用 Java Vector API (jdk.incubator.vector) 重写了 Minecraft 中一组经过精心挑选的热门数组循环,使得单条 CPU 指令能够一次性处理 4、8 或 16 个值,而不是像以前那样一次只处理一个。这得益于 AVX2、AVX-512 或 NEON 指令集,具体取决于你的 CPU 和 JDK 实际提供的支持。
与大多数 SIMD(单指令多数据)实验不同,此处的每个向量化内核都附带了一个标量孪生版本。一旦检测到任何异常:缺少 Vector API 模块、遇到意外的 CPU、启动时的正确性检查失败、存在冲突模组,或在运行时发生异常,VectorX 就会回退到该标量孪生版本。原版行为得以保留。
VectorX 是一项针对世界生成和区块 I/O 的优化。它减少了服务器线程(以及单人游戏内置服务器)上的 CPU 时间。它并不旨在提升 FPS。
⚠️ 请先阅读:必需的 JVM 标志
Vector API 仍然是 JDK 的一个**孵化(incubating)**模块,因此除非你明确要求,否则 JVM 不会暴露它。请将以下内容添加到你的 JVM 参数、启动器配置文件或服务器启动脚本中:
--add-modules=jdk.incubator.vector
即使没有这个标志,VectorX 仍然会加载并正常运行,但它会在标量后端运行所有内容,完全不提供任何加速效果。 请在启动时检查日志:
[vectorx] VectorX ready: 4/4 kernels on the vector backend
如果显示 0/4,则说明缺少该标志。
优化内容
四个独立的内核,每个均可单独切换:
densityFunctionMap: DensityFunctions$Mapped 中的逐元素变换,广泛用于噪声和地形生成。仅对 half_negative、quarter_negative 和 squeeze 进行了向量化,其他操作已经是无分支的,且实测没有实际收益,因此故意保留在原版代码中。
densityFunctionClamp: 应用于整个密度数组的钳位操作,使用按通道的 min/max 替代逐元素的分支判断。
packedStorageUnpack: SimpleBitStoragez / PalettedContainer 中调色板位打包的两个方向,在每次区块保存和加载时都会命中。还包括 SimpleBitStorage.getAll(IntConsumer),用于区块加载时的方块/流体数量重新计算以及生物群系装饰可能的生物群系扫描。
canyonCarverSkip: CanyonWorldCarver 中的椭球成员资格跳过测试,针对整个垂直列进行批量计算,而不是每个方块调用一次。CaveWorldCarver 故意未被覆盖,因为其跳过测试闭包了一个由 RNG 派生的局部变量,若不冒风险导致生成的地形与原版的差异,就无法访问该变量。
性能表现
在真实的专用服务器上测得。相同的种子、相同的坐标、1024 个强制加载的区块,AMD Ryzen 7 9800X3D(选择 AVX-512),Temurin 25.0.4 JDK:
| 后端 |
CPU 秒数 |
| Vector (AVX-512) |
4.20 |
| Scalar |
5.59 |
速度提高约 1.33 倍,CPU 时间减少约 25%,用于相同的世界生成。
这些数字是在 3 个内核激活的情况下测得的(在添加 canyonCarverSkip 之前),尚未反映峡谷雕刻的贡献。将此表视为下限,而非当前的总数值。
孤立的微基准测试显示出更大的数字(内核本身达到 5–12 倍),但实际的区块生成将大部分时间花费在噪声采样、特征/结构放置和生物群系计算上,而 VectorX 并未触及这些部分。25% 是向量化内核在实际测量位置所占的总成本份额,而非外推得出的结果。
你的结果会有所不同:较旧的仅限 SSE/AVX2 的 CPU,或选择更窄向量宽度的 JDK,将会看到按比例更少的提升。在配置文件中启用 diagnostics 以查看你实际获得的向量宽度。
安全性保障
- 启动差分自测。 每个向量后端与其标量孪生版本并行运行,输入为生成的数据,包括边缘情况和非通道宽度倍数的长度。一旦出现不匹配,该内核在本会话期间降级为标量模式。
- 每次调用故障开放。 每个钩子都会捕获
Throwable 并回退到确切的原版代码路径、Mojang 自己的方法体,或调用 Mojang 自身 transform 的逐元素循环。
- 无需硬性 Mixin 要求。 如果 Minecraft 更新或冲突模组移动了目标代码,VectorX 会变得无效,而不会导致崩溃。
- 不对孵化模块持有静态引用。 即使在完全缺失
jdk.incubator.vector 的 JVM 上,VectorX 也能正确加载和运行。
配置
编辑 config/ 下生成的 TOML 文件,或通过 Mod Menu 在游戏中打开设置屏幕。
| 选项 |
默认值 |
效果 |
backendForcedScalar |
false |
强制所有内核使用标量后端,覆盖以下所有设置。 |
densityFunctionMap |
auto |
auto = 如果可用且正确则使用向量,否则使用标量 · scalar = 强制标量 · off = 禁用钩子。 |
densityFunctionClamp |
auto |
相同的 auto / scalar / off 语义。 |
packedStorageUnpack |
auto |
相同的 auto / scalar / off 语义。 |
canyonCarverSkip |
auto |
相同的 auto / scalar / off 语义。 |
selfTest |
true |
在启动时运行标量与向量的检查。请保持开启。 |
diagnostics |
false |
记录完整的启动报告:模块解析、每内核后端、回退原因、向量宽度、已知模组交互。 |
除 diagnostics 外,所有更改都需要重启才能生效。
VectorX
VectorX rewrites a small, carefully chosen set of Minecraft's hottest array loops using the Java Vector API (jdk.incubator.vector), so a single CPU instruction processes 4, 8 or 16 values at once instead of one thanks to AVX2, AVX-512 or NEON, whichever your CPU and JDK actually provide.
Unlike most SIMD experiments, every vectorized kernel here ships with a scalar twin, and VectorX falls back to that twin the moment anything is off: no Vector API module, an unexpected CPU, a failed startup correctness check, a conflicting mod, or an exception at runtime. Vanilla behaviour is preserved.
VectorX is a world generation and chunk I/O optimization. It cuts CPU time on the server thread (and on singleplayer's integrated server). It is not targeting FPS.
⚠️ Read this first: the required JVM flag
The Vector API is still an incubating JDK module, so the JVM won't expose it unless you ask for it. Add this to your JVM arguments, launcher profile or your server start script:
--add-modules=jdk.incubator.vector
Without this flag VectorX still loads and works perfectly, it just runs everything on the scalar backend and gives you no speedup at all. Check your log at startup:
[vectorx] VectorX ready: 4/4 kernels on the vector backend
If it says 0/4, the flag is missing.
What it optimizes
Four independent kernels, each individually toggleable:
densityFunctionMap: element-wise transforms in DensityFunctions$Mapped, used throughout noise and terrain generation. Only half_negative, quarter_negative and squeeze are vectorized, the other ops are already branch-free and measured no real gain, so they're deliberately left on vanilla.
densityFunctionClamp: the clamp applied across a whole density array, using lane-wise min/max instead of a per-element branch.
packedStorageUnpack: both directions of palette bit-packing in SimpleBitStoragez / PalettedContainer, hit on every chunk save and every chunk load. Also covers SimpleBitStorage.getAll(IntConsumer), used by the block/fluid count recompute on chunk load and the biome-decoration possible-biomes scan.
canyonCarverSkip: the ellipsoid-membership skip test in CanyonWorldCarver, bulk-computed for a whole vertical column instead of one call per block. CaveWorldCarver is deliberately not covered, its skip test closes over an RNG-derived local that can't be reached without risking desyncing generated terrain from vanilla.
Performance
Measured on a real dedicated server. Same seed, same coordinates, 1024 force-loaded chunks, AMD Ryzen 7 9800X3D (AVX-512 selected), Temurin 25.0.4 JDK:
| Backend |
CPU-seconds |
| Vector (AVX-512) |
4.20 |
| Scalar |
5.59 |
≈1.33× faster, ≈25% less CPU time for identical world generation.
These numbers were measured with 3 of the 4 kernels active (before canyonCarverSkip was added) and don't yet reflect canyon carving's contribution. Treat this table as a floor, not the current total.
Isolated microbenchmarks show far bigger numbers (5–12× on the kernels themselves), but real chunk generation spends most of its time on noise sampling, feature/structure placement and biome computation, none of which VectorX touches. The 25% is the vectorized kernels' actual share of the total cost, measured in place rather than extrapolated.
Your results will vary: older CPUs limited to SSE/AVX2, or JDKs selecting a narrower vector width, will see proportionally less. Enable diagnostics in the config to see the width you actually get.
Why it's safe
- Startup differential self-test. Each vector backend is run side by side with its scalar twin over generated inputs, including edge cases and non-multiple-of-lane-width lengths. One mismatch demotes that kernel to scalar for the session.
- Per-call fail-open. Every hook catches
Throwable and falls through to the exact vanilla code path, Mojang's own method body, or a per-element loop calling Mojang's own transform.
- No hard Mixin requirements. A Minecraft update or conflicting mod that moves the targeted code makes VectorX inert, not crash-prone.
- No static reference to the incubator module. VectorX loads and runs correctly on a JVM where
jdk.incubator.vector is entirely absent.
Configuration
Edit the generated TOML file under config/, or open the settings screen in-game with Mod Menu.
| Option |
Default |
Effect |
backendForcedScalar |
false |
Forces every kernel to scalar, overriding everything below. |
densityFunctionMap |
auto |
auto = vector if available and correct, scalar otherwise · scalar = force scalar · off = disable the hook. |
densityFunctionClamp |
auto |
Same auto / scalar / off semantics. |
packedStorageUnpack |
auto |
Same auto / scalar / off semantics. |
canyonCarverSkip |
auto |
Same auto / scalar / off semantics. |
selfTest |
true |
Runs the scalar-vs-vector check at startup. Leave this on. |
diagnostics |
false |
Logs a full startup report: module resolution, per-kernel backend, fallback reason, vector width, known mod interactions. |
Everything except diagnostics needs a restart.
评论 0
登录后参与评分
还没有评论,来抢沙发吧