StructPadding 1.1.0
dotnet add package StructPadding --version 1.1.0
NuGet\Install-Package StructPadding -Version 1.1.0
<PackageReference Include="StructPadding" Version="1.1.0" />
<PackageVersion Include="StructPadding" Version="1.1.0" />
<PackageReference Include="StructPadding" />
paket add StructPadding --version 1.1.0
#r "nuget: StructPadding, 1.1.0"
#:package StructPadding@1.1.0
#addin nuget:?package=StructPadding&version=1.1.0
#tool nuget:?package=StructPadding&version=1.1.0
StructPadding
StructPadding is a high-performance library for clearing (zeroing) padding bytes in unmanaged structures.
It ensures a deterministic memory state, which is critical for binary comparison (memcmp), hash calculation, and security sanitization.
Installation
| Package | Download |
|---|---|
| StructPadding |
dotnet add package StructPadding
Features
- Maximum Performance: Uses System.Reflection.Emit (IL Generation) to create dynamic code specific to each structure.
- Zero Allocation: No memory allocations during method calls (after cache warmup).
- Nested Support: Correctly handles complex structures with nested unmanaged types.
- Span-friendly: Optimized processing for arrays and Span<T>.
- Thread-Safe: Thread-safe caching of memory layouts.
Why is this needed?
In .NET (and other languages), structures are aligned in memory to optimize CPU access. This creates "holes" (padding) between fields containing random garbage.
Example structure:
[StructLayout(LayoutKind.Sequential)]
struct Example
{
public byte A; // 1 byte
// --- 3 bytes of garbage (padding) ---
public int B; // 4 bytes
}
Problems solved by StructPadding:
- Hashing: If you calculate a hash (CRC32, MD5, SHA) from the raw memory of a structure, garbage in the padding will result in different hashes for logically identical objects.
- Comparison (memcmp): You cannot simply compare two memory blocks to determine if the structures are equal.
- Security: Padding may contain residual data from RAM (passwords, keys) which could leak during serialization or memory dumps.
Usage
Zeroing a single structure
using StructPadding;
[StructLayout(LayoutKind.Sequential)]
public struct MyData
{
public byte Id;
public long Value; // There will be 7 bytes of padding before this field
}
public void Example()
{
MyData data = new MyData { Id = 1, Value = 100 };
// Before zeroing: padding bytes contain garbage
// After call: padding bytes are guaranteed to be 0
Zeroer.Zero(ref data);
}
Zeroing an Array or Span
The ZeroArray method is optimized for processing arrays without unnecessary overhead. It generates code that iterates through memory linearly.
public void ProcessBatch(Span<MyData> batch)
{
// Fast clearing of the entire array/span
Zeroer.ZeroArray(batch);
// Now the batch can be safely passed to native code,
// saved to disk, or hashed.
}
or
public void ProcessBatch(Span<MyData> batch)
{
batch.ZeroPadding();
}
Performance
The library analyzes the structure only once (upon first access). Based on this analysis, dynamic IL code (DynamicMethod) is generated to perform zero-writing directly at specific offsets.
This means:
- No reflection on the hot path.
- No foreach loops over field lists.
- Performance is comparable to manually written ptr[offset] = 0 code.
Requirements
The type T must have the unmanaged constraint (structs containing only primitives or other unmanaged structs).
License
MIT License. See LICENSE for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.0 | 263 | 11/21/2025 |
1.1.0: Fixed the processing of structures with the InlineArray attribute and fixed fields.
1.0.1: Added an extension method for T[]
1.0.0: First version