Skip to content

Use unaligned loads and stores in UnalignedCopy64/128 - #159

Merged
brantburnett merged 2 commits into
mainfrom
claude/unaligned-copy-arm32-safety-7a1f93
Nov 14, 2026
Merged

brantburnett merged 2 commits into
mainfrom
claude/unaligned-copy-arm32-safety-7a1f93

Conversation

@brantburnett

Copy link
Copy Markdown
Owner

Summary

UnalignedCopy64 and UnalignedCopy128 copied through Unsafe.As refs. That emits plain ldobj/stobj with no unaligned. prefix, which lets the JIT assume the address is aligned for long or Guid. Callers pass arbitrary byte offsets: op + 1, literal pointers and match sources. On ARM32 the JIT may therefore emit LDRD/STRD or LDM/STM, which trap on misaligned addresses. #157 documented this as a known limitation; this PR fixes it.

Changes

  • UnalignedCopy64 and UnalignedCopy128 now use Unsafe.ReadUnaligned and Unsafe.WriteUnaligned, which emit the unaligned. prefix. Their <safety> comments drop the ARM32 caveat.
  • The readonly Unsafe.As extension is removed from UnsafeReadonly. Nothing calls it any more, and it is the API that allowed the aligned-load pattern.
  • CopyHelpers.cs:49 still uses Unsafe.As for the SSSE3 shuffle-pattern table. That path runs only on x86, so it is left as is.

Performance

  • x64 codegen is identical. I compared DOTNET_JitDisasm output on net11.0 before and after: mov qword for the 64-bit copy and vmovups xmm for the 128-bit copy. The only difference is one fewer inlinee, the removed wrapper.
  • Benchmarks (net10.0, in-process, Default job): within noise.
Benchmark Before After
BlockCompressHtml 22.59 µs ± 0.44 22.09 µs ± 0.10
BlockDecompressHtml 9.76 µs ± 0.15 9.54 µs ± 0.07

The UnalignedCopy64/UnalignedCopy128 micro-benchmarks measure under 1 ns, below resolution, so the disassembly comparison is the meaningful check for them.

Notes for reviewers

  • Not run on ARM32. I don't have ARM32 hardware or an emulator set up, so the ARM32 fix rests on the IL semantics, not on observed output.
  • Benchmarks ran with --inProcess. Since Enable C# 15 memory safety rules and annotate unsafe code #157, BenchmarkDotNet's generated boilerplate fails to build under the memory-safety rules (CS9377), so the default out-of-process toolchain can't run. That is unrelated to this change and is being fixed separately.

Testing

  • dotnet build -c Release: succeeded.
  • dotnet test --project Snappier.Tests -c Release: 1,544 passed, 0 failed, across net8.0, net9.0, net10.0, net11.0 and net48.

🤖 Generated with Claude Code

UnalignedCopy64 and UnalignedCopy128 copied through Unsafe.As refs, which
emit plain ldobj/stobj and let the JIT assume natural alignment. Callers
pass arbitrary byte offsets, so on ARM32 the JIT could legally emit
LDRD/STRD or LDM/STM, which trap on misaligned addresses.

Switch both to Unsafe.ReadUnaligned/WriteUnaligned, which emit the
unaligned. prefix. x64 codegen is unchanged (mov qword / vmovups xmm),
and block compress/decompress benchmarks are within noise.

Remove the readonly Unsafe.As extension from UnsafeReadonly, which has
no remaining callers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 14, 2026 17:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the fixed-size copy helpers’ switch to explicit unaligned memory access.

No open issues 🎉

What changed in this PR

This PR addresses the ARM32 alignment limitation documented in #157 by making the fixed-size copy helpers explicitly use unaligned memory access.

Changes:

  • Replace typed loads and stores with Unsafe.ReadUnaligned and Unsafe.WriteUnaligned.
  • Update safety contracts to remove alignment restrictions and document overlap support.
  • Remove the now-unused readonly Unsafe.As helper.
