ordofp_core 0.1.0

OrdoFP core provides developers with HList, Disiunctio, NominataUniversalis, Universalis, and functional type classes
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
//! Monomorphic fast path for `ParFlumen` pipelines.
//!
//! `FlumenParallelumFast<P>` is an additive, opt-in alternative to the
//! default `FlumenParallelum<T>` pipeline. It keeps the pipeline node as a
//! **concrete generic type** instead of `Arc<dyn Nodus<Item = T>>`, so every
//! internal `visit_scalar` call is a direct (inlineable) call rather than a
//! vtable indirect call.
//!
//! # When to use the fast path
//!
//! - Hot pipelines that are built once and executed per element over large inputs.
//! - Pipelines that don't need to be stored behind a uniform `Arc<dyn>` type
//!   (e.g. in a heterogeneous collection).
//!
//! # Trade-offs
//!
//! - **Pros:** zero-vtable per-element cost; LLVM can inline through the whole
//!   chain and vectorize/unroll.
//! - **Cons:** each `.map` / `.filter` / `.scan` changes the return type, so
//!   the fast pipeline is harder to store in a field. Monomorphization
//!   multiplies concrete types across call sites, so deep pipelines cost
//!   compile time / code size.
//!
//! # Example
//!
//! ```rust
//! use ordofp_core::par::{FlumenParallelumFast, backend::CpuScalar};
//!
//! let data = vec![1, 2, 3, 4, 5];
//! let result = FlumenParallelumFast::from_vec(data)
//!     .map(|x| x * 2)
//!     .filter(|x| *x > 4)
//!     .collect_vec(&CpuScalar);
//! assert_eq!(result, vec![6, 8, 10]);
//! ```
//!
//! # Bridge to the dynamic path
//!
//! Call [`FlumenParallelumFast::into_dyn`] to convert a fast pipeline into a
//! regular [`super::FlumenParallelum`] (shareable, uniform type) once the
//! shape is fixed.

#![cfg(feature = "par")]

use alloc::vec;
use alloc::vec::Vec;
use core::marker::PhantomData;
use core::ops::ControlFlow;

use super::Nodus;
use super::backend::Backend;

// =============================================================================
// Fast-path node types (no Arc<dyn>, no Arc<dyn Fn>)
// =============================================================================

/// Source node holding owned data (fast path).
pub struct FastInit<T> {
    data: Vec<T>,
}

impl<T> Nodus for FastInit<T>
where
    T: Clone + Send + Sync + 'static,
{
    type Item = T;

    #[inline(always)]
    fn len(&self) -> usize {
        self.data.len()
    }

    #[inline(always)]
    fn visit_scalar(&self, sink: &mut dyn FnMut(Self::Item)) {
        for item in &self.data {
            sink(item.clone());
        }
    }

    #[inline(always)]
    fn visit_scalar_ref(&self, sink: &mut dyn FnMut(&Self::Item)) {
        // Own the storage — iterate by reference, zero clones. Without this
        // override FastInit falls back to the default (visit_scalar + clone),
        // cloning every source element and making the fast path SLOWER than
        // the dyn path (which has this override on NodusInit).
        for item in &self.data {
            sink(item);
        }
    }

    #[inline(always)]
    fn is_indexed(&self) -> bool {
        true
    }

    #[inline(always)]
    fn get(&self, index: usize) -> Self::Item {
        self.data[index].clone()
    }

    #[inline(always)]
    fn collect_scalar(&self) -> Vec<Self::Item> {
        self.data.clone()
    }

    #[inline(always)]
    fn try_visit_scalar_ref(&self, f: &mut dyn FnMut(&Self::Item) -> ControlFlow<()>) {
        for item in &self.data {
            if f(item).is_break() {
                return;
            }
        }
    }

    #[cfg(feature = "rayon")]
    #[inline]
    fn collect_rayon(&self) -> Vec<Self::Item> {
        use rayon::prelude::*;
        self.data.par_iter().cloned().collect()
    }

    #[inline]
    fn try_drain_vec(&mut self) -> Option<Vec<Self::Item>> {
        Some(core::mem::take(&mut self.data))
    }
}

