Skip to content

Commit 1127ba8

Browse files
committed
refactor: use a simple loop for percentage allocation selection
1 parent 7c28d1d commit 1127ba8

3 files changed

Lines changed: 8 additions & 9 deletions

File tree

‎.changeset/multivariate-evaluation.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Add protocol-v2 `CompiledFlag` evaluation via `evaluateFlag(flag, context)`:
99
- Preserve the existing hash function and legacy rollout filters; assign the inclusive maximum distribution bucket to the final nonzero allocation.
1010
- Return source version, selected variant, matched rule, allocation, and per-rule diagnostics for exposure events and debugging.
1111

12-
Use `newFlagEvaluator(flag)` for repeated checks: it prepares hash-set membership lookups and validated cumulative percentage thresholds once, including filters nested under groups and negations. Evaluations reuse those structures, binary-search percentage allocations, and reuse per-rule diagnostic scratch storage.
12+
Use `newFlagEvaluator(flag)` for repeated checks: it prepares hash-set membership lookups and validated cumulative percentage thresholds once, including filters nested under groups and negations. Evaluations reuse those structures, scan precomputed percentage thresholds, and reuse per-rule diagnostic scratch storage.
1313

1414
Existing `evaluateFlagRules` and `newEvaluator` exports remain available for legacy consumers. SDK adoption of `/flags` and variant-valued public APIs is a separate change.

‎packages/flag-evaluation/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ returned JSON values as immutable. Each evaluation has independent diagnostics.
2525
array-valued context requires one lookup per examined context element, not a
2626
traversal of the candidate list.
2727
- Percentage validation and cumulative integer thresholds are computed once.
28-
Checks hash the company/context attribute and binary-search the thresholds.
28+
Checks hash the company/context attribute and scan the precomputed thresholds.
2929
The inclusive maximum hash maps to the last nonzero allocation; 0% allocations
3030
never receive traffic.
3131
- Rules stop at the first selected variant. `nextRule` continues evaluation.

‎packages/flag-evaluation/src/index.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -935,14 +935,13 @@ function evaluatePreparedFlag(
935935
99999,
936936
);
937937
const thresholds = rule.thresholds!;
938-
let low = 0;
939-
let high = thresholds.length - 1;
940-
while (low < high) {
941-
const mid = (low + high) >>> 1;
942-
if (bucket < thresholds[mid]) high = mid;
943-
else low = mid + 1;
938+
let allocationIndex = 0;
939+
while (
940+
allocationIndex < thresholds.length - 1 &&
941+
bucket >= thresholds[allocationIndex]
942+
) {
943+
allocationIndex++;
944944
}
945-
const allocationIndex = low;
946945
ruleResult.allocationIndex = allocationIndex;
947946
destination = distribution.allocations[allocationIndex].destination;
948947
} else {

0 commit comments

Comments
 (0)