From bcb84388563d31088646a64b63c7d46d46d1c55c Mon Sep 17 00:00:00 2001 From: edef Date: Thu, 1 May 2025 14:07:30 +0000 Subject: [PATCH] refactor(eval/builtins/intersectAttrs): use cmp for three-way comparison There is no point in separately checking lt/eq/gt separately when we always need the full ordering anyway. Change-Id: I993108029d205ac17f01acdb6dbf9b2f0cd80f28 Reviewed-on: https://cl.snix.dev/c/snix/+/30372 Reviewed-by: Florian Klink Tested-by: besadii --- snix/eval/src/builtins/mod.rs | 52 ++++++++++++++++------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/snix/eval/src/builtins/mod.rs b/snix/eval/src/builtins/mod.rs index 414f82261..379cac61b 100644 --- a/snix/eval/src/builtins/mod.rs +++ b/snix/eval/src/builtins/mod.rs @@ -811,36 +811,32 @@ mod pure_builtins { // We opted for this implementation over simpler ones because of the // heavy use of this function in nixpkgs. loop { - if left.0 == right.0 { - out.insert(right.0.clone(), right.1.clone()); + match left.0.cmp(right.0) { + Ordering::Equal => { + out.insert(right.0.clone(), right.1.clone()); - left = match left_iter.next() { - Some(x) => x, - None => break, - }; + left = match left_iter.next() { + Some(x) => x, + None => break, + }; - right = match right_iter.next() { - Some(x) => x, - None => break, - }; - - continue; - } - - if left.0 < right.0 { - left = match left_iter.next() { - Some(x) => x, - None => break, - }; - continue; - } - - if right.0 < left.0 { - right = match right_iter.next() { - Some(x) => x, - None => break, - }; - continue; + right = match right_iter.next() { + Some(x) => x, + None => break, + }; + } + Ordering::Less => { + left = match left_iter.next() { + Some(x) => x, + None => break, + }; + } + Ordering::Greater => { + right = match right_iter.next() { + Some(x) => x, + None => break, + }; + } } }