difference_performant

Function difference_performant 

Source
pub fn difference_performant<T: Eq + Hash + Clone>(
    to_compare: &Vec<T>,
    others: &Vec<&Vec<T>>,
) -> Vec<T>
Expand description

Computes the difference between a primary list and multiple exclusion lists using AHashSet for maximum performance.

§Type Parameters

  • T: The element type. Must implement Clone, Eq, and Hash.

§Arguments

  • to_compare: A vector of values to retain if not found in others.
  • others: A reference to a list of reference vectors containing values to exclude.

§Returns

A new Vec<T> containing only the values from to_compare that are not found in any of the others.

§Behavior

  • Identical in output to difference, but optimized using ahash::AHashSet for faster performance.
  • Equality comparison based on == (requires Eq + Hash).

§Performance

  • ⚡ Uses AHashSet, a fast, non-cryptographic hashing algorithm (Blake3-inspired).
  • 🚀 Significantly faster than HashSet for large data, but not DoS-resistant (not safe for untrusted input).
  • Preallocates exclusion set and result vector for efficiency.
  • Performs at most one clone() per unique value processed.

§Examples

§🚀 Fast difference on large numbers

use pencil_box::array::difference::difference_performant;

let a: Vec<_> = (0..100_000).collect();
let b: Vec<_> = (50_000..150_000).collect();
let result = difference_performant(&a, &vec![&b]);
assert_eq!(result.len(), 50_000);

§⚠️ Not suitable for hostile input

AHashSet is not cryptographically secure. Use `difference` with HashSet if you're handling untrusted or externally-supplied keys.

§✅ Identical logic to difference

let a = vec![1, 2, 3, 4];
let b = vec![2, 4];
let result = difference_performant(&a, &vec![&b]);
assert_eq!(result, vec![1, 3]);