Skip to content

Commit 7cef005

Browse files
committed
perf: remove getters and use standard functions
1 parent 57b27a2 commit 7cef005

File tree

8 files changed

+63
-33
lines changed

8 files changed

+63
-33
lines changed

.changeset/thick-tomatoes-watch.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
'bentocache': patch
3+
---
4+
5+
Deleted the getters usage in the CacheEntryOptions file. Looks like getters are super slow. Just removing them doubled the performance in some cases.
6+
7+
Before :
8+
9+
```sh
10+
┌─────────┬──────────────────────────────────┬─────────────────────┬─────────────────────┬────────────────────────┬────────────────────────┬─────────┐
11+
│ (index) │ Task name │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
12+
├─────────┼──────────────────────────────────┼─────────────────────┼─────────────────────┼────────────────────────┼────────────────────────┼─────────┤
13+
│ 0 │ 'L1 GetOrSet - BentoCache''16613 ± 97.87%''1560.0 ± 45.00''613098 ± 0.10%''641026 ± 19040' │ 83796 │
14+
│ 1 │ 'L1 GetOrSet - CacheManager''953451 ± 111.03%''160022 ± 3815.00''5700 ± 1.23%''6249 ± 151' │ 1049 │
15+
│ 4 │ 'Tiered GetOrSet - BentoCache''16105 ± 98.11%''1515.0 ± 45.00''636621 ± 0.08%''660066 ± 20206' │ 86675 │
16+
│ 5 │ 'Tiered GetOrSet - CacheManager''877297 ± 111.36%''161617 ± 2876.00''5948 ± 0.67%''6187 ± 112' │ 1140 │
17+
│ 6 │ 'Tiered Get - BentoCache''1542.4 ± 4.43%''992.00 ± 18.00''973931 ± 0.03%''1008065 ± 17966' │ 648343 │
18+
│ 7 │ 'Tiered Get - CacheManager''1957.6 ± 0.51%''1848.0 ± 26.00''534458 ± 0.02%''541126 ± 7722' │ 510827 │
19+
└─────────┴──────────────────────────────────┴─────────────────────┴─────────────────────┴────────────────────────┴────────────────────────┴─────────┘
20+
```
21+
22+
After:
23+
24+
```sh
25+
┌─────────┬──────────────────────────────────┬─────────────────────┬─────────────────────┬────────────────────────┬────────────────────────┬─────────┐
26+
│ (index) │ Task name │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
27+
├─────────┼──────────────────────────────────┼─────────────────────┼─────────────────────┼────────────────────────┼────────────────────────┼─────────┤
28+
│ 0 │ 'L1 GetOrSet - BentoCache''9610.3 ± 98.26%''1109.0 ± 29.00''879036 ± 0.05%''901713 ± 22979' │ 143980 │
29+
│ 1 │ 'L1 GetOrSet - CacheManager''906687 ± 110.96%''172470 ± 1785.00''5601 ± 0.56%''5798 ± 61' │ 1103 │
30+
│ 4 │ 'Tiered GetOrSet - BentoCache''8752.8 ± 98.40%''1060.0 ± 19.00''924367 ± 0.04%''943396 ± 17219' │ 158461 │
31+
│ 5 │ 'Tiered GetOrSet - CacheManager''925163 ± 111.45%''173578 ± 2970.00''5590 ± 0.55%''5761 ± 100' │ 1081 │
32+
│ 6 │ 'Tiered Get - BentoCache''556.57 ± 0.52%''511.00 ± 10.00''1923598 ± 0.01%''1956947 ± 37561' │ 1796720 │
33+
│ 7 │ 'Tiered Get - CacheManager''2060.2 ± 2.54%''1928.0 ± 20.00''513068 ± 0.02%''518672 ± 5325' │ 485387 │
34+
└─────────┴──────────────────────────────────┴─────────────────────┴─────────────────────┴────────────────────────┴────────────────────────┴─────────┘
35+
```
36+
37+
Pretty good improvement 😁

packages/bentocache/src/cache/cache.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class Cache implements CacheProvider {
6666
return remoteItem.getValue()
6767
}
6868

69-
if (!options.isGraceEnabled) {
69+
if (!options.isGraceEnabled()) {
7070
this.#stack.emit(cacheEvents.miss(key, this.name))
7171
return this.#resolveDefaultValue(defaultValueFn)
7272
}

packages/bentocache/src/cache/cache_entry/cache_entry_options.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function createCacheEntryOptions(
5959
* Logical TTL is when the value is considered expired
6060
* but still can be in the cache ( Grace period )
6161
*/
62-
get logicalTtl() {
62+
getLogicalTtl() {
6363
return logicalTtl
6464
},
6565

@@ -68,14 +68,14 @@ export function createCacheEntryOptions(
6868
* removed from the cache. This is the Grace period
6969
* duration
7070
*/
71-
get physicalTtl() {
71+
getPhysicalTtl() {
7272
return physicalTtl
7373
},
7474

7575
/**
7676
* Determine if the gracing system is enabled
7777
*/
78-
get isGraceEnabled() {
78+
isGraceEnabled() {
7979
return grace > 0
8080
},
8181

@@ -111,7 +111,7 @@ export function createCacheEntryOptions(
111111
options.ttl = newTtl
112112

113113
logicalTtl = resolveTtl(options.ttl)
114-
physicalTtl = self.isGraceEnabled ? grace : logicalTtl
114+
physicalTtl = self.isGraceEnabled() ? grace : logicalTtl
115115

116116
return self
117117
},
@@ -139,7 +139,7 @@ export function createCacheEntryOptions(
139139
* factory
140140
*/
141141
factoryTimeout(hasFallbackValue: boolean) {
142-
if (hasFallbackValue && self.isGraceEnabled && is.number(timeout)) {
142+
if (hasFallbackValue && self.isGraceEnabled() && is.number(timeout)) {
143143
return { type: 'soft', duration: timeout, exception: errors.E_FACTORY_SOFT_TIMEOUT }
144144
}
145145

@@ -152,7 +152,7 @@ export function createCacheEntryOptions(
152152
* Determine if we should use the SWR strategy
153153
*/
154154
shouldSwr(hasFallback: boolean) {
155-
return self.isGraceEnabled && timeout === 0 && hasFallback
155+
return self.isGraceEnabled() && timeout === 0 && hasFallback
156156
},
157157

158158
/**
@@ -167,7 +167,7 @@ export function createCacheEntryOptions(
167167
* that means we should wait at most for the soft timeout
168168
* duration.
169169
*/
170-
if (hasFallbackValue && self.isGraceEnabled && typeof timeout === 'number') {
170+
if (hasFallbackValue && self.isGraceEnabled() && typeof timeout === 'number') {
171171
return timeout
172172
}
173173
},

packages/bentocache/src/cache/facades/local_cache.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,16 @@ export class LocalCache {
4545
/**
4646
* If grace period is disabled and Physical TTL is 0 or less, we can just delete the item.
4747
*/
48-
if (!options.isGraceEnabled && options.physicalTtl && options.physicalTtl <= 0) {
48+
const physicalTtl = options.getPhysicalTtl()
49+
if (!options.isGraceEnabled() && physicalTtl && physicalTtl <= 0) {
4950
return this.delete(key, options)
5051
}
5152

5253
/**
5354
* Save the item to the local cache
5455
*/
5556
this.#logger.trace({ key, value, opId: options.id }, 'saving local cache item')
56-
this.#driver.set(key, value, options.physicalTtl)
57+
this.#driver.set(key, value, physicalTtl)
5758
}
5859

5960
/**

packages/bentocache/src/cache/facades/remote_cache.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class RemoteCache {
7373
*/
7474
async set(key: string, value: string, options: CacheEntryOptions) {
7575
return await this.#tryCacheOperation('set', options, false, async () => {
76-
await this.#driver.set(key, value, options.physicalTtl)
76+
await this.#driver.set(key, value, options.getPhysicalTtl())
7777
return true
7878
})
7979
}

packages/bentocache/src/cache/get_set/single_tier_handler.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ export class SingleTierHandler {
2525
return this.stack.logger
2626
}
2727

28-
get emitter() {
29-
return this.stack.emitter
30-
}
31-
3228
/**
3329
* Emit a CacheEvent using the emitter
3430
*/
@@ -63,7 +59,7 @@ export class SingleTierHandler {
6359
options: CacheEntryOptions,
6460
err: Error,
6561
) {
66-
if (options.isGraceEnabled && item) {
62+
if (options.isGraceEnabled() && item) {
6763
const isLogicallyExpired = item.isLogicallyExpired()
6864
this.#emit(cacheEvents.hit(key, item.getValue(), this.stack.name, isLogicallyExpired))
6965
this.logger.trace(
@@ -158,7 +154,7 @@ export class SingleTierHandler {
158154
'factory error',
159155
)
160156

161-
if (staleItem && options.isGraceEnabled) {
157+
if (staleItem && options.isGraceEnabled()) {
162158
this.#locks.release(key, releaser)
163159
return this.#applyFallbackAndReturnGracedValue(key, staleItem, options)
164160
}

packages/bentocache/src/cache/get_set/two_tier_handler.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ export class TwoTierHandler {
2525
return this.stack.logger
2626
}
2727

28-
get emitter() {
29-
return this.stack.emitter
30-
}
31-
3228
/**
3329
* Emit a CacheEvent using the emitter
3430
*/
@@ -83,7 +79,7 @@ export class TwoTierHandler {
8379
options: CacheEntryOptions,
8480
err: Error,
8581
) {
86-
if (options.isGraceEnabled && item) {
82+
if (options.isGraceEnabled() && item) {
8783
return this.#returnLocalCacheValue(key, item, options, 'local cache hit (graced)')
8884
}
8985

@@ -187,7 +183,7 @@ export class TwoTierHandler {
187183
'factory error',
188184
)
189185

190-
if (staleItem && options.isGraceEnabled) {
186+
if (staleItem && options.isGraceEnabled()) {
191187
this.#locks.release(key, releaser)
192188
return this.#applyFallbackAndReturnGracedValue(key, staleItem, options)
193189
}

packages/bentocache/tests/cache/cache_entry_options.spec.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@ test.group('Cache Entry Options', () => {
1010

1111
const options = createCacheEntryOptions(override, defaults)
1212

13-
assert.equal(options.logicalTtl, string.milliseconds.parse('10m'))
14-
assert.equal(options.physicalTtl, string.milliseconds.parse('30m'))
13+
assert.equal(options.getLogicalTtl(), string.milliseconds.parse('10m'))
14+
assert.equal(options.getPhysicalTtl(), string.milliseconds.parse('30m'))
1515
})
1616

1717
test('physical ttl should be logical ttl when grace period is disabled', ({ assert }) => {
1818
const options = createCacheEntryOptions({ ttl: '10m' })
1919

20-
assert.equal(options.physicalTtl, string.milliseconds.parse('10m'))
20+
assert.equal(options.getPhysicalTtl(), string.milliseconds.parse('10m'))
2121
})
2222

2323
test('physical ttl should be grace period ttl when enabled', ({ assert }) => {
2424
const options = createCacheEntryOptions({ ttl: '10m', grace: '30m' })
25-
assert.equal(options.physicalTtl, string.milliseconds.parse('30m'))
25+
assert.equal(options.getPhysicalTtl(), string.milliseconds.parse('30m'))
2626
})
2727

2828
test('null ttl should be kept and resolved to undefined', ({ assert }) => {
2929
const options = createCacheEntryOptions({ ttl: null, grace: '30m' })
30-
assert.deepEqual(options.logicalTtl, undefined)
30+
assert.deepEqual(options.getLogicalTtl(), undefined)
3131
})
3232

3333
test('clone with null ttl should be kept and resolved as undefined', ({ assert }) => {
3434
const options = createCacheEntryOptions({ ttl: '10m', grace: '30m' })
3535
const clone = options.cloneWith({ ttl: null })
3636

37-
assert.deepEqual(clone.logicalTtl, undefined)
37+
assert.deepEqual(clone.getLogicalTtl(), undefined)
3838
})
3939

4040
test('should assign timeouts', ({ assert }) => {
@@ -65,8 +65,8 @@ test.group('Cache Entry Options', () => {
6565
const r1 = createCacheEntryOptions({ grace: false })
6666
const r2 = r1.cloneWith({ grace: '60m' })
6767

68-
assert.isFalse(r1.isGraceEnabled)
69-
assert.isTrue(r2.isGraceEnabled)
68+
assert.isFalse(r1.getIsGraceEnabled())
69+
assert.isTrue(r2.getIsGraceEnabled())
7070
})
7171

7272
test('timeout should be soft one if fallback value and grace period enabled', ({ assert }) => {
@@ -112,7 +112,7 @@ test.group('Cache Entry Options', () => {
112112

113113
options.setLogicalTtl(string.milliseconds.parse('5m'))
114114

115-
assert.equal(options.logicalTtl, string.milliseconds.parse('5m'))
116-
assert.equal(options.physicalTtl, string.milliseconds.parse('5m'))
115+
assert.equal(options.getLogicalTtl(), string.milliseconds.parse('5m'))
116+
assert.equal(options.getPhysicalTtl(), string.milliseconds.parse('5m'))
117117
})
118118
})

0 commit comments

Comments
 (0)