Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
53bb8e9
feat: expensive cache
SkArchon Mar 9, 2026
fa033d7
feat: expensive query cache
SkArchon Mar 9, 2026
797b9a9
Merge branch 'main' into milinda/eng-9010-router-investigate-customiz…
SkArchon Mar 9, 2026
4378c80
fix: review comments
SkArchon Mar 9, 2026
4f644cf
fix: improvements
SkArchon Mar 9, 2026
2515b07
fix: review comments
SkArchon Mar 9, 2026
61bad8a
Merge branch 'main' into milinda/eng-9010-router-investigate-customiz…
SkArchon Mar 10, 2026
e094ee5
Merge branch 'main' into milinda/eng-9010-router-investigate-customiz…
SkArchon Mar 11, 2026
adf6498
fix: updates
SkArchon Mar 11, 2026
e36f2b3
fix: updates
SkArchon Mar 11, 2026
e53f464
Merge branch 'main' into milinda/eng-9010-router-investigate-customiz…
SkArchon Mar 11, 2026
80c5594
fix: only persist the expensive cache elements
SkArchon Mar 11, 2026
ed60ea5
fix: updates
SkArchon Mar 11, 2026
56f65b3
fix: updates
SkArchon Mar 11, 2026
83314ce
fix: temporary skip
SkArchon Mar 11, 2026
5ff028f
Merge remote-tracking branch 'origin/main' into milinda/eng-9010-rout…
SkArchon Mar 11, 2026
c495de1
fix: tests
SkArchon Mar 11, 2026
ed895d8
fix: tests
SkArchon Mar 11, 2026
46c1fb5
fix: updates
SkArchon Mar 11, 2026
8f2818d
fix: updates
SkArchon Mar 11, 2026
731829d
fix: tests
SkArchon Mar 11, 2026
4a0fed7
fix: updates
SkArchon Mar 11, 2026
2d80455
fix: updates
SkArchon Mar 11, 2026
c708af5
fix: review comments
SkArchon Mar 12, 2026
64fa885
fix: review comments
SkArchon Mar 12, 2026
40dc67f
fix: review comments
SkArchon Mar 12, 2026
6e0923c
fix: cleanup
SkArchon Mar 12, 2026
424c999
fix: review comments
SkArchon Mar 12, 2026
1c7707c
Merge branch 'main' into milinda/eng-9010-router-investigate-customiz…
SkArchon Mar 12, 2026
478e3de
fix: review comments
SkArchon Mar 12, 2026
6163132
Merge branch 'main' into milinda/eng-9010-router-investigate-customiz…
StarpTech Mar 12, 2026
87ac36d
fix: changes
SkArchon Mar 12, 2026
cce5c7f
fix: review comments
SkArchon Mar 12, 2026
830dfc0
fix: refactoring
SkArchon Mar 12, 2026
238c379
fix: updates
SkArchon Mar 12, 2026
91c0b79
fix: comments
SkArchon Mar 12, 2026
3f12de9
Merge remote-tracking branch 'origin/main' into milinda/eng-9010-rout…
SkArchon Mar 15, 2026
426093f
feat: documentation
SkArchon Mar 15, 2026
09a4df6
fix: updates
SkArchon Mar 15, 2026
6f3cfd0
fix: updates
SkArchon Mar 15, 2026
9e7540e
fix: iterators
SkArchon Mar 16, 2026
8e6da6b
Merge branch 'main' into milinda/eng-9010-router-investigate-customiz…
SkArchon Mar 16, 2026
bac502b
fix: refactor
SkArchon Mar 16, 2026
e99824e
fix: updates
SkArchon Mar 16, 2026
d1ffde4
fix: review comments
SkArchon Mar 17, 2026
04ee8b0
fix: review comments
SkArchon Mar 18, 2026
8ded16b
fix: review comments
SkArchon Mar 18, 2026
a9828a0
fix: updates
SkArchon Mar 18, 2026
283437c
fix: updates
SkArchon Mar 18, 2026
41f64a6
fix: updates
SkArchon Mar 18, 2026
4fb6860
fix: bench cleanup
SkArchon Mar 18, 2026
9be8a47
fix: changes
SkArchon Mar 18, 2026
e393588
fix: changes
SkArchon Mar 18, 2026
61cc607
Merge remote-tracking branch 'origin/main' into milinda/eng-9010-rout…
SkArchon Mar 18, 2026
853135b
fix: updates
SkArchon Mar 18, 2026
6cfe7cf
fix: tests
SkArchon Mar 18, 2026
0148d7b
Merge branch 'main' into milinda/eng-9010-router-investigate-customiz…
SkArchon Mar 19, 2026
e0db927
Merge branch 'main' into milinda/eng-9010-router-investigate-customiz…
SkArchon Mar 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
523 changes: 523 additions & 0 deletions router-tests/expensive_query_cache_test.go

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion router/core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,9 @@ type operationContext struct {
preparedPlan *planWithMetaData
traceOptions resolve.TraceOptions
executionOptions resolve.ExecutionOptions
planCacheHit bool
planCacheHit bool
expensivePlanCacheHit bool
expensiveCacheEnabled bool
initialPayload []byte
extensions []byte
persistedID string
Expand Down
116 changes: 116 additions & 0 deletions router/core/expensive_query_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package core

import (
"fmt"
"sync"
"time"
)

// expensivePlanEntry holds a cached plan and the duration it took to plan.
type expensivePlanEntry struct {
plan *planWithMetaData
duration time.Duration
}

// expensivePlanCache is a bounded, mutex-protected map that holds expensive plans
// that should not be subject to TinyLFU eviction in the main cache.
// It tracks the minimum-duration entry so that rejection of cheaper entries is O(1).
type expensivePlanCache struct {
mu sync.RWMutex
entries map[uint64]*expensivePlanEntry
maxSize int
minKey uint64
minDur time.Duration
}

func newExpensivePlanCache(maxSize int) (*expensivePlanCache, error) {
if maxSize < 1 {
return nil, fmt.Errorf("expensive query cache size must be at least 1, got %d", maxSize)
}
return &expensivePlanCache{
entries: make(map[uint64]*expensivePlanEntry, maxSize),
maxSize: maxSize,
}, nil
}

func (c *expensivePlanCache) Get(key uint64) (*planWithMetaData, bool) {
c.mu.RLock()
defer c.mu.RUnlock()

entry, ok := c.entries[key]
if !ok {
return nil, false
}
return entry.plan, true
}

// Set stores a plan in the expensive cache. When at capacity, it only adds the
// new entry if its duration exceeds the current minimum; otherwise, it is skipped.
func (c *expensivePlanCache) Set(key uint64, plan *planWithMetaData, duration time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()

if c.entries == nil {
return
}

// If key already exists, update it
if _, ok := c.entries[key]; ok {
c.entries[key] = &expensivePlanEntry{plan: plan, duration: duration}
// If this was the tracked min, or the new duration is lower, refresh the min
if key == c.minKey || duration < c.minDur {
c.refreshMin()
}
return
}

// If not at capacity, just add and update min tracking
if len(c.entries) < c.maxSize {
c.entries[key] = &expensivePlanEntry{plan: plan, duration: duration}
if len(c.entries) == 1 || duration < c.minDur {
c.minKey = key
c.minDur = duration
}
return
}

// At capacity: reject if new entry is not more expensive than the current minimum
if duration <= c.minDur {
return
}

// Evict the minimum and insert the new entry
delete(c.entries, c.minKey)
c.entries[key] = &expensivePlanEntry{plan: plan, duration: duration}
c.refreshMin()
}

// refreshMin rescans the entries to find the new minimum. Must be called with mu held.
func (c *expensivePlanCache) refreshMin() {
first := true
for k, e := range c.entries {
if first || e.duration < c.minDur {
c.minKey = k
c.minDur = e.duration
first = false
}
}
}

func (c *expensivePlanCache) IterValues(cb func(v *planWithMetaData) bool) {
c.mu.RLock()
defer c.mu.RUnlock()

for _, e := range c.entries {
if cb(e.plan) {
return
}
}
}

func (c *expensivePlanCache) Close() {
c.mu.Lock()
defer c.mu.Unlock()

c.entries = nil
}
Loading
Loading