Commit b86b348
committed
refactor(iterators): NDIterator fully backed by NpyIter state
Replaces the lazy-but-standalone ValueOffsetIncrementor path with one
that constructs an NpyIter state and drives MoveNext / HasNext / Reset
directly off that state. NDIterator is now an honest thin wrapper
over NpyIter — the same traversal machinery used by all the Phase 2
production call sites — rather than reimplementing the coord-walk
logic with legacy incrementors.
How it works
------------
- ctor calls NpyIterRef.New(arr, NPY_CORDER) to build the state, then
transfers ownership of the NpyIterState* pointer out of the ref
struct (see NpyIterRef.ReleaseState / FreeState below). The class
holds that pointer for its lifetime and frees it in Dispose (or in
the finalizer as a safety net).
- MoveNext reads `*(TOut*)state->DataPtrs[0]` then calls
`state->Advance()`. IterIndex tracks position, IterEnd bounds the
non-AutoReset case, and `state->Reset()` restarts from IterStart on
AutoReset wraparound and on explicit Reset.
- Cross-dtype wraps the same read with a Converts.FindConverter<TSrc, TOut>
lookup — one switch at construction picks the typed helper, so the
per-element hot path is still just one read + one converter delegate
call. MoveNextReference throws when casting is in play, matching the
legacy contract.
- NPY_CORDER is explicit so iterating a transposed view yields the
logical row-major order the old NDIterator provided. Without it,
KEEPORDER would give memory-efficient order (which e.g.
`b.T.AsIterator<int>()` would surface as `0 1 2 ... 11` instead of
the expected `0 4 8 1 5 9 2 6 10 3 7 11`).
NpyIter additions
-----------------
- NpyIterRef.ReleaseState(): hand the owned NpyIterState* to a caller
who needs it across a non-ref-struct boundary (e.g. a class field).
Marks the ref struct as non-owning so its Dispose is a no-op.
- NpyIterRef.FreeState(NpyIterState*): static tear-down mirror of
Dispose's cleanup path — frees buffers (when BUFFER set), calls
FreeDimArrays, and NativeMemory.Free's the state pointer. The
long-lived owner calls this from its own Dispose/finalizer.
Bug fixes along the way
-----------------------
NpyIter initialization previously computed base pointers as
`(byte*)arr.Address + (shape.offset * arr.dtypesize)` in two places
(initial broadcast setup on line 340 and ResetBasePointers on line
1972). `arr.dtypesize` goes through `Marshal.SizeOf(bool) == 4` because
bool is marshaled to win32 BOOL, but the in-memory `bool[]` storage is
1 byte per element. For strided bool arrays this produced a base
pointer 4× too far into the buffer.
Switched both sites to `arr.GetTypeCode.SizeOf()` which returns the
actual in-memory size (1 for bool). Surfaced by `Boolean_Strided_Odd`
once NDIterator started routing through NpyIter — previously only
LATENT because the legacy NDIterator path computed offsets in
element units, not bytes, and sidestepped the NpyIter init.
Test impact: 6,748 / 6,748 passing on net8.0 and net10.0 (CI filter:
TestCategory!=OpenBugs&TestCategory!=HighMemory). Smoke test of
same-type contig / cross-type / strided / transposed / broadcast /
AutoReset / Reset / foreach all produce the expected element sequences.1 parent fb4b7dc commit b86b348
8 files changed
Lines changed: 3740 additions & 170 deletions
File tree
- docs
- plans
- src/NumSharp.Core/Backends/Iterators
- tools/iterator_parity
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
0 commit comments