/// Map node storing the previous node and closure by concrete type.
pub struct FastMap<P, F, A, B> {
    prev: P,
    f: F,
    _phantom: PhantomData<fn(A) -> B>,
}

impl<P, F, A, B> Nodus for FastMap<P, F, A, B>
where
    P: Nodus<Item = A> + Send + Sync,
    F: Fn(A) -> B + Send + Sync,
    A: Clone + Send + Sync + 'static,
    B: Clone + Send + Sync + 'static,
{
    type Item = B;

    #[inline(always)]
    fn len(&self) -> usize {
        self.prev.len()
    }

    #[inline(always)]
    fn visit_scalar(&self, sink: &mut dyn FnMut(Self::Item)) {
        let f = &self.f;
        self.prev.visit_scalar(&mut |a| sink(f(a)));
    }

    #[inline(always)]
    fn is_indexed(&self) -> bool {
        self.prev.is_indexed()
    }

    #[inline(always)]
    fn get(&self, index: usize) -> Self::Item {
        (self.f)(self.prev.get(index))
    }

    #[inline(always)]
    fn try_visit_scalar_ref(&self, f: &mut dyn FnMut(&Self::Item) -> ControlFlow<()>) {
        let func = &self.f;
        self.prev.try_visit_scalar_ref(&mut |a| {
            let b = func(a.clone());
            f(&b)
        });
    }
}

/// Filter node (fast path).
pub struct FastFilter<P, F, T> {
    prev: P,
    predicate: F,
    _phantom: PhantomData<fn(&T) -> bool>,
}

impl<P, F, T> Nodus for FastFilter<P, F, T>
where
    P: Nodus<Item = T> + Send + Sync,
    F: Fn(&T) -> bool + Send + Sync,
    T: Clone + Send + Sync + 'static,
{
    type Item = T;

    #[inline(always)]
    fn len(&self) -> usize {
        self.prev.len()
    }

    #[inline(always)]
    fn visit_scalar(&self, sink: &mut dyn FnMut(Self::Item)) {
        let predicate = &self.predicate;
        self.prev.visit_scalar(&mut |a| {
            if predicate(&a) {
                sink(a);
            }
        });
    }

    #[inline(always)]
    fn visit_scalar_ref(&self, sink: &mut dyn FnMut(&Self::Item)) {
        let predicate = &self.predicate;
        self.prev.visit_scalar_ref(&mut |a| {
            if predicate(a) {
                sink(a);
            }
        });
    }

    #[inline(always)]
    fn try_visit_scalar_ref(&self, f: &mut dyn FnMut(&Self::Item) -> ControlFlow<()>) {
        let predicate = &self.predicate;
        self.prev.try_visit_scalar_ref(&mut |a| {
            if predicate(a) {
                f(a)
            } else {
                ControlFlow::Continue(())
            }
        });
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, Some(self.prev.len()))
    }

    #[inline]
    fn collect_scalar(&self) -> Vec<Self::Item> {
        // Start with at most 1/4 of upstream length, capped at 1024, with a
        // floor of 16. This avoids worst-case over-allocation while keeping
        // realloc count low for typical filter rates.
        let cap = (self.prev.len() / 4).clamp(16, 1024);
        let mut out = Vec::with_capacity(cap);
        self.visit_scalar_ref(&mut |x| out.push(x.clone()));
        out
    }
}

/// `FilterMap` node (fast path).
pub struct FastFilterMap<P, F, A, B> {
    prev: P,
    f: F,
    _phantom: PhantomData<fn(A) -> Option<B>>,
}

