mirror of
https://github.com/codeflash-ai/codeflash-internal.git
synced 2026-05-04 18:25:18 +00:00
perf: parallelize event + features queries in getOptimizationEventById (#2559)
## Summary - Runs `optimization_events.findFirst` and `optimization_features.findUnique` in parallel via `Promise.all` - The features query only needs `trace_id` (a parameter), not the event result, making the queries independent - Wall-clock time goes from sum of both queries to max of either ## Evidence - Proof doc: `js/cf-webapp/proof/15-parallel-optimization-event.md` ## Test plan - [ ] `bash js/cf-webapp/proof/reproducers/15-parallel-optimization-event.sh` — 6/6 checks pass - [ ] Optimization review page loads correctly with review quality data
This commit is contained in:
parent
dc684b9a28
commit
c7df7bf27c
3 changed files with 134 additions and 28 deletions
42
js/cf-webapp/proof/15-parallel-optimization-event.md
Normal file
42
js/cf-webapp/proof/15-parallel-optimization-event.md
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
# Proof 15: Parallelize getOptimizationEventById Queries
|
||||
|
||||
## What Changed
|
||||
In `src/app/(dashboard)/review-optimizations/[traceId]/action.ts`, the `getOptimizationEventById` function previously ran two Prisma queries sequentially:
|
||||
1. Fetch the optimization event (with repository include)
|
||||
2. Then, if found, fetch `optimization_features` for review quality data
|
||||
|
||||
Now both queries run in parallel via `Promise.all`, since the features query only needs `trace_id` (available upfront), not the event result.
|
||||
|
||||
## Why
|
||||
The two queries are independent — `optimization_features` is looked up by `trace_id`, which is a parameter, not derived from the event result. Running them sequentially wastes wall-clock time equal to the slower query's latency.
|
||||
|
||||
**Before**: Total time = event query + features query (sequential)
|
||||
**After**: Total time = max(event query, features query) (parallel)
|
||||
|
||||
## Evidence
|
||||
|
||||
### Before (sequential)
|
||||
```typescript
|
||||
const event = await prisma.optimization_events.findFirst({ where, include: { repository: true } })
|
||||
if (event) {
|
||||
const features = await prisma.optimization_features.findUnique({ where: { trace_id: event.trace_id }, ... })
|
||||
return { ...event, review_quality: features?.review_quality || null, ... }
|
||||
}
|
||||
```
|
||||
|
||||
### After (parallel)
|
||||
```typescript
|
||||
const [event, features] = await Promise.all([
|
||||
prisma.optimization_events.findFirst({ where, include: { repository: true } }),
|
||||
prisma.optimization_features.findUnique({ where: { trace_id }, select: { review_quality: true, review_explanation: true } }),
|
||||
])
|
||||
return { ...event, review_quality: features?.review_quality || null, ... }
|
||||
```
|
||||
|
||||
Key insight: the features query uses `trace_id` (the function parameter), not `event.trace_id` (the result). This makes the queries truly independent.
|
||||
|
||||
## How to Verify
|
||||
```bash
|
||||
cd js/cf-webapp
|
||||
bash proof/reproducers/15-parallel-optimization-event.sh
|
||||
```
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
#!/usr/bin/env bash
|
||||
# Proof reproducer for commit 15: parallelize getOptimizationEventById
|
||||
set -euo pipefail
|
||||
|
||||
PASS=0
|
||||
FAIL=0
|
||||
TOTAL=0
|
||||
|
||||
check() {
|
||||
local desc="$1"; shift
|
||||
TOTAL=$((TOTAL + 1))
|
||||
if "$@" >/dev/null 2>&1; then
|
||||
echo " PASS: $desc"
|
||||
PASS=$((PASS + 1))
|
||||
else
|
||||
echo " FAIL: $desc"
|
||||
FAIL=$((FAIL + 1))
|
||||
fi
|
||||
}
|
||||
|
||||
check_not() {
|
||||
local desc="$1"; shift
|
||||
TOTAL=$((TOTAL + 1))
|
||||
if "$@" >/dev/null 2>&1; then
|
||||
echo " FAIL: $desc"
|
||||
FAIL=$((FAIL + 1))
|
||||
else
|
||||
echo " PASS: $desc"
|
||||
PASS=$((PASS + 1))
|
||||
fi
|
||||
}
|
||||
|
||||
ACTION_FILE="src/app/(dashboard)/review-optimizations/[traceId]/action.ts"
|
||||
|
||||
echo "=== Proof 15: parallelize getOptimizationEventById ==="
|
||||
echo ""
|
||||
|
||||
echo "--- Parallelization checks ---"
|
||||
check "Promise.all used in getOptimizationEventById" \
|
||||
grep -q 'Promise.all' "$ACTION_FILE"
|
||||
|
||||
check "event and features destructured from Promise.all" \
|
||||
grep -q '\[event, features\].*await Promise.all' "$ACTION_FILE"
|
||||
|
||||
check "optimization_events.findFirst inside Promise.all block" \
|
||||
grep -q 'optimization_events.findFirst' "$ACTION_FILE"
|
||||
|
||||
check "optimization_features.findUnique inside Promise.all block" \
|
||||
grep -q 'optimization_features.findUnique' "$ACTION_FILE"
|
||||
|
||||
echo ""
|
||||
echo "--- Independence check ---"
|
||||
check "features query uses trace_id param directly" \
|
||||
grep -q 'where: { trace_id }' "$ACTION_FILE"
|
||||
|
||||
check_not "no sequential if(event) then features pattern" \
|
||||
grep -q 'if (event)' "$ACTION_FILE"
|
||||
|
||||
echo ""
|
||||
echo "=== Results: $PASS/$TOTAL passed, $FAIL failed ==="
|
||||
[ "$FAIL" -eq 0 ] && echo "ALL CHECKS PASSED" || echo "SOME CHECKS FAILED"
|
||||
exit "$FAIL"
|
||||
|
|
@ -157,42 +157,44 @@ export async function getOptimizationEventById({
|
|||
trace_id,
|
||||
...buildOptimizationOrCondition(payload, repoIds),
|
||||
}
|
||||
const event = await prisma.optimization_events.findFirst({
|
||||
where,
|
||||
include: {
|
||||
repository: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (event) {
|
||||
// Fetch review_quality and review_explanation from optimization_features
|
||||
const features = await prisma.optimization_features.findUnique({
|
||||
where: { trace_id: event.trace_id },
|
||||
// Fire both queries in parallel — features only needs trace_id, not the event result
|
||||
const [event, features] = await Promise.all([
|
||||
prisma.optimization_events.findFirst({
|
||||
where,
|
||||
include: {
|
||||
repository: true,
|
||||
},
|
||||
}),
|
||||
prisma.optimization_features.findUnique({
|
||||
where: { trace_id },
|
||||
select: {
|
||||
review_quality: true,
|
||||
review_explanation: true,
|
||||
},
|
||||
})
|
||||
}),
|
||||
])
|
||||
|
||||
// Track that this optimization was reviewed
|
||||
const userId = "userId" in payload ? payload.userId : undefined
|
||||
if (userId) {
|
||||
trackOptimizationReviewed(userId, {
|
||||
traceId: event.trace_id,
|
||||
functionName: event.function_name,
|
||||
repositoryName: event.repository?.full_name ?? null,
|
||||
status: event.status,
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
...event,
|
||||
review_quality: features?.review_quality || null,
|
||||
review_explanation: features?.review_explanation || null,
|
||||
}
|
||||
if (!event) {
|
||||
return null
|
||||
}
|
||||
|
||||
return event
|
||||
// Track that this optimization was reviewed
|
||||
const userId = "userId" in payload ? payload.userId : undefined
|
||||
if (userId) {
|
||||
trackOptimizationReviewed(userId, {
|
||||
traceId: event.trace_id,
|
||||
functionName: event.function_name,
|
||||
repositoryName: event.repository?.full_name ?? null,
|
||||
status: event.status,
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
...event,
|
||||
review_quality: features?.review_quality || null,
|
||||
review_explanation: features?.review_explanation || null,
|
||||
}
|
||||
}
|
||||
export async function saveOptimizationChanges({
|
||||
eventId,
|
||||
|
|
|
|||
Loading…
Reference in a new issue