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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright Peter G. Bower 2025-2026.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! # Stream Buffer - Wire Alignment Abstraction
//!
//! Controls the alignment of Arrow IPC frame boundaries on the wire.
//!
//! ## Wire alignment parameter `B`
//!
//! Throughout the library, the generic parameter `B: StreamBuffer` determines
//! how Arrow IPC frames are padded on the wire:
//!
//! - **`Vec64<u8>` (ALIGN=64)** - 64-byte SIMD-aligned frames. Use for
//! lightstream-to-lightstream communication where both sides are this
//! library. Column buffers land on 64-byte boundaries, enabling zero-copy
//! `Buffer::from_shared` without alignment fixup copies. This is the
//! recommended default.
//!
//! - **`Vec<u8>` (ALIGN=8)** - Standard 8-byte aligned frames. Use when
//! reading Arrow IPC data produced by external tools (incl. PyArrow, Arrow C++,
//! etc.) that use the minimum spec alignment. Also safe for writing data
//! consumed by external readers.
//!
//! ## Receiver side
//!
//! On the decode side, the library always reads body data into a `Vec64<u8>`
//! internally, regardless of `B`. The `B` parameter only affects frame
//! boundary calculations (where metadata padding and body padding land).
//! Column data is mapped via `SharedBuffer` for zero-copy access.
//! This allows compatibility with bytes from a different sources whilst
//! ensuring data is captured into 64-byte aligned SIMD-ready vectors.
//!
//! ## Wire padding overhead
//!
//! With `Vec64<u8>`, metadata and body sections are padded to 64-byte
//! boundaries. This adds at most 63 bytes of padding per section compared
//! to the 8-byte minimum. For a typical record batch of tens of KB or
//! more, this is well under 1% overhead on the wire. The trade-off is
//! worthwhile when targeting SIMD cache alignment because the receiver can map
//! column buffers directly from the SharedBuffer without alignment fixup copies,
//! which would otherwise cost far more than the extra padding bytes. Consequently,
//! SIMD calculations are available on the buffers permanently via `Minarrow`.
//!
//! With `Vec<u8>`, padding follows the Arrow spec minimum of 8 bytes.
//! Column buffers may not be 64-byte aligned on arrival, so
//! `Buffer::from_shared` will copy into an aligned Vec64, as this is enforced by Minarrow
//! to support its central SIMD compatibility and cache-optimal promise.
//! This means that if data was written via an 8-byte Arrow implementation there is one memory copy,
//! to resolve high-performance data buffers once at source ingest.
//!
//! ## Choosing B
//!
//! - Lightstream protocol connections: `Vec64<u8>` - both sides are controlled
//! - Arrow IPC transport for internal use: `Vec64<u8>` - best performance
//! - Arrow IPC transport for interop with external readers: `Vec<u8>` - spec minimum alignment
use Vec64;
use SharedBuffer;
/// Wire alignment buffer for Arrow IPC frame encoding and decoding.
///
/// The `ALIGN` constant controls how IPC frames are padded on the wire.
/// This is the single parameter that determines whether frames use
/// 64-byte SIMD alignment or standard 8-byte Arrow spec alignment.
///
/// Implemented for `Vec<u8>` with ALIGN=8 and `Vec64<u8>` with ALIGN=64.
/// The receiver always reads body data into a Vec64 internally -
/// `B` only affects frame boundary padding calculations.