impl<P, F, A, B> Nodus for FastFilterMap<P, F, A, B>
where
    P: Nodus<Item = A> + Send + Sync,
    F: Fn(A) -> Option<B> + Send + Sync,
    A: Clone + Send + Sync + 'static,
    B: Clone + Send + Sync + 'static,
{
    type Item = B;

    #[inline(always)]
    fn len(&self) -> usize {
        self.prev.len()
    }

    #[inline(always)]
    fn visit_scalar(&self, sink: &mut dyn FnMut(Self::Item)) {
        let f = &self.f;
        self.prev.visit_scalar(&mut |a| {
            if let Some(b) = f(a) {
                sink(b);
            }
        });
    }

    #[inline(always)]
    fn try_visit_scalar_ref(&self, f: &mut dyn FnMut(&Self::Item) -> ControlFlow<()>) {
        let func = &self.f;
        self.prev.try_visit_scalar_ref(&mut |a| {
            if let Some(b) = func(a.clone()) {
                f(&b)
            } else {
                ControlFlow::Continue(())
            }
        });
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, Some(self.prev.len()))
    }

    #[inline]
    fn collect_scalar(&self) -> Vec<Self::Item> {
        // Start with at most 1/4 of upstream length, capped at 1024, with a
        // floor of 16. This avoids worst-case over-allocation while keeping
        // realloc count low for typical filter rates.
        let cap = (self.prev.len() / 4).clamp(16, 1024);
        let mut out = Vec::with_capacity(cap);
        // Use visit_scalar (owned) rather than visit_scalar_ref + clone: each
        // matching element is produced once as an owned B and pushed directly,
        // saving one clone per surviving element vs. the ref path.
        self.visit_scalar(&mut |x| out.push(x));
        out
    }
}

/// Scan node (fast path).
pub struct FastScan<P, F, A, B> {
    prev: P,
    init: B,
    f: F,
    _phantom: PhantomData<fn(B, A) -> B>,
}

impl<P, F, A, B> Nodus for FastScan<P, F, A, B>
where
    P: Nodus<Item = A> + Send + Sync,
    F: Fn(B, A) -> B + Send + Sync,
    A: Clone + Send + Sync + 'static,
    B: Clone + Send + Sync + 'static,
{
    type Item = B;

    #[inline(always)]
    fn len(&self) -> usize {
        self.prev.len()
    }

    #[inline(always)]
    fn visit_scalar(&self, sink: &mut dyn FnMut(Self::Item)) {
        let f = &self.f;
        // Use Option<B> to move acc out to feed f, eliminating the extra
        // clone the naive `f(acc.clone(), a); sink(acc.clone())` approach
        // required. Cost: 1 clone per element (for sink) instead of 2.
        let mut acc: Option<B> = Some(self.init.clone());
        self.prev.visit_scalar(&mut |a| {
            let old = acc.take().unwrap();
            let next = f(old, a);
            sink(next.clone());
            acc = Some(next);
        });
    }

    #[inline(always)]
    fn visit_scalar_ref(&self, sink: &mut dyn FnMut(&Self::Item)) {
        let f = &self.f;
        // Same Option<B> pattern; sink receives &B (no extra clone of B).
        let mut acc: Option<B> = Some(self.init.clone());
        self.prev.visit_scalar(&mut |a| {
            let old = acc.take().unwrap();
            let next = f(old, a);
            acc = Some(next);
            sink(acc.as_ref().unwrap());
        });
    }
}

/// Take first N elements (fast path).
pub struct FastTake<P> {
    prev: P,
    count: usize,
}

