Skip to content

[C#] Add a typed, initialising grow/shrink API (RawMem parity); stale bytes reappear after shrink + grow #100

Backlog

Description

@konard

Problem

C# exposes only raw UsedCapacity / ReservedCapacity byte counters. Growing UsedCapacity again after shrinking it exposes stale bytes (probe P6: the byte 42 comes back). There is also no way to say "grow by N elements filled with X".

mem-rs RawMem provides len, grow_filled, grow_zeroed, grow_with, grow_from_slice, shrink, truncate, clear, resize, and Resizable::{reserve, reserve_exact, shrink_to_fit, spare_capacity}. It also has a typed slice view.

Proposed solution

Add IRawMemory<T> where T : unmanaged with:

  • long Length;
  • Span<T> AsSpan() and Span<T> Slice(long start, int length);
  • GrowFilled(long count, T value), GrowZeroed(long count), GrowWith(long count, Func<long, T>), GrowFrom(ReadOnlySpan<T>);
  • Shrink(long count), Truncate(long length), Clear(), Resize(long length, T fill);
  • Reserve(long additional) and ShrinkToFit().

Implement it once over IResizableDirectMemory (heap, file-mapped, temporary) through MemoryMarshal/Unsafe and NativeMemory.Clear. A shrink must not leave readable stale data behind for the next grow: zero the tail on grow, as mem-rs does. Keep the old properties as thin wrappers for compatibility.

Libraries: System.Memory (Span<T>, MemoryMarshal), System.Runtime.CompilerServices.Unsafe, optionally CommunityToolkit.HighPerformance (Span2D, MemoryOwner<T>).


Part of #94 (catch up with mem-rs). Analysis and evidence: case study.

Metadata

Metadata

Assignees

No one assigned

    Labels

    csharpenhancementmem-rs-parity

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions