尽可能优化通用 Jigsaw(拼图)结构的生成。本质上,此模组旨在完全不改变结构的外观。它仅致力于让结构生成得更快、更高效。仅此而已。
以下是所执行优化的更详细技术说明。如果在整合包中运行此模组时发现任何冲突或问题,请报告!
-
使用 BoxOctree 替换未优化的 VertexShape 调用,使各部件仅检查附近部件的交叉情况,而非整个结构。
- 默认情况下,原版 Minecraft 使用 VoxelShape(体素形状)来存储正在组装的结构布局中各部件的边界。当添加新部件时,需要判断其是否能放入布局且不与任何其他部件交叉。
问题在于,VoxelShape 并不适合此用途,因为在检查一个 VoxelShape 是否与另一个 VoxelShape 交叉时,会比对所有顶点。在包含大量部件的 Jigsaw 结构中,随着有效部件添加到布局中的数量增加,这种交叉检查的速度会大幅降低。在生成大量递归 Jigsaw 结构部件时,可能会导致卡顿。
此处的优化是用一个内部持有 BoxOctree 实现的虚拟 VoxelShape 替换普通的 VoxelShape,以便将 BoxOctree 传递到需要的地方。该 BoxOctree 在进行交叉检查时,仅检查传入边界框附近的部件。因此,通过忽略大部分距离过远而不影响检查结果的部件,防止了交叉检查时间的无限增长。
- 默认情况下,原版 Minecraft 使用 VoxelShape(体素形状)来存储正在组装的结构布局中各部件的边界。当添加新部件时,需要判断其是否能放入布局且不与任何其他部件交叉。
-
检查 Jigsaw Block(拼图方块)是否完全被阻挡,以确定何时跳过对子刚性部件的检查。
- 在原版中,父部件中的每一个 Jigsaw Block 都会检查子部件中的每一个 Jigsaw Block,以决定何时尝试连接这两个部件。问题是,如果父部件的 Jigsaw Block 面向结构边界的边缘,或者面向另一个独立的部件,那么该 Jigsaw Block 就永远没有空间生成刚性结构部件。因此,我们可以指示游戏跳过检查这个刚性子部件,转而检查下一个部件。这为我们节省了大量昂贵的 Jigsaw Block 匹配检查操作。特别是在拥有大量 Jigsaw Block 的结构中效果显著。
-
(26.2 及以下版本) 用略微优化的版本替换 Jigsaw 目标/朝向匹配逻辑。
- 减少了一半的属性获取调用次数。原版每次为每个 JigsawBlock 调用 getValue 两次,以从同一属性中获取顶部和正面值,而实际上只需获取一次即可得到所有需要的值。同时也略微提升了从 Jigsaw Block 的 NBT 数据中获取关节、目标和名称字符串值的效率。
此外,简化了关节数据检查,使其无需通过低效的byName方法转换为 Enum,并通过重新排序检查顺序来减少频繁运行的逻辑量。
这将有助于处理那些包含极多 Jigsaw Block 的结构,因为每个 Jigsaw Block 都需要对其他结构部件中的所有 Jigsaw Block 运行此匹配代码。
- 减少了一半的属性获取调用次数。原版每次为每个 JigsawBlock 调用 getValue 两次,以从同一属性中获取顶部和正面值,而实际上只需获取一次即可得到所有需要的值。同时也略微提升了从 Jigsaw Block 的 NBT 数据中获取关节、目标和名称字符串值的效率。
-
使没有任何 finalizeProcessing StructureProcessor(最终处理处理器)的大型结构 NBT 加载速度大幅提升。
- 当 NBT 结构部件要在区块中生成时,整个 NBT 部件会被加载到内存中,然后多次迭代所有位置供 StructureProcessors 应用,最后忽略当前生成区块之外的所有位置。这种方式效率极低(非常浪费),并且会导致大型 NBT 文件加载时间过长。
此模组的优化方法是在早期进行边界检查,在传递给 StructureProcessors 之前剔除所有不需要的坐标。然而,任何重写了finalizeProcessing方法的 StructureProcessor 可能需要所有 NBT 坐标才能正常工作,因此对于包含这类 StructureProcessors 的部件(这样的结构极少),此优化将被禁用。在原版中,只有 Trail Ruins(遗迹废墟)不会获得此优化,因为它使用了重写finalizeProcessing方法的 Capped StructureProcessor。
- 当 NBT 结构部件要在区块中生成时,整个 NBT 部件会被加载到内存中,然后多次迭代所有位置供 StructureProcessors 应用,最后忽略当前生成区块之外的所有位置。这种方式效率极低(非常浪费),并且会导致大型 NBT 文件加载时间过长。
-
(1.21.1+) 用略微更快的版本替换 SinglePoolElement 中的 Jigsaw Block 列表洗牌和优先级排序逻辑。
- 主要好处是通过新方法从 NBT 中获取
selection_priority数据时速度略有提升,该方法只获取一次条目而不是两次。原版会先获取两次以检查数据类型,然后再返回值。这种做法有点奇怪且浪费资源。
还尝试了一种新的优先级排序系统,但仅在真正使用selection_priority的少数结构(如 Trials Chamber)中有所帮助。总体而言,这可能是最弱的优化,但对于拥有极其庞大数量的 Jigsaw Block 的 Jigsaw 结构来说,它确实提供了相当大的帮助。因此值得保留此优化。
可能会破坏与原版种子在 Jigsaw Block 运行顺序上的parity(一致性),但我尚未找到相关证据。
- 主要好处是通过新方法从 NBT 中获取
-
跳过对已在当前位置检查过且无法生成的 SinglePoolElements 的逻辑运行。
-
这项优化源于原版 StructureTemplatePool 的机制:它会将所有 SinglePoolElement(单个池元素)放入一个列表中,并根据权重值重复添加元素。例如,如果在模板池中为某个房屋设置了权重 100,该房屋就会被放入列表 100 次!在生成布局时,游戏会复制这个列表,打乱顺序,然后遍历列表尝试生成第一个能适配的位置。这意味着,即使某个元素之前已被判定无法在该位置放置,游戏仍会对列表中的重复条目重新执行检查逻辑。我所做的优化是跳过已检查过的元素的逻辑,直接继续处理列表中的下一个元素。对于在模板池中使用大量高权重值的结构,这能带来显著的性能提升。
-
此外,你可以启用
deduplicateShuffledTemplatePoolElementList配置选项,以从模板池的高权重元素中获得更多性能提升。但该配置选项会带来一定的代价:它会改变结构的布局。尽管生成的布局仍然有效且良好,但它会与关闭该选项时的布局不同。简而言之,为了获得额外的性能提升,此配置会打破结构布局的种子一致性(seed parity)。 -
*明确说明:种子一致性是指每次使用种子 777 并在某处生成包含两座铁匠铺村庄时,只要使用相同的种子,该村庄就会始终位于同一位置并拥有这两座铁匠铺。在此优化配置开启且使用相同种子的情况下,该村庄仍会出现在同一位置,但这次可能只有一座铁匠铺。每次使用该配置开启的相同种子,村庄都会继续生成为一座铁匠铺的布局。这种布局在种子层面上是稳定的,只是与关闭配置时的布局不再相同。
-
-
(适用于 1.21.4 且版本为 v1.1.0+):将
StructureTemplate$Palette中的StructureBlockInfo对象列表替换为StructureBlockInfo对象的调色板(palette),从而在未生成该缓存的StructureTemplate时减少内存占用。-
此优化是基于 contaria 的 Glacier 模组(适用于 1.16.1)修改而来。特别感谢他们允许我以其代码为基础进行此项优化!
-
本质上,在原版中,每次加载结构 NBT 文件时,它都会被转换为
StructureTemplate对象并缓存。在该对象内部,包含一个列表,记录了来自该 NBT 文件的所有位置及其对应位置的方块和 NBT 标签。这是StructureTemplate对象中最大的内存消耗源。本模组将在底层将此列表替换为一个特殊的调色板数据结构对象,该对象伪装成列表形式。调色板的内存大小远小于原列表!当查询世界生成为目的时,它会重新构建该列表作为WeakReference(弱引用),以确保世界生成保持快速且正常运行。世界生成完成后,该列表将被垃圾回收以释放内存,从而使每个StructureTemplate仅在世界生成期间使用所需的完整内存量,而在长期缓存中保持较小的内存占用。缺点是此列表不可也不应通过add、remove、clear等方法进行修改,否则将抛出异常。幸运的是,我认为没有模组直接修改StructureTemplate上的此列表,但如果存在此类情况,请告知我。
-
-
ModernFix 确实包含一个 mixin,它会将 StructureTemplate 对象本身设为 SoftReference,并在游戏内存满载时将其从内存中完全移除。这是一个有效的解决方案,旨在防止在长期游玩过程中将结构 NBT 加载到永不清除的缓存中所导致的内存泄漏问题。这种方案的缺点是,如果结构片段已从内存中清除,则必须再次直接从文件中加载它。该 mixin 可与我的模组的优化功能良好共存。实际可能发生的情况是:我的模组会减小 StructureTemplate 在内存中的占用体积,从而使 ModernFix 能够在不将其完全从内存中移除的情况下保留更多的 StructureTemplate 实例。这在理论上应能提升长期游玩过程中的缓存命中率。
Optimizes general Jigsaw structure generation as best I can. Essentially this mod aims to not change how structures look at all. It only works to try and make structures generate faster and more efficiently. That's it.
Here are the more technical details of the optimizations done. Please report any conflicts or issues that is discovered when running this mod in your modpacks!
-
Replaces VertexShape unoptimized calls with a BoxOctree to make pieces only check nearby pieces for intersections instead of entire structure.
- By default, vanilla uses a VoxelShape to store the bounds of pieces it is assembling for the structure layout. When it goes to add a new piece, it needs to see if it will fit into the layout without intersection any other pieces.
The issue is VoxelShape is not good for this purpose as when checking if a VoxelShape intersects another VoxelShape, ALL the vertices are compared. In Jigsaw structures with lots and lots of pieces, this intersection check slows down greatly the more valid pieces are added to the layout. Can cause a lag spike when generating high number of pieces for recursive Jigsaw structures.
The optimization here replaces the normal VoxelShape with a dummy VoxelShape that holds a BoxOctree implementation inside in order to pass around the BoxOctree to where it needs to be. This BoxOctree, when checking for intersections, will check only the nearby pieces to the incoming bounding box. Thus preventing the runaway growth in intersection checking time as it'll ignore the majority of pieces as they are too far away to matter for the check.
- By default, vanilla uses a VoxelShape to store the bounds of pieces it is assembling for the structure layout. When it goes to add a new piece, it needs to see if it will fit into the layout without intersection any other pieces.
-
Check if a Jigsaw Block is blocked off entirely to know when to skip checking child rigid pieces.
- In vanilla, every single Jigsaw Block in the parent piece will check every single Jigsaw Block in a child piece to know when to attempt to connect the two pieces. The issue is if the parent piece's Jigsaw Block is facing the edge of the structure boundary OR is facing another separate piece, then that Jigsaw Block has no room to ever spawn a rigid structure piece. Therefore, we can tell the game to skip checking this rigid child piece and check the next piece instead. This saves us from doing lots of expensive Jigsaw Block match up checks. Especially in structures that have a huge number of Jigsaw Blocks.
-
(26.2 and below) Replaces Jigsaw target/facing match up with a slightly more optimized version.
- Reduces the number of block properties grabbing by half. Vanilla calls getValue twice for each JigsawBlock to get top and front values from the same property when it could just grab it once and get all values it needs. Slightly improved the speed of getting the joints, targets, and name string values from the Jigsaw Blocks's NBT as well.
Also simplify joint data checking to not need to be converted to an Enum with byName (not performant) and reordering checks to reduce amount of logic that needs to run often.
This will help with Jigsaw structures that have a very high amount of Jigsaw Blocks in them as each Jigsaw Block runs through this matching code for all Jigsaw Blocks in the other structure pieces.
- Reduces the number of block properties grabbing by half. Vanilla calls getValue twice for each JigsawBlock to get top and front values from the same property when it could just grab it once and get all values it needs. Slightly improved the speed of getting the joints, targets, and name string values from the Jigsaw Blocks's NBT as well.
-
Made any giant structure NBT that has no finalizeProcessing StructureProcessor now load much faster.
- When a NBT structure piece goes to generate in a chunk, the ENTIRE NBT piece is loaded into memory, and then iterated over all the positions multiple times for StructureProcessors to apply and then later ignores all the positions outside the currently generating chunk. This is not very efficient (really wasteful) and causes large load times for giant NBT files.
This mod's optimization works by doing the bounds check early to strip out all positions that we don't need before passing it to the StructureProcessors. However, any StructureProcessor that overrides finalizeProcessing method could require all the NBT positions to function correctly so this optimization is disabled for any pieces with these kinds of StructureProcessors which will be very few structures. In vanilla, only Trail Ruins will not get this optimization due to its use of the Capped StructureProcessor that overrides finalizeProcessing method.
- When a NBT structure piece goes to generate in a chunk, the ENTIRE NBT piece is loaded into memory, and then iterated over all the positions multiple times for StructureProcessors to apply and then later ignores all the positions outside the currently generating chunk. This is not very efficient (really wasteful) and causes large load times for giant NBT files.
-
(1.21.1+) Replaced the Jigsaw Block list shuffling and prioritization logic in SinglePoolElement with a slightly faster version.
- Main benefit is slightly faster
selection_prioritydata grabbing from NBT by using a new method that grabs the entry once instead of twice. Vanilla does it twice to check the data type first before returning the value. A bit odd and wasteful.
Also tried a new sorting system for the prioritization, but only helps the few structures that actually makes use ofselection_prioritylike Trials Chamber. Overall, this is probably the weakest optimization but it does quite a bit of help for Jigsaw structures that has an absolutely ridiculous amount of Jigsaw Blocks. So it is worth keeping this optimization.
Might break parity with vanilla seeds in regard to what order the Jigsaw Block are ran in, but I could not find evidence of this yet.
- Main benefit is slightly faster
-
Skip running logic for SinglePoolElements we already checked and could not spawn at the current spot.
-
So this optimization comes from the fact that vanilla StructureTemplatePool holds a list of all the SinglePoolElements with duplicates of the elements based on their weight value. So if in a Template Pool, you specify weight 100 for a house, that house will be put into this list 100 times! And when generating the layout, the game will make a copy of this list, shuffle it, and iterate over the list to try and spawn the first piece it finds that fits. This means it will rerun the checking logic for duplicate entries in this list even if it was found to not fit before at the spot. The optimization I do is skip the logic for pieces we already checked before and continue to the next piece in the list. This gives a nice performance boost for structures that uses a lot of high weight values in their Template Pools.
-
Furthermore, you can turn on the
deduplicateShuffledTemplatePoolElementListconfig option to get even more performance out of high weight elements in Template Pools. The issue is this config comes at a cost of changing the layout of the structure. The layout is still valid and good. It would just be different than if the config option is kept off. Basically it breaks seed parity for structure layouts specifically to get that extra performance boost. -
*To be clear, seed parity is every time you use the seed 777 and there is a village at a spot with 2 blacksmith houses, every time you use that same seed, that village will remain at that spot with the two blacksmith houses. With this optimization config turned on and same seed, that village will still be at same spot. But it may have 1 blacksmith house this time. And every time you use the same seed with this config on, the village will continue to generate with the 1 blacksmith house. The layout is seed stable. But just no longer is the same as if the config is turned off.
-
-
(1.21.4 with v1.1.0+): Swap StructureTemplate$Palette's list of StructureBlockInfo objects to be a palette of StructureBlockInfo objects instead for reduced memory usage when not generating that cached StructureTemplate.
-
This optimization is a modified form from contaria's Glacier mod for 1.16.1. Special thanks to them for allowing me to use their code as a basis for this optimization!
-
Essentially in vanilla, every time a structure nbt file is loaded, it is converted to a StructureTemplate object and cached. Buried in that object is a list of ALL the positions from that nbt file paired with the blocks and nbt tag at each position. This is the biggest memory hog for StructureTemplate objects. What this mod will do is swap this list with a special palette data structure object under the hood that pretends to be a list. The memory size of the palette is significantly smaller than the list! Then when queried for worldgen, it will construct the list again as a WeakReference so worldgen remains fast and works properly. And when worldgen is done, this list will get garbage collected to free up memory again, allowing each StructureTemplate to only use the full amount of memory they need when doing worldgen but stay cached in a smaller form long term. The downside is this list cannot and should not be modified with add, remove, clear calls and will throw and exception if done. Luckily, I do not think any mod is modifying this list itself on StructureTemplate directly but if they do, please let me know.
-
ModernFix does have a mixin that will make the StructureTemplate objects themselves be a SoftReference and be removed completely from memory when the game gets full on memory. This is a valid solution and aims to stop the memory leak of loading structure nbts into a cache that never clears in long term gameplay. The downside of this is that the piece has to be loaded directly from the file again if it was cleared from memory. This mixin will work just fine alongside my mod's optimization. What will probably happen is my mod make StructureTemplate smaller in memory which allows ModernFix to keep more StructureTemplates in memory without them getting fully removed from memory. Which should allow more cache hits over long term. In theory.
-
下载 Structure Layout Optimizer
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Structure Layout Optimizer
下载 Structure Layout Optimizer
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Structure Layout Optimizer
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Structure Layout Optimizer
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Structure Layout Optimizer
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Structure Layout Optimizer
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Structure Layout Optimizer
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Structure Layout Optimizer
下载 Structure Layout Optimizer
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
v1.1.5
修复了 finalize 处理器检查逻辑中的内存泄漏问题
v1.0.12
修复了 finalize 处理器检查逻辑中的内存泄漏问题
v1.1.4
已更新至 26.3
关于这个资源的讨论、问题反馈与使用心得
还没有讨论,来发第一帖吧
你是这个资源的作者?登录后可认领此资源。
相似资源
模组
原版世界生成优化模组
模组
缓冲的原始输入和线程化的事件轮询
模组
让你能够在世界中的任何位置定位结构。
模组
一堆修复
模组
对 Minecraft 的沙漠神殿进行了全面重新设计!
模组
TerraFirmaCraft (TFC) 是一个全面转换模组,对生存模式进行了彻底的改造与重新设计。
评论 0
登录后参与评分
还没有评论,来抢沙发吧