rumtk-arena 0.5.3

Arena/Bump allocator library to support performance critical portions of the rumtk framework.
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 *     rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
 *     This toolkit aims to be reliable, simple, performant, and standards compliant.
 *     Copyright (C) 2026  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
 *     Copyright (C) 2026  MedicalMasses L.L.C. <contact@medicalmasses.com>
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
use crate::base::RUMVec;
pub use branches::{likely as cpu_likely_branch, prefetch_read_data, unlikely as cpu_unlikely_branch};
pub use std::simd::prelude::*;
use crate::rumtk_mem_quick_array_init;

pub const CPU_L1_PREFETCH: i32 = 0;
pub const CPU_L2_PREFETCH: i32 = 1;
pub const CPU_L3_PREFETCH: i32 = 2;
pub const CPU_NONTEMPORAL_PREFETCH: i32 = 3;
pub const CPU_L1_CACHE_LINE_SIZE: usize = 64; // Number of bytes in a typical x86_64 CPU L1 cache line.
pub const CPU_L1_CACHE_SIZE: usize = 32 * 1024; // Number of bytes in a typical x86_64 CPU L1 cache per core.
pub const CPU_PAGE_SIZE: usize = 4 * 1024; // Typical CPU page size
pub const CPU_SIMD_64_SIZE: usize = 64;
pub const CPU_SIMD_32_SIZE: usize = 32;
pub const CPU_SIMD_16_SIZE: usize = 16;
pub const CPU_SIMD_8_SIZE: usize = 8;
pub const CPU_SIMD_AVERAGE_ALU_COUNT: usize = 4;
pub const CPU_SEARCH_WINDOW_1024_SIZE: usize = 1024;
pub const CPU_SEARCH_WINDOW_512_SIZE: usize = 512;
pub const CPU_SEARCH_WINDOW_256_SIZE: usize = 256;
pub const CPU_SEARCH_WINDOW_128_SIZE: usize = 128;
pub const CPU_SEARCH_WINDOW_64_SIZE: usize = 64;
pub const CPU_SEARCH_WINDOW_32_SIZE: usize = 32;
pub const CPU_SEARCH_WINDOW_16_SIZE: usize = 16;


#[cfg(feature = "simd")]
pub type u8xN<const SEARCH_WINDOW_SIZE: usize> = Simd<u8, SEARCH_WINDOW_SIZE>;

////////////////////////////////////////CPU CACHE HINTS///////////////////////////////
#[inline]
pub fn cpu_l3_prefetch(data: *const u8) {
    prefetch_read_data::<u8, CPU_L3_PREFETCH>(data);
}

#[inline]
pub fn cpu_l2_prefetch(data: *const u8) {
    prefetch_read_data::<u8, CPU_L2_PREFETCH>(data);
}

#[inline]
pub fn cpu_l1_prefetch(data: *const u8) {
    prefetch_read_data::<u8, CPU_L1_PREFETCH>(data);
}

#[inline(always)]
pub fn cpu_slice_to_array<const SLICE_SIZE: usize>(chunk: &[u8]) -> [u8; SLICE_SIZE] {
    chunk.try_into().expect("length mismatch")
}

#[inline(always)]
pub fn cpu_slice_to_array_padded<const SLICE_SIZE: usize, const PAD: u8>(chunk: &[u8]) -> [u8; SLICE_SIZE] {
    let mut result = [PAD; SLICE_SIZE];
    result[..chunk.len()].copy_from_slice(chunk);
    result
}

#[cfg(feature = "simd")]
#[inline(always)]
pub fn cpu_slice_to_simd<const SLICE_SIZE: usize>(chunk: &[u8]) -> u8xN<SLICE_SIZE> {
    u8xN::from_slice(chunk)
}

#[cfg(feature = "simd")]
#[inline(always)]
pub fn cpu_slice_to_simd_padded<const SLICE_SIZE: usize, const PAD: u8>(chunk: &[u8]) -> u8xN<SLICE_SIZE> {
    u8xN::from_array(cpu_slice_to_array_padded::<SLICE_SIZE, 0>(chunk))
}