impl<P, T> Nodus for FastTake<P>
where
    P: Nodus<Item = T> + Send + Sync,
    T: Clone + Send + Sync + 'static,
{
    type Item = T;

    #[inline(always)]
    fn len(&self) -> usize {
        self.prev.len().min(self.count)
    }

    #[inline(always)]
    fn visit_scalar(&self, sink: &mut dyn FnMut(Self::Item)) {
        if self.prev.is_indexed() {
            for i in 0..self.len() {
                sink(self.prev.get(i));
            }
            return;
        }
        let mut remaining = self.count;
        self.prev.visit_scalar(&mut |a| {
            if remaining > 0 {
                remaining -= 1;
                sink(a);
            }
        });
    }

    #[inline(always)]
    fn visit_scalar_ref(&self, sink: &mut dyn FnMut(&Self::Item)) {
        if self.prev.is_indexed() {
            for i in 0..self.len() {
                let item = self.prev.get(i);
                sink(&item);
            }
            return;
        }
        let mut remaining = self.count;
        self.prev.visit_scalar_ref(&mut |a| {
            if remaining > 0 {
                remaining -= 1;
                sink(a);
            }
        });
    }

    #[inline(always)]
    fn is_indexed(&self) -> bool {
        self.prev.is_indexed()
    }

    #[inline(always)]
    fn get(&self, index: usize) -> Self::Item {
        crate::unlikely_panic!(index >= self.count, "FastTake: index out of bounds");
        self.prev.get(index)
    }

    #[inline(always)]
    fn try_visit_scalar_ref(&self, f: &mut dyn FnMut(&Self::Item) -> ControlFlow<()>) {
        let mut remaining = self.count;
        self.prev.try_visit_scalar_ref(&mut |a| {
            if remaining == 0 {
                return ControlFlow::Break(());
            }
            remaining -= 1;
            f(a)
        });
    }
}

/// Skip first N elements (fast path).
pub struct FastSkip<P> {
    prev: P,
    count: usize,
}

impl<P, T> Nodus for FastSkip<P>
where
    P: Nodus<Item = T> + Send + Sync,
    T: Clone + Send + Sync + 'static,
{
    type Item = T;

    #[inline(always)]
    fn len(&self) -> usize {
        self.prev.len().saturating_sub(self.count)
    }

    #[inline(always)]
    fn visit_scalar(&self, sink: &mut dyn FnMut(Self::Item)) {
        if self.prev.is_indexed() {
            let len = self.len();
            for i in 0..len {
                sink(self.prev.get(i + self.count));
            }
            return;
        }
        let mut skipped = 0;
        let count = self.count;
        self.prev.visit_scalar(&mut |a| {
            if skipped >= count {
                sink(a);
            } else {
                skipped += 1;
            }
        });
    }

    #[inline(always)]
    fn visit_scalar_ref(&self, sink: &mut dyn FnMut(&Self::Item)) {
        if self.prev.is_indexed() {
            let len = self.len();
            for i in 0..len {
                let item = self.prev.get(i + self.count);
                sink(&item);
            }
            return;
        }
        let mut skipped = 0;
        let count = self.count;
        self.prev.visit_scalar_ref(&mut |a| {
            if skipped >= count {
                sink(a);
            } else {
                skipped += 1;
            }
        });
    }

    #[inline(always)]
    fn is_indexed(&self) -> bool {
        self.prev.is_indexed()
    }

    #[inline(always)]
    fn get(&self, index: usize) -> Self::Item {
        self.prev.get(index + self.count)
    }

    #[inline(always)]
    fn try_visit_scalar_ref(&self, f: &mut dyn FnMut(&Self::Item) -> ControlFlow<()>) {
        let mut skipped = 0;
        let count = self.count;
        self.prev.try_visit_scalar_ref(&mut |a| {
            if skipped >= count {
                f(a)
            } else {
                skipped += 1;
                ControlFlow::Continue(())
            }
        });
    }
}

/// Enumerate each element with its index (fast path).
pub struct FastEnumerate<P> {
    prev: P,
}

impl<P, T> Nodus for FastEnumerate<P>
where
    P: Nodus<Item = T> + Send + Sync,
    T: Clone + Send + Sync + 'static,
{
    type Item = (usize, T);

    #[inline(always)]
    fn len(&self) -> usize {
        self.prev.len()
    }

    #[inline(always)]
    fn visit_scalar(&self, sink: &mut dyn FnMut(Self::Item)) {
        let mut index = 0usize;
        self.prev.visit_scalar(&mut |a| {
            sink((index, a));
            index += 1;
        });
    }

    #[inline(always)]
    fn is_indexed(&self) -> bool {
        self.prev.is_indexed()
    }

    #[inline(always)]
    fn get(&self, index: usize) -> Self::Item {
        (index, self.prev.get(index))
    }

    #[inline(always)]
    fn try_visit_scalar_ref(&self, f: &mut dyn FnMut(&Self::Item) -> ControlFlow<()>) {
        let mut index = 0usize;
        self.prev.try_visit_scalar_ref(&mut |a| {
            let pair = (index, a.clone());
            index += 1;
            f(&pair)
        });
    }
}

/// Inspect each element (fast path).
pub struct FastInspect<P, F, T> {
    prev: P,
    f: F,
    _phantom: PhantomData<fn(&T)>,
}

impl<P, F, T> Nodus for FastInspect<P, F, T>
where
    P: Nodus<Item = T> + Send + Sync,
    F: Fn(&T) + Send + Sync,
    T: Clone + Send + Sync + 'static,
{
    type Item = T;

    #[inline(always)]
    fn len(&self) -> usize {
        self.prev.len()
    }

    #[inline(always)]
    fn visit_scalar(&self, sink: &mut dyn FnMut(Self::Item)) {
        let f = &self.f;
        self.prev.visit_scalar(&mut |a| {
            f(&a);
            sink(a);
        });
    }

    #[inline(always)]
    fn visit_scalar_ref(&self, sink: &mut dyn FnMut(&Self::Item)) {
        let f = &self.f;
        self.prev.visit_scalar_ref(&mut |a| {
            f(a);
            sink(a);
        });
    }

    #[inline(always)]
    fn is_indexed(&self) -> bool {
        self.prev.is_indexed()
    }

    #[inline(always)]
    fn get(&self, index: usize) -> Self::Item {
        let item = self.prev.get(index);
        (self.f)(&item);
        item
    }

    #[inline(always)]
    fn try_visit_scalar_ref(&self, f: &mut dyn FnMut(&Self::Item) -> ControlFlow<()>) {
        let inspect_f = &self.f;
        self.prev.try_visit_scalar_ref(&mut |a| {
            inspect_f(a);
            f(a)
        });
    }
}

// =============================================================================
// FlumenParallelumFast - monomorphic wrapper
// =============================================================================

/// Monomorphic, zero-vtable parallel pipeline.
///
/// Generic over the concrete node type `P` so every chained operation builds
/// up a larger concrete type and `visit_scalar` calls stay as static dispatch.
///
/// See module-level docs for guidance on when to prefer this over
/// [`super::FlumenParallelum`].
pub struct FlumenParallelumFast<P> {
    node: P,
}

impl<T> FlumenParallelumFast<FastInit<T>>
where
    T: Clone + Send + Sync + 'static,
{
    /// Build a fast-path pipeline from an owned vector.
    #[inline(always)]
    pub fn from_vec(vec: Vec<T>) -> Self {
        Self {
            node: FastInit { data: vec },
        }
    }

    /// Build a fast-path pipeline from a slice (clones the data).
    #[inline(always)]
    pub fn from_slice(slice: &[T]) -> Self {
        Self::from_vec(slice.to_vec())
    }

    /// Empty fast-path pipeline.
    #[inline(always)]
    pub fn empty() -> Self {
        Self::from_vec(Vec::new())
    }

    /// Singleton fast-path pipeline.
    #[inline(always)]
    pub fn singleton(value: T) -> Self {
        Self::from_vec(vec![value])
    }
}