File Description
Snappier/​Internal/​UnsafeReadonly.cs Removes the unused readonly reinterpretation helper and its safety contract.
Snappier/​Internal/​CopyHelpers.cs Uses explicit unaligned operations for 64-bit and 128-bit copies and updates safety comments.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Final approval should wait for human validation of the ARM32 unaligned loads and stores through runtime tests or disassembly, which this environment cannot perform.

No open issues 🎉

Review effort: Lite

@brantburnett
brantburnett enabled auto-merge (squash) November 14, 2026 17:44
@brantburnett
brantburnett merged commit 276a99a into main Nov 14, 2026
52 checks passed
@brantburnett
brantburnett deleted the claude/unaligned-copy-arm32-safety-7a1f93 branch November 14, 2026 17:45
ogonna-ee pushed a commit to DEFRA/waste-organisations that referenced this pull request Nov 16, 2026
Updated [Snappier](https://github.com/brantburnett/Snappier) from 1.3.1
to 1.3.2.

<details>
<summary>Release notes</summary>

_Sourced from [Snappier's
releases](https://github.com/brantburnett/Snappier/releases)._

## 1.3.2

## What's Changed
* Support fork pull requests in build workflow by @​brantburnett in
brantburnett/Snappier#151
* Build with the .NET 11 SDK and C# 15 by @​brantburnett in
brantburnett/Snappier#153
* Migrate tests to xunit.v3 4.x on Microsoft Testing Platform v2 by
@​brantburnett in brantburnett/Snappier#154
* Fix literal overrun validation in Snappy decompression by
@​brantburnett in brantburnett/Snappier#156
* Enable C# 15 memory safety rules and annotate unsafe code by
@​brantburnett in brantburnett/Snappier#157
* Keep memory safety rules out of BenchmarkDotNet's generated project by
@​brantburnett in brantburnett/Snappier#160
* Use unaligned loads and stores in UnalignedCopy64/128 by
@​brantburnett in brantburnett/Snappier#159
* Move hot-path exception messages into NoInlining throw helpers by
@​brantburnett in brantburnett/Snappier#158
* Upgrade BenchmarkDotNet to 0.16.0-preview.2 and benchmark .NET 11 by
@​brantburnett in brantburnett/Snappier#161
* Compare buffer distances instead of forming out-of-range refs by
@​brantburnett in brantburnett/Snappier#162
* Reject undersized data chunks in framed stream decompression by
@​brantburnett in brantburnett/Snappier#163
* Use five launches for Default benchmark jobs by @​brantburnett in
brantburnett/Snappier#164
* Keep rare decompression paths out of the hot loop under Dynamic PGO by
@​brantburnett in brantburnett/Snappier#165


**Full Changelog**:
brantburnett/Snappier@release/1.3.1...release/1.3.2

Commits viewable in [compare
view](brantburnett/Snappier@release/1.3.1...release/1.3.2).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=Snappier&package-manager=nuget&previous-version=1.3.1&new-version=1.3.2)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore <dependency name> major version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's major version (unless you unignore this specific
dependency's major version or upgrade to it yourself)
- `@dependabot ignore <dependency name> minor version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's minor version (unless you unignore this specific
dependency's minor version or upgrade to it yourself)
- `@dependabot ignore <dependency name>` will close this group update PR
and stop Dependabot creating any more for the specific dependency
(unless you unignore this specific dependency or upgrade to it yourself)
- `@dependabot unignore <dependency name>` will remove all of the ignore
conditions of the specified dependency
- `@dependabot unignore <dependency name> <ignore condition>` will
remove the ignore condition of the specified dependency and ignore
conditions


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
ooples pushed a commit to ooples/AiDotNet that referenced this pull request Nov 16, 2026
Updated
[coverlet.collector](https://github.com/coverlet-coverage/coverlet) from
10.1.0 to 10.1.1.

<details>
<summary>Release notes</summary>

_Sourced from [coverlet.collector's
releases](https://github.com/coverlet-coverage/coverlet/releases)._

## 10.1.1

### Improvements

- Add `IsReachable` to branch points, computed from the method's
control-flow graph, and exclude unreachable branches from
branch-coverage totals and from branch details in Cobertura, LCOV and
OpenCover reports
[#​2056](coverlet-coverage/coverlet#2056) by
@​pcshrosbree
- coverlet.MTP: Narrow Dynamic Exclude Filters for Loaded Assemblies
[#​2069](coverlet-coverage/coverlet#2069)

### Fixed

- Fix Regression Prevent invalid IL from branch trampolines
[#​2067](coverlet-coverage/coverlet#2067)
- Fix Regression coverlet.MTP does not instrument local functions
[#​2046](coverlet-coverage/coverlet#2046)

## Maintenance

- Update NuGet package versions
[#​2050](coverlet-coverage/coverlet#2050)

[Diff between 10.1.0 and
10.1.1](coverlet-coverage/coverlet@v10.1.0...v10.1.1)

Commits viewable in [compare
view](coverlet-coverage/coverlet@v10.1.0...v10.1.1).
</details>

Updated
[Microsoft.CodeAnalysis.Analyzers](https://github.com/dotnet/roslyn)
from 5.9.0 to 5.12.0.

<details>
<summary>Release notes</summary>

_Sourced from [Microsoft.CodeAnalysis.Analyzers's
releases](https://github.com/dotnet/roslyn/releases)._

No release notes found for this version range.

Commits viewable in [compare
view](https://github.com/dotnet/roslyn/commits).
</details>

Updated Microsoft.CodeAnalysis.CSharp from 4.14.0 to 5.12.0.

Updated [Snappier](https://github.com/brantburnett/Snappier) from 1.3.1
to 1.3.2.

<details>
<summary>Release notes</summary>

_Sourced from [Snappier's
releases](https://github.com/brantburnett/Snappier/releases)._

## 1.3.2

## What's Changed
* Support fork pull requests in build workflow by @​brantburnett in
brantburnett/Snappier#151
* Build with the .NET 11 SDK and C# 15 by @​brantburnett in
brantburnett/Snappier#153
* Migrate tests to xunit.v3 4.x on Microsoft Testing Platform v2 by
@​brantburnett in brantburnett/Snappier#154
* Fix literal overrun validation in Snappy decompression by
@​brantburnett in brantburnett/Snappier#156
* Enable C# 15 memory safety rules and annotate unsafe code by
@​brantburnett in brantburnett/Snappier#157
* Keep memory safety rules out of BenchmarkDotNet's generated project by
@​brantburnett in brantburnett/Snappier#160
* Use unaligned loads and stores in UnalignedCopy64/128 by
@​brantburnett in brantburnett/Snappier#159
* Move hot-path exception messages into NoInlining throw helpers by
@​brantburnett in brantburnett/Snappier#158
* Upgrade BenchmarkDotNet to 0.16.0-preview.2 and benchmark .NET 11 by
@​brantburnett in brantburnett/Snappier#161
* Compare buffer distances instead of forming out-of-range refs by
@​brantburnett in brantburnett/Snappier#162
* Reject undersized data chunks in framed stream decompression by
@​brantburnett in brantburnett/Snappier#163
* Use five launches for Default benchmark jobs by @​brantburnett in
brantburnett/Snappier#164
* Keep rare decompression paths out of the hot loop under Dynamic PGO by
@​brantburnett in brantburnett/Snappier#165


**Full Changelog**:
brantburnett/Snappier@release/1.3.1...release/1.3.2

Commits viewable in [compare
view](brantburnett/Snappier@release/1.3.1...release/1.3.2).
</details>

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore <dependency name> major version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's major version (unless you unignore this specific
dependency's major version or upgrade to it yourself)
- `@dependabot ignore <dependency name> minor version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's minor version (unless you unignore this specific
dependency's minor version or upgrade to it yourself)
- `@dependabot ignore <dependency name>` will close this group update PR
and stop Dependabot creating any more for the specific dependency
(unless you unignore this specific dependency or upgrade to it yourself)
- `@dependabot unignore <dependency name>` will remove all of the ignore
conditions of the specified dependency
- `@dependabot unignore <dependency name> <ignore condition>` will
remove the ignore condition of the specified dependency and ignore
conditions


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.

2 participants