#[inline(always)]
pub fn cpu_slice_splat<const SLICE_SIZE: usize>(input: &[u8]) -> [u8; SLICE_SIZE] {
    let mut result = [0u8; SLICE_SIZE];

    for chunk in result.chunks_mut(input.len()) {
        chunk.copy_from_slice(&input[..chunk.len()]);
    }

    result
}

////////////////////////////////////////SIMD MASKS//////////////////////////////////////////
#[cfg(feature = "simd")]
#[inline(always)]
pub fn cpu_simd_shift_right_n<const LANE_SIZE: usize, const SHIFT: usize, const PAD: u8>(item: &u8xN<LANE_SIZE>) -> u8xN<LANE_SIZE> {
    item.shift_elements_right::<SHIFT>(PAD)
}

#[cfg(feature = "simd")]
#[inline(always)]
pub fn cpu_simd_masks<const LANE_SIZE: usize>(pattern: &[u8]) -> RUMVec<u8xN<LANE_SIZE>> {
    let mask = u8xN::<LANE_SIZE>::from_array(cpu_slice_splat(pattern));
    let mut masks = RUMVec::<u8xN<LANE_SIZE>>::with_capacity(pattern.len());

    masks.push(mask);

    for i in 1..pattern.len() {
        let shifted = cpu_simd_shift_right_n::<LANE_SIZE, 1, 0>(&mask);
        masks.push(shifted);
    }

    masks
}

////////////////////////////////////////SEARCH FOR NEEDLE IN HAYSTACK///////////////////////////////

#[inline(always)]
pub fn cpu_find_fallback(chunk: &[u8], byte: u8) -> Option<usize> {
    chunk.iter().position(|c| *c==byte)
}

#[cfg(feature = "simd")]
#[inline(always)]
fn cpu_find_simd_avx2_n<const SEARCH_WINDOW_SIZE: usize>(data_vec: u8xN<SEARCH_WINDOW_SIZE>, target: u8xN<SEARCH_WINDOW_SIZE>) -> Option<usize> {
    let mask = data_vec.simd_eq(target);

    if mask.any() {
        let bitmask = mask.to_bitmask();
        let lane_i = bitmask.trailing_zeros() as usize;
        return Some(lane_i);
    }

    None
}

#[cfg(feature = "simd")]
#[inline(always)]
fn cpu_find_simd_avx2_unpadded<const SEARCH_WINDOW_SIZE: usize>(chunk: &[u8], target: u8xN<SEARCH_WINDOW_SIZE>) -> Option<usize> {
    let data_vec = cpu_slice_to_simd::<SEARCH_WINDOW_SIZE>(chunk);
    cpu_find_simd_avx2_n(data_vec, target)
}

#[cfg(feature = "simd")]
#[inline(always)]
fn cpu_find_simd_avx2_padded<const SEARCH_WINDOW_SIZE: usize>(chunk: &[u8], target: u8xN<SEARCH_WINDOW_SIZE>) -> Option<usize> {
    let data_vec = cpu_slice_to_simd_padded::<SEARCH_WINDOW_SIZE, 0>(chunk);
    cpu_find_simd_avx2_n(data_vec, target)
}

///
/// Use SIMD to find the index of a byte in a byte slice.
///
///
#[cfg(feature = "simd")]
#[inline(always)]
pub fn cpu_find_simd_n<const LANE_SIZE: usize>
(
    chunk: &[u8],
    byte: u8,
) -> Option<usize>
{
    let mask = u8xN::<LANE_SIZE>::splat(byte);
    let needs_padding = chunk.len() % LANE_SIZE;
    let unpadded = chunk.len() - needs_padding; // The compiler will optimize this with a constant per my compiler explorer experiment
    // https://godbolt.org/z/fhh5nG34f

    for (i,window) in chunk[..unpadded].chunks(LANE_SIZE).enumerate() {
        cpu_l1_prefetch(window.as_ptr());
        if let Some(lane_i) = cpu_find_simd_avx2_unpadded::<LANE_SIZE>(window, mask) {
            return Some(i * LANE_SIZE + lane_i);
        }
    }

    cpu_find_simd_avx2_padded::<LANE_SIZE>(&chunk[unpadded..], mask).map(|idx| unpadded + idx)
}