impl<P> FlumenParallelumFast<P>
where
    P: Nodus + Send + Sync,
    P::Item: Clone + Send + Sync + 'static,
{
    /// Stream length.
    #[inline(always)]
    pub fn len(&self) -> usize {
        self.node.len()
    }

    /// Whether the stream is empty.
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Borrow the underlying node.
    #[inline(always)]
    pub fn as_node(&self) -> &P {
        &self.node
    }

    /// Consume and return the underlying node.
    #[inline(always)]
    pub fn into_node(self) -> P {
        self.node
    }

    /// Map each element with `f`.
    #[inline(always)]
    pub fn map<B, F>(self, f: F) -> FlumenParallelumFast<FastMap<P, F, P::Item, B>>
    where
        F: Fn(P::Item) -> B + Send + Sync,
        B: Clone + Send + Sync + 'static,
    {
        FlumenParallelumFast {
            node: FastMap {
                prev: self.node,
                f,
                _phantom: PhantomData,
            },
        }
    }

    /// Filter elements matching `predicate`.
    #[inline(always)]
    pub fn filter<F>(self, predicate: F) -> FlumenParallelumFast<FastFilter<P, F, P::Item>>
    where
        F: Fn(&P::Item) -> bool + Send + Sync,
    {
        FlumenParallelumFast {
            node: FastFilter {
                prev: self.node,
                predicate,
                _phantom: PhantomData,
            },
        }
    }

    /// Map + filter in one pass.
    #[inline(always)]
    pub fn filter_map<B, F>(self, f: F) -> FlumenParallelumFast<FastFilterMap<P, F, P::Item, B>>
    where
        F: Fn(P::Item) -> Option<B> + Send + Sync,
        B: Clone + Send + Sync + 'static,
    {
        FlumenParallelumFast {
            node: FastFilterMap {
                prev: self.node,
                f,
                _phantom: PhantomData,
            },
        }
    }

    /// Prefix scan (cumulative fold).
    #[inline(always)]
    pub fn scan<B, F>(self, init: B, f: F) -> FlumenParallelumFast<FastScan<P, F, P::Item, B>>
    where
        F: Fn(B, P::Item) -> B + Send + Sync,
        B: Clone + Send + Sync + 'static,
    {
        FlumenParallelumFast {
            node: FastScan {
                prev: self.node,
                init,
                f,
                _phantom: PhantomData,
            },
        }
    }

    /// Take the first `n` elements.
    #[inline(always)]
    pub fn take(self, n: usize) -> FlumenParallelumFast<FastTake<P>> {
        FlumenParallelumFast {
            node: FastTake {
                prev: self.node,
                count: n,
            },
        }
    }

    /// Skip the first `n` elements.
    #[inline(always)]
    pub fn skip(self, n: usize) -> FlumenParallelumFast<FastSkip<P>> {
        FlumenParallelumFast {
            node: FastSkip {
                prev: self.node,
                count: n,
            },
        }
    }

    /// Pair each element with its index.
    #[inline(always)]
    pub fn enumerate(self) -> FlumenParallelumFast<FastEnumerate<P>> {
        FlumenParallelumFast {
            node: FastEnumerate { prev: self.node },
        }
    }

    /// Inspect each element without modifying it.
    #[inline(always)]
    pub fn inspect<F>(self, f: F) -> FlumenParallelumFast<FastInspect<P, F, P::Item>>
    where
        F: Fn(&P::Item) + Send + Sync,
    {
        FlumenParallelumFast {
            node: FastInspect {
                prev: self.node,
                f,
                _phantom: PhantomData,
            },
        }
    }

    /// Collect elements into a `Vec` using the given backend.
    #[inline(always)]
    pub fn collect_vec<Bk>(&self, backend: &Bk) -> Vec<P::Item>
    where
        Bk: Backend,
    {
        backend.collect(&self.node)
    }

    /// Reduce using an associative operation.
    #[inline(always)]
    pub fn reduce<Bk, F>(&self, backend: &Bk, f: F) -> Option<P::Item>
    where
        Bk: Backend,
        F: Fn(P::Item, P::Item) -> P::Item + Send + Sync,
    {
        backend.reduce(&self.node, f)
    }

    /// Fold with an initial value.
    #[inline(always)]
    pub fn fold<Bk, B, F>(&self, backend: &Bk, init: B, f: F) -> B
    where
        Bk: Backend,
        B: Clone + Send + Sync + 'static,
        F: Fn(B, P::Item) -> B + Send + Sync,
    {
        backend.fold(&self.node, init, f)
    }

    /// Side-effect for each element.
    #[inline(always)]
    pub fn for_each<Bk, F>(&self, backend: &Bk, f: F)
    where
        Bk: Backend,
        F: Fn(P::Item) + Send + Sync,
    {
        backend.for_each(&self.node, f);
    }

    /// Any element matching predicate?
    #[inline(always)]
    pub fn any<Bk, F>(&self, backend: &Bk, predicate: F) -> bool
    where
        Bk: Backend,
        F: Fn(&P::Item) -> bool + Send + Sync,
    {
        backend.any(&self.node, predicate)
    }

    /// All elements matching predicate?
    #[inline(always)]
    pub fn all<Bk, F>(&self, backend: &Bk, predicate: F) -> bool
    where
        Bk: Backend,
        F: Fn(&P::Item) -> bool + Send + Sync,
    {
        backend.all(&self.node, predicate)
    }

    /// First element matching predicate.
    #[inline(always)]
    pub fn find<Bk, F>(&self, backend: &Bk, predicate: F) -> Option<P::Item>
    where
        Bk: Backend,
        F: Fn(&P::Item) -> bool + Send + Sync,
    {
        backend.find(&self.node, predicate)
    }

    /// Count elements.
    #[inline(always)]
    pub fn count<Bk>(&self, backend: &Bk) -> usize
    where
        Bk: Backend,
    {
        backend.count(&self.node)
    }
}

