Skip to content

Commit d8987d3

Browse files
committed
refactor(aggregate): flatten nested conditionals in accumulator hot path
Replace nested if-let and if-inside-if-let patterns with let-chain conditions in AggAccum::accumulate. Eliminates unnecessary nesting, makes null-skip and dedup conditions read left-to-right, and avoids a spurious clone of COUNT(*) row in the non-null branch. Also replace manual .max(64).min(MAX_EF_SEARCH) with .clamp() in the ef_search sizing helper for clarity.
1 parent 14fb1d6 commit d8987d3

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

nodedb/src/data/executor/handlers/accum.rs

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ impl AggAccum {
118118
use nodedb_query::msgpack_scan::aggregate_helpers as ah;
119119
match self {
120120
AggAccum::Count { n } => {
121-
if agg.field == "*" && agg.expr.is_none() {
122-
*n += 1;
123-
} else if ah::extract_non_null(doc, &agg.field, agg.expr.as_ref()).is_some() {
121+
if (agg.field == "*" && agg.expr.is_none())
122+
|| ah::extract_non_null(doc, &agg.field, agg.expr.as_ref()).is_some()
123+
{
124124
*n += 1;
125125
}
126126
}
@@ -168,10 +168,10 @@ impl AggAccum {
168168
}
169169
}
170170
AggAccum::CountDistinct { seen } => {
171-
if let Some(bytes) = ah::extract_bytes(doc, &agg.field, agg.expr.as_ref()) {
172-
if bytes != [0xc0u8] {
173-
seen.insert(bytes);
174-
}
171+
if let Some(bytes) = ah::extract_bytes(doc, &agg.field, agg.expr.as_ref())
172+
&& bytes != [0xc0u8]
173+
{
174+
seen.insert(bytes);
175175
}
176176
}
177177
AggAccum::Welford { n, mean, m2 } => {
@@ -184,10 +184,10 @@ impl AggAccum {
184184
}
185185
}
186186
AggAccum::Hll { hll } => {
187-
if let Some(bytes) = ah::extract_bytes(doc, &agg.field, agg.expr.as_ref()) {
188-
if bytes != [0xc0u8] {
189-
hll.add(fnv1a(&bytes));
190-
}
187+
if let Some(bytes) = ah::extract_bytes(doc, &agg.field, agg.expr.as_ref())
188+
&& bytes != [0xc0u8]
189+
{
190+
hll.add(fnv1a(&bytes));
191191
}
192192
}
193193
AggAccum::TDigest { digest } => {
@@ -198,45 +198,43 @@ impl AggAccum {
198198
}
199199
AggAccum::TopK { ss, .. } => {
200200
let actual = field_after_colon(&agg.field);
201-
if let Some(bytes) = ah::extract_bytes(doc, actual, agg.expr.as_ref()) {
202-
if bytes != [0xc0u8] {
203-
ss.add(fnv1a(&bytes));
204-
}
201+
if let Some(bytes) = ah::extract_bytes(doc, actual, agg.expr.as_ref())
202+
&& bytes != [0xc0u8]
203+
{
204+
ss.add(fnv1a(&bytes));
205205
}
206206
}
207207
AggAccum::ArrayAgg { values } => {
208-
if values.len() < ARRAY_AGG_CAP {
209-
if let Some(v) = ah::extract_value(doc, &agg.field, agg.expr.as_ref()) {
210-
if !v.is_null() {
211-
values.push(v);
212-
}
213-
}
208+
if values.len() < ARRAY_AGG_CAP
209+
&& let Some(v) = ah::extract_value(doc, &agg.field, agg.expr.as_ref())
210+
&& !v.is_null()
211+
{
212+
values.push(v);
214213
}
215214
}
216215
AggAccum::ArrayAggDistinct { seen, values } => {
217-
if values.len() < ARRAY_AGG_CAP {
218-
if let Some(bytes) = ah::extract_bytes(doc, &agg.field, agg.expr.as_ref()) {
219-
if bytes != [0xc0u8] && seen.insert(bytes) {
220-
if let Some(v) = ah::extract_value(doc, &agg.field, agg.expr.as_ref()) {
221-
values.push(v);
222-
}
223-
}
224-
}
216+
if values.len() < ARRAY_AGG_CAP
217+
&& let Some(bytes) = ah::extract_bytes(doc, &agg.field, agg.expr.as_ref())
218+
&& bytes != [0xc0u8]
219+
&& seen.insert(bytes)
220+
&& let Some(v) = ah::extract_value(doc, &agg.field, agg.expr.as_ref())
221+
{
222+
values.push(v);
225223
}
226224
}
227225
AggAccum::PercentileCont { values, .. } => {
228-
if values.len() < ARRAY_AGG_CAP {
229-
let actual = field_after_colon(&agg.field);
230-
if let Some(v) = ah::extract_f64(doc, actual, agg.expr.as_ref()) {
231-
values.push(v);
232-
}
226+
let actual = field_after_colon(&agg.field);
227+
if values.len() < ARRAY_AGG_CAP
228+
&& let Some(v) = ah::extract_f64(doc, actual, agg.expr.as_ref())
229+
{
230+
values.push(v);
233231
}
234232
}
235233
AggAccum::StringAgg { parts } => {
236-
if parts.len() < ARRAY_AGG_CAP {
237-
if let Some(s) = ah::extract_str(doc, &agg.field, agg.expr.as_ref()) {
238-
parts.push(s);
239-
}
234+
if parts.len() < ARRAY_AGG_CAP
235+
&& let Some(s) = ah::extract_str(doc, &agg.field, agg.expr.as_ref())
236+
{
237+
parts.push(s);
240238
}
241239
}
242240
}

nodedb/src/data/executor/handlers/vector_search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,6 @@ fn effective_ef(ef_search: usize, top_k: usize) -> usize {
359359
if ef_search > 0 {
360360
ef_search.max(top_k).min(MAX_EF_SEARCH)
361361
} else {
362-
top_k.saturating_mul(4).max(64).min(MAX_EF_SEARCH)
362+
top_k.saturating_mul(4).clamp(64, MAX_EF_SEARCH)
363363
}
364364
}

0 commit comments

Comments
 (0)