[#12590] 一次写入多个相邻元素
变更摘要
本 PR 为
write指令增加了length参数,允许一次写入多个相邻元素,旨在提升内存单元清除速度。默认长度为 1 以保持向后兼容性,各相关实现均已更新或做了忽略处理。
状态 Closed · 目标 master ← pr/multiple-writes
文件 6 · 代码变化 +26 / -16
类型 Feature · Enhancement
主要变更
为
write指令引入length参数,支持批量写入。更新
LWritable接口签名,write方法新增length参数。MemoryBlock使用Arrays.fill实现高效的数组批量填充。CanvasBlock通过循环实现批量像素设置并增加边界检查。LogicBlock更新签名但忽略length参数,因其地址类型为字符串。
影响范围
Logic System
Memory Block
Canvas Block
可能影响
Improves logic execution performance for batch operations
Backward compatible with existing logic code
以上影响分析根据 PR 描述及代码修改推断,不代表维护者正式结论。
重点文件
core/src/mindustry/world/blocks/logic/MemoryBlock.java
修改 · +7 / -5
实现
write方法,利用Arrays.fill批量设置内存数组。增加对写入长度和边界的检查以防止越界。
core/src/mindustry/world/blocks/logic/CanvasBlock.java
修改 · +7 / -4
setPixel方法新增length参数,通过循环批量设置像素。限制写入范围不超过画布大小。
core/src/mindustry/logic/LStatements.java
修改 · +6 / -2
WriteStatement增加length字段(默认值为 "1")及对应的 UI 输入框。
core/src/mindustry/logic/LExecutor.java
修改 · +4 / -3
WriteI指令类新增length变量并在执行时传递给write方法。
core/src/mindustry/logic/LWritable.java
修改 · +1 / -1
接口
write方法签名增加LVar length参数。
core/src/mindustry/world/blocks/logic/LogicBlock.java
修改 · +1 / -1
更新
write方法签名以匹配接口,实际忽略length参数。
作者说明
本 PR 为 write 指令添加了一个 length 参数。它指定了要写入的元素数量。主要目标是允许更快地清除内存单元,例如 write 0 cell1 0 64 可以在单条指令中清除整个内存单元。length 的默认值为 1,这意味着旧代码的工作方式与以前相同。
LWritable.write() 方法已更新以接受 length 参数。CanvasBlock 和 MemoryBlock 实现将长度限制为支持的最大值,因此提供像 1e20 这样不合理的数字不会使处理器陷入循环挂起。LogicBlock 实现完全忽略该参数,因为 address 是字符串,而 length 在这种情况下没有意义。
MemoryBlock 使用 Arrays.fill() 来设置数组。CanvasBlock 使用循环;它本可以进行优化,如果有需要我会做,但我认为非必要情况下不想冒险引入 bug。
如果你的 Pull Request 不涉及翻译或服务器列表,请阅读下方的需求列表并勾选每个复选框:
我已阅读贡献指南。
如果适用,我已确保我的代码可以编译。
如果适用,我已确保此 PR 中的任何新功能在游戏中正常运行。
GitHub 原文
This PR adds a length parameter to the write instruction. It specifies the number of elements to write to. The primary goal is to allow clearing memory cells faster, e.g. write 0 cell1 0 64 clears the entire memory cell in a single instruction. The default value of length is 1, meaning that old code works the same as before.
The LWritable.write() method has been updated to accept the length parameter. The CanvasBlock and MemoryBlock implementations limit the length to the maximum supported value, so that providing an absurd number, such as 1e20, doesn't hang the processor in a loop. The LogicBlock implementation ignores the parameter entirely, as the address is a string and length doesn't make sense in that context.
MemoryBlock uses Arrays.fill() to set the arrays. The CanvasBlock uses loops; it could be optimized and I'll do it if required, but I didn't want to risk introducing bugs if not necessary.
If your pull request is not translation or serverlist-related, read the list of requirements below and check each box:
I have read the contribution guidelines.
I have ensured that my code compiles, if applicable.
I have ensured that any new features in this PR function correctly in-game, if applicable.
由 MDT GitHub 同步机器人自动同步。中文翻译、摘要及代码影响分析由 AI 生成,仅供参考。