// Bridge: fast -> dyn
impl<P> FlumenParallelumFast<P>
where
    P: Nodus + Send + Sync + 'static,
    P::Item: Clone + Send + Sync + 'static,
{
    /// Erase the concrete node type and return a shareable
    /// [`super::FlumenParallelum`] pipeline.
    ///
    /// Use this when you need uniform storage (e.g. a `Vec<FlumenParallelum<T>>`)
    /// or `Clone` semantics — the fast pipeline is not `Clone`.
    #[inline]
    pub fn into_dyn(self) -> super::FlumenParallelum<P::Item> {
        super::FlumenParallelum::__from_node(alloc::sync::Arc::new(self.node))
    }
}

// =============================================================================
// Monomorphization evidence hook
//
// This function is a public, non-inlined entry point that forces LLVM to emit
// a concrete specialisation of a known fast-path pipeline. Inspecting the
// generated code for this symbol shows that no vtable dispatch appears along
// the per-element visit path — the `visit_scalar` calls on `FastInit`,
// `FastMap`, `FastFilter` are statically resolved.
// =============================================================================

/// Evidence-hook: sum of `(x * 2)` for even `x` across a slice using the fast path.
///
/// Not part of the normal public API surface — exposed to allow inspection of
/// the monomorphised pipeline without a dev-dep on `criterion`. The
/// `#[inline(never)]` ensures the symbol survives codegen so tools (cargo-asm,
/// llvm-objdump, etc.) can find it.
#[doc(hidden)]
#[inline(never)]
pub fn __fastpath_evidence_sum_even_doubled_i64(xs: &[i64]) -> i64 {
    use super::backend::CpuScalar;
    FlumenParallelumFast::from_slice(xs)
        .filter(|x| x % 2 == 0)
        .map(|x| x * 2)
        .fold(&CpuScalar, 0i64, |acc, x| acc + x)
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::super::backend::CpuScalar;
    use super::*;

    #[test]
    fn fast_from_vec_collect() {
        let v = vec![1i32, 2, 3, 4, 5];
        let got = FlumenParallelumFast::from_vec(v).collect_vec(&CpuScalar);
        assert_eq!(got, vec![1, 2, 3, 4, 5]);
    }

    #[test]
    fn fast_from_slice_collect() {
        let v = [1i32, 2, 3];
        let got = FlumenParallelumFast::from_slice(&v).collect_vec(&CpuScalar);
        assert_eq!(got, vec![1, 2, 3]);
    }

    #[test]
    fn fast_map() {
        let got = FlumenParallelumFast::from_vec(vec![1, 2, 3])
            .map(|x| x * 2)
            .collect_vec(&CpuScalar);
        assert_eq!(got, vec![2, 4, 6]);
    }

    #[test]
    fn fast_filter() {
        let got = FlumenParallelumFast::from_vec(vec![1, 2, 3, 4, 5])
            .filter(|x| x % 2 == 0)
            .collect_vec(&CpuScalar);
        assert_eq!(got, vec![2, 4]);
    }

    #[test]
    fn fast_filter_map() {
        let got = FlumenParallelumFast::from_vec(vec![1, 2, 3, 4])
            .filter_map(|x| if x % 2 == 0 { Some(x * 10) } else { None })
            .collect_vec(&CpuScalar);
        assert_eq!(got, vec![20, 40]);
    }

    #[test]
    fn fast_scan() {
        let got = FlumenParallelumFast::from_vec(vec![1, 2, 3, 4])
            .scan(0, |acc, x| acc + x)
            .collect_vec(&CpuScalar);
        assert_eq!(got, vec![1, 3, 6, 10]);
    }

    #[test]
    fn fast_take_skip() {
        let got = FlumenParallelumFast::from_vec(vec![1, 2, 3, 4, 5])
            .skip(1)
            .take(3)
            .collect_vec(&CpuScalar);
        assert_eq!(got, vec![2, 3, 4]);
    }

    #[test]
    fn fast_enumerate() {
        let got = FlumenParallelumFast::from_vec(vec!["a", "b", "c"])
            .enumerate()
            .collect_vec(&CpuScalar);
        assert_eq!(got, vec![(0, "a"), (1, "b"), (2, "c")]);
    }

    #[test]
    fn fast_pipeline() {
        // map -> filter -> take -> enumerate, mirroring the dyn pipeline test
        let got = FlumenParallelumFast::from_vec(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
            .map(|x| x * 2)
            .filter(|x| *x > 6)
            .take(4)
            .enumerate()
            .collect_vec(&CpuScalar);
        assert_eq!(got, vec![(0, 8), (1, 10), (2, 12), (3, 14)]);
    }

    #[test]
    fn fast_reduce_fold_count() {
        let pipe = FlumenParallelumFast::from_vec(vec![1, 2, 3, 4, 5]);
        assert_eq!(pipe.count(&CpuScalar), 5);
        assert_eq!(pipe.reduce(&CpuScalar, |a, b| a + b), Some(15));
        assert_eq!(pipe.fold(&CpuScalar, 0, |a, x| a + x), 15);
    }

    #[test]
    fn fast_any_all_find() {
        let pipe = FlumenParallelumFast::from_vec(vec![1, 2, 3, 4, 5]);
        assert!(pipe.any(&CpuScalar, |x| *x > 3));
        assert!(!pipe.all(&CpuScalar, |x| *x > 3));
        assert_eq!(pipe.find(&CpuScalar, |x| *x > 3), Some(4));
    }

    #[test]
    fn fast_into_dyn_bridge() {
        let fast = FlumenParallelumFast::from_vec(vec![1, 2, 3])
            .map(|x| x + 10)
            .filter(|x| *x > 10);
        let dynamic = fast.into_dyn();
        assert_eq!(dynamic.collect_vec(&CpuScalar), vec![11, 12, 13]);
    }

    #[test]
    fn fast_empty_singleton() {
        let e: FlumenParallelumFast<FastInit<i32>> = FlumenParallelumFast::empty();
        assert!(e.is_empty());
        let s = FlumenParallelumFast::singleton(42);
        assert_eq!(s.len(), 1);
        assert_eq!(s.collect_vec(&CpuScalar), vec![42]);
    }
}