#[cfg(feature = "simd")]
#[inline(always)]
pub fn cpu_find(window: &[u8], byte: u8) -> Option<usize> {
    cpu_find_simd_n::<CPU_SIMD_64_SIZE>(
        window,
        byte,
    )
}

#[cfg(not(feature = "simd"))]
#[inline]
pub fn cpu_find(window: &[u8], byte: u8) -> Option<usize> {
    cpu_find_fallback(
        window,
        byte,
    )
}

////////////////////////////////////////SEARCH FOR CONTINUOUS SLICE OF NEEDLES///////////////////////////////
#[inline(always)]
pub fn cpu_continuous_count_fallback(chunk: &[u8], byte: u8) -> Option<usize> {
    let mut count = 0;
    for i in 0..chunk.len() {
        if cpu_unlikely_branch(chunk[i] != byte) {
            return Some(count);
        }
        count += 1;
    }
    Some(count)
}

#[cfg(feature = "simd")]
#[inline]
fn cpu_continuous_count_simd_avx2_n<const SEARCH_WINDOW_SIZE: usize>(data_vec: u8xN<SEARCH_WINDOW_SIZE>, target: u8xN<SEARCH_WINDOW_SIZE>) -> usize {
    let mask = data_vec.simd_eq(target);

    let bitmask = mask.to_bitmask();
    bitmask.trailing_zeros() as usize
}

#[cfg(feature = "simd")]
#[inline]
fn cpu_continuous_count_simd_avx2_unpadded<const SEARCH_WINDOW_SIZE: usize>(chunk: &[u8], target: u8xN<SEARCH_WINDOW_SIZE>) -> usize {
    let data_vec = cpu_slice_to_simd::<SEARCH_WINDOW_SIZE>(chunk);
    cpu_continuous_count_simd_avx2_n(data_vec, target)
}

#[cfg(feature = "simd")]
#[inline]
fn cpu_continuous_count_simd_avx2_padded<const SEARCH_WINDOW_SIZE: usize>(chunk: &[u8], target: u8xN<SEARCH_WINDOW_SIZE>) -> usize {
    let data_vec = cpu_slice_to_simd_padded::<SEARCH_WINDOW_SIZE, 0>(chunk);
    cpu_continuous_count_simd_avx2_n(data_vec, target)
}

///
/// Use SIMD to find the index of a byte in a byte slice.
///
/// ## Note
///
/// We try saturating the CPU SIMD ports with ops. If we had to do all [CPU_SIMD_AVERAGE_ALU_COUNT]
/// passes to find the needle, we essentially speculatively precomputed the index. Our worst case
/// is if we only had to do one pass to find the needle, but we are relying on out-of-order execution
/// and the cheapness of SIMD to hide the latency. This also serves as a form of prefetching the
/// byte slice into the cache lines, so beware of thrashing it.
///
///
#[cfg(feature = "simd")]
#[inline]
pub fn cpu_continuous_count_simd_n<const LANE_SIZE: usize>
(
    chunk: &[u8],
    byte: u8,
) -> Option<usize>
{
    let mask = u8xN::<LANE_SIZE>::splat(byte);
    let mut iter = chunk.chunks(LANE_SIZE);
    let max_iter = iter.len();
    let large_steps = max_iter / CPU_SIMD_AVERAGE_ALU_COUNT; // div here so consider changing to shifts for extra ns perf
    let mut indx = 0;

    // Try saturating the CPU SIMD ports with ops. If we had to do all 4 passes to find the needle,
    // we essentially speculatively precomputed the index. Our worst case is if we only had to do
    // one pass to find the needle but we are relying on out of order execution and the cheapness of
    // SIMD to hide the latency. This also serves as a form of prefetching byte slice.
    for _ in 0..large_steps {
        let mut sizes = rumtk_mem_quick_array_init!(usize, CPU_SIMD_AVERAGE_ALU_COUNT);
        sizes[0] = cpu_continuous_count_simd_avx2_unpadded::<LANE_SIZE>(iter.next().unwrap_or_default(), mask);
        sizes[1] = cpu_continuous_count_simd_avx2_unpadded::<LANE_SIZE>(iter.next().unwrap_or_default(), mask);
        sizes[2] = cpu_continuous_count_simd_avx2_unpadded::<LANE_SIZE>(iter.next().unwrap_or_default(), mask);
        sizes[3] = cpu_continuous_count_simd_avx2_unpadded::<LANE_SIZE>(iter.next().unwrap_or_default(), mask);

        for s in sizes {
            if s < LANE_SIZE {
                return Some(indx + s);
            }
            indx += LANE_SIZE;
        }
    }

    match iter.next() {
        Some(window) => {
            Some(indx + cpu_continuous_count_simd_avx2_padded::<LANE_SIZE>(window, mask))
        },
        None => Some(indx),
    }
}

#[cfg(feature = "simd")]
#[inline]
pub fn cpu_continuous_count(window: &[u8], byte: u8) -> Option<usize> {
    let start = cpu_find(window, byte)?;
    cpu_continuous_count_simd_n::<CPU_SIMD_64_SIZE>(
        &window[start..],
        byte,
    )
}

#[cfg(not(feature = "simd"))]
#[inline]
pub fn cpu_continuous_count(window: &[u8], byte: u8) -> Option<usize> {
    let start = cpu_find(window, byte)?;
    cpu_continuous_count_fallback(
        &window[start..],
        byte,
    )
}

/////////////////////////////Replacement Helpers///////////////////////////////
#[inline(always)]
pub fn cpu_replace_fallback(data: &mut [u8], pattern: u8, replacement: u8) {
    for i in 0..data.len() {
        if data[i] == pattern {
            data[i] = replacement;
        }
    }
}

#[cfg(feature = "simd")]
#[inline(always)]
pub fn cpu_find_replace_simd_n<const LANE_SIZE: usize>(chunk: &mut [u8], pattern: u8xN<LANE_SIZE>, replacement: u8xN<LANE_SIZE>) {
    let simd_chunk = u8xN::<LANE_SIZE>::from_array(cpu_slice_to_array_padded::<LANE_SIZE, 0>(chunk));
    let bitmask = simd_chunk.simd_eq(pattern);

    if bitmask.any() {
        replacement.store_select(chunk, bitmask);
    }
}

#[cfg(feature = "simd")]
#[inline(always)]
pub fn cpu_replace_simd_n<const LANE_SIZE: usize>(data: &mut [u8], pattern: u8, replacement: u8) {
    let mask = u8xN::<LANE_SIZE>::splat(pattern);
    let simd_replacement = u8xN::<LANE_SIZE>::splat(replacement);

    for chunk in data.chunks_mut(LANE_SIZE) {
        cpu_find_replace_simd_n::<LANE_SIZE>(chunk, mask, simd_replacement);
    }
}

#[cfg(feature = "simd")]
#[inline(always)]
pub fn cpu_replace_byte(data: &mut [u8], pattern: u8, replacement: u8) {
    cpu_replace_simd_n::<CPU_SIMD_64_SIZE>(data, pattern, replacement)
}

#[cfg(not(feature = "simd"))]
#[inline(always)]
pub fn cpu_replace_byte(data: &mut [u8], pattern: u8, replacement: u8) {
    cpu_replace_fallback(data, pattern, replacement)
}

/////////////////////////////GATHER ALL INDICES OF NEEDLE IN HAYSTACK///////////////////////////////
pub type CPUTokenStackIndex<const LANE_SIZE: usize> = [u32; LANE_SIZE];
pub type CPUTokenRelativeStackInfo<const LANE_SIZE: usize> = (usize, CPUTokenStackIndex<LANE_SIZE>);
pub type CPUTokenIndexCollection = RUMVec<u32>;
pub type CPUTokenIndexSet = (u8, RUMVec<u32>);
pub type CPUTokenSet = (u8, u32);
pub type CPUTokenSetCollection = RUMVec<CPUTokenSet>;

#[inline(always)]
pub fn cpu_collect_fallback(chunk: &[u8], byte: u8, offset: usize) -> CPUTokenIndexCollection {
    let mut results: CPUTokenStackIndex<CPU_SIMD_64_SIZE> = [0; CPU_SIMD_64_SIZE];
    let mut length = 0;

    for i in 0..chunk.len() {
        if chunk[i]==byte {
            let pos = offset + i;
            results[length] = pos as u32;
            length += 1;
        }
    }

    CPUTokenIndexCollection::from(&results[..length])
}

#[cfg(feature = "simd")]
#[inline]
fn cpu_collect_simd_avx2_n<const LANE_SIZE: usize>(data_vec: &u8xN<LANE_SIZE>, target: u8xN<LANE_SIZE>, offset: usize) -> Option<CPUTokenRelativeStackInfo<LANE_SIZE>> {
    let mut results: CPUTokenStackIndex<LANE_SIZE> = [0; LANE_SIZE];
    let mut length = 0;

    let mask = data_vec.simd_eq(target);

    if mask.any() {
        let items = mask.to_array();

        for i in 0..items.len() {
            if items[i] {
                let pos = offset + i;
                results[length] = pos as u32;
                length += 1;
            }
        }

        return Some((length, results));
    }

    None
}

#[cfg(feature = "simd")]
#[inline]
pub fn cpu_collect_simd_n<const LANE_SIZE: usize>
(
    chunk: &[u8],
    byte: u8,
    offset: usize
) -> CPUTokenIndexCollection
{
    let mask = u8xN::<LANE_SIZE>::splat(byte);
    let (prefix, middle, postfix) = chunk.as_simd::<LANE_SIZE>();

    let mut local_offset: usize = offset;
    let data: CPUTokenIndexCollection = cpu_collect_fallback(prefix, byte, local_offset);
    let mut positions: CPUTokenIndexCollection = CPUTokenIndexCollection::from(data);
    local_offset += prefix.len();

    for window in middle.into_iter() {
        match cpu_collect_simd_avx2_n::<LANE_SIZE>(window, mask, local_offset) {
            Some((len, data)) => {
                positions.extend_from_slice(&data[..len]);
            },
            None => {},
        };
        local_offset += LANE_SIZE;
    }

    let data: CPUTokenIndexCollection = cpu_collect_fallback(postfix, byte, local_offset);
    positions.extend_from_slice(&data);

    positions
}

#[cfg(feature = "simd")]
#[inline]
pub fn cpu_collect(window: &[u8], byte: u8, offset: usize) -> CPUTokenIndexSet {
    let indx = cpu_collect_simd_n::<CPU_SIMD_64_SIZE>(
        window,
        byte,
        offset
    );
    (byte, indx)
}

#[cfg(not(feature = "simd"))]
#[inline]
pub fn cpu_collect(window: &[u8], byte: u8, offset: usize) -> CPUTokenIndexSet {
    let indx = cpu_collect_fallback(
        window,
        byte,
        offset
    );
    (byte, indx)
}

#[inline]
pub fn cpu_tokenize<const WINDOW_SIZE: usize>(haystack: &[u8], bytes: &[u8]) -> CPUTokenSetCollection
{
    let mut results = CPUTokenSetCollection::with_capacity(haystack.len() * size_of::<CPUTokenSet>());
    let mut offset = 0;

    for window in haystack.chunks(WINDOW_SIZE) {
        for byte in bytes {
            let (b, indx) = cpu_collect(window, *byte, offset);

            if !indx.is_empty() {
                for tok_indx in indx {
                    results.push((b, tok_indx));
                }
            }
        }
        offset += window.len();
    }

    results.sort_unstable_by(|a,b| a.1.cmp(&b.1));

    results
}

#[inline]
pub fn cpu_tokenize_rev<const WINDOW_SIZE: usize>(haystack: &[u8], bytes: &[u8]) -> CPUTokenSetCollection
{
    let reversed: Vec<u8> = bytes.iter().rev().cloned().collect();
    cpu_tokenize::<WINDOW_SIZE>(haystack, &reversed)
}