Skip to content

[GFX-217] 消除 XxHashHelper Hash64Core 的 Sonar S6640 unsafe 违规 - #155

Merged
AlianBlank merged 1 commit into
mainfrom
feature/GFX-217
Aug 5, 2026
Merged

AlianBlank merged 1 commit into
mainfrom
feature/GFX-217

Conversation

@AlianBlank

@AlianBlank AlianBlank commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Linear: GFX-217

Summary by CodeRabbit

  • 改进
    • 优化 64 位 xxHash 的数据读取方式,提升跨平台兼容性与安全性。
    • 保持现有公开接口不变,确保现有调用方式无需调整。

XxHashHelper.InternalXxHashHelper.Hash64Core 将签名从 unsafe byte* + int length 改为 ulong Hash64Core(ReadOnlySpan<byte> input, uint seed = 0),长度由 input.Length 派生;使用 BinaryPrimitives.ReadUInt64LittleEndian / ReadUInt32LittleEndian 安全读取 8/4 字节,输入偏移以本地 offset 变量推进。同步更新 ComputeHash64(byte[]) 调用方,移除 unsafe 关键字与 fixed (byte*) 块,直接传入 buffer 隐式转为 ReadOnlySpan<byte>。算法常量/混合/最终 avalanche 与原实现完全等价,公共 Hash64(byte[]/string/Type/<T>) 签名与参数校验均不变;现有 XxHashHelperTests 63 个测试(空数组/单字节/中文/长字符串/Unicode/性能/Null 校验/重复一致性等)全部通过,整库 1807 个测试 0 失败。Linear: GFX-217
@linear-code

linear-code Bot commented Aug 5, 2026

Copy link
Copy Markdown

GFX-217

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d2a80773-61a9-42ba-bf0b-470f2ca0847d

📥 Commits

Reviewing files that changed from the base of the PR and between 5cc9306 and 1241f2a.

📒 Files selected for processing (1)
  • GameFrameX.Foundation.Hash/XxHashHelper.cs

📝 Walkthrough

Walkthrough

本次变更将 xxHash64 的核心输入读取改为 ReadOnlySpan<byte>BinaryPrimitives,并更新 ComputeHash64(byte[]) 入口以直接调用新的核心方法。

Changes

xxHash64 Span 读取重构

Layer / File(s) Summary
xxHash64 核心读取改造
GameFrameX.Foundation.Hash/XxHashHelper.cs
Hash64Core 改用 ReadOnlySpan<byte>。主循环和尾部处理使用显式偏移量及 BinaryPrimitives 进行小端读取。
字节数组入口接入
GameFrameX.Foundation.Hash/XxHashHelper.cs
ComputeHash64(byte[]) 移除固定指针和显式长度计算,直接传入字节数组。

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了将 Hash64Core 的 unsafe 实现改为安全实现并消除 Sonar S6640 违规这一主要变更。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/GFX-217

Comment @coderabbitai help to get the list of available commands.

@sonarqubecloud

sonarqubecloud Bot commented Aug 5, 2026

Copy link
Copy Markdown

@AlianBlank
AlianBlank merged commit 7ce3c1c into main Aug 5, 2026
2 checks passed
@AlianBlank
AlianBlank deleted the feature/GFX-217 branch August 5, 2026 05:28
@AlianBlank

Copy link
Copy Markdown
Contributor Author

GFX-217 等价替代方案(仅供参考,不阻塞):

注意到 InternalXxHashHelper.Hash64Core 整段手写 xxHash64 与本包已引用的 Standart.Hash.xxHash 库(外层 Hash64(byte[]/string) 已直接调用 xxHash64.ComputeHash)功能重复。另一条消除 S6640 的路线是:直接删除 Hash64CoreComputeHash64(byte[]) 改为 return xxHash64.ComputeHash(buffer);——可减少 ~125 行冗余手写 xxHash64 代码,且与 GFX-218/C30(同文件 32 位路径)的处理方向一致。

本 PR 的 span 重写(ReadOnlySpan<byte> + BinaryPrimitives)同样正确消除了 line 358 的 unsafe 块,公共 API/校验/返回类型不变。两种方案都 OK,提此仅作简化建议。

AlianBlank added a commit that referenced this pull request Aug 5, 2026
XxHashHelper.InternalXxHashHelper 的 32 位手写核心从 unsafe byte* + int length 改为 uint Hash32Core(ReadOnlySpan<byte> input, uint seed = 0),长度由 input.Length 派生;使用 BinaryPrimitives.ReadUInt32LittleEndian 安全读取 4 字节,输入偏移以本地 offset 变量推进。同步更新 ComputeHash32(byte[]) 调用方,移除 unsafe 关键字与 fixed (byte*) 块(即 Sonar line 487 的 S6640 违规点),直接传入 buffer 隐式转为 ReadOnlySpan<byte>。算法常量/16 字节块混合/尾轮/avalanche 与原实现完全等价,小端平台逐位一致;公共 ComputeHash32(string/Type/<T>) 与外层 Hash32(...) 签名/校验/返回类型不变。命名从 ComputeHash32 改为 Hash32Core,与已合并的 64 位 Hash64Core(PR #155)对齐,同时避免与 ComputeHash32(byte[]) 重载决议递归。现有 XxHashHelperTests 63 个测试全部通过,整库 1807 个测试 0 失败。Linear: GFX-219
AlianBlank added a commit that referenced this pull request Aug 5, 2026
InternalXxHashHelper 的 32 位 unsafe 指针核心 ComputeHash32(byte*,int,uint) 改写为安全 span 核心 Hash32Core(ReadOnlySpan<byte>,uint),长度由 input.Length 派生,用 BinaryPrimitives.ReadUInt32LittleEndian 安全读取 4 字节块、input[offset] 读单字节、本地 offset 推进偏移;调用方 ComputeHash32(byte[]) 移除 unsafe/fixed 改为 return Hash32Core(buffer)。核心重命名为 Hash32Core(与 64 位 Hash64Core 对称)以避免 ComputeHash32(byte[]) 的 return ComputeHash32(buffer) 被 C# 重载决议命中自身 byte[] 重载导致无限递归。算法常量/位移/混合/avalanche 与原实现逐行等价,公共 ComputeHash32(byte[]/string/Type/<T>) 签名与参数校验不变;对齐已合入的 GFX-217/#155(64 位 Hash64Core span 改写)范式。现有 XxHashHelperTests 63 个测试全通过,整库 1807 个测试 0 失败。

Linear: GFX-220
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant