Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): add reusable rxSwr operator #7562

Merged
merged 3 commits into from
Oct 7, 2024
Merged

refactor(core): add reusable rxSwr operator #7562

merged 3 commits into from
Oct 7, 2024

Conversation

bjoerge
Copy link
Member

@bjoerge bjoerge commented Sep 30, 2024

Description

This PR introduces a new utility function createSWR for implementing stale-while-revalidate caching in RxJS observables. The createSwr function lets you create an operator function that can be used with a cache key for storing the last emitted value. Once an observable with the same cache key is subscribed to, it will emit the cached value (if any) immediately while waiting for the fresh value to arrive.

This PR also refactors the listenSearchQuery function to use this new SWR implementation, replacing the previous LRU cache approach.

What to review

  • The new rxSwr.ts file in the core/util directory, which contains the createSWR function and related types.
  • The changes in listenSearchQuery.ts, particularly the removal of the memoLRU function and the integration of the new swr utility.

To add SWR to any observable:

import {createSWR} from 'sanity'

const swr = createSWR<ObservableValueType>({maxSize: 100})

// In your observable pipeline:
someObservable$.pipe(
  swr('<some cacheKey identifying the observable>'),
  map(({fromCache, value}) => {
    // Handle cached and fresh data
    // `fromCache` is true if `value` was read from cache, otherwise false.
  })
)

Testing

Included some basic unit tests for rxSwr

Notes for release

n/a internal

Copy link

vercel bot commented Sep 30, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
page-building-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 6, 2024 10:58pm
performance-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 6, 2024 10:58pm
test-compiled-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 6, 2024 10:58pm
test-next-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 6, 2024 10:58pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 6, 2024 10:58pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Oct 6, 2024 10:58pm

Copy link
Member Author

bjoerge commented Sep 30, 2024

@bjoerge bjoerge marked this pull request as ready for review September 30, 2024 15:50
@bjoerge bjoerge requested a review from a team as a code owner September 30, 2024 15:50
@bjoerge bjoerge requested review from juice49 and removed request for a team September 30, 2024 15:50
@bjoerge bjoerge changed the title n refactor(core): add reusable rxSwr operator Sep 30, 2024
Copy link
Contributor

No changes to documentation

Copy link
Contributor

github-actions bot commented Sep 30, 2024

Component Testing Report Updated Oct 6, 2024 10:55 PM (UTC)

✅ All Tests Passed -- expand for details
File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 45s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 9s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ✅ Passed (Inspect) 31s 6 0 0
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 36s 11 7 0
formBuilder/inputs/PortableText/copyPaste/CopyPasteFields.spec.tsx ✅ Passed (Inspect) 0s 0 12 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 17s 6 0 0
formBuilder/inputs/PortableText/DisableFocusAndUnset.spec.tsx ✅ Passed (Inspect) 10s 3 0 0
formBuilder/inputs/PortableText/DragAndDrop.spec.tsx ✅ Passed (Inspect) 3m 0s 0 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 44s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 42s 21 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 1m 14s 18 0 0
formBuilder/inputs/PortableText/PresenceCursors.spec.tsx ✅ Passed (Inspect) 9s 3 9 0
formBuilder/inputs/PortableText/RangeDecoration.spec.tsx ✅ Passed (Inspect) 25s 9 0 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 17s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 35s 12 0 0
formBuilder/tree-editing/TreeEditing.spec.tsx ✅ Passed (Inspect) 0s 0 3 0
formBuilder/tree-editing/TreeEditingNestedObjects.spec.tsx ✅ Passed (Inspect) 0s 0 3 0

juice49
juice49 previously approved these changes Oct 1, 2024
Copy link
Contributor

@juice49 juice49 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me!

Base automatically changed from fix-memoize-list-results to next October 2, 2024 06:15
@bjoerge bjoerge dismissed juice49’s stale review October 2, 2024 06:15

The base branch was changed.

@bjoerge bjoerge requested a review from juice49 October 2, 2024 06:16
juice49
juice49 previously approved these changes Oct 2, 2024
Copy link
Contributor

github-actions bot commented Oct 6, 2024

⚡️ Editor Performance Report

Updated Sun, 06 Oct 2024 23:07:41 GMT

Benchmark reference
latency of sanity@latest
experiment
latency of this branch
Δ (%)
latency difference
article (title) 17.9 efps (56ms) 18.5 efps (54ms) -2ms (-3.6%)
article (body) 63.3 efps (16ms) 59.9 efps (17ms) +1ms (+5.7%)
article (string inside object) 19.6 efps (51ms) 18.9 efps (53ms) +2ms (+3.9%)
article (string inside array) 15.2 efps (66ms) 14.5 efps (69ms) +3ms (+4.5%)
recipe (name) 31.3 efps (32ms) 30.8 efps (33ms) +1ms (+1.6%)
recipe (description) 34.5 efps (29ms) 35.7 efps (28ms) -1ms (-3.4%)
recipe (instructions) 99.9+ efps (6ms) 99.9+ efps (6ms) -0ms (-/-%)
synthetic (title) 15.2 efps (66ms) 14.7 efps (68ms) +2ms (+3.0%)
synthetic (string inside object) 15.9 efps (63ms) 15.6 efps (64ms) +1ms (+1.6%)

efps — editor "frames per second". The number of updates assumed to be possible within a second.

Derived from input latency. efps = 1000 / input_latency

Detailed information

🏠 Reference result

The performance result of sanity@latest

Benchmark latency p75 p90 p99 blocking time test duration
article (title) 56ms 62ms 120ms 247ms 1063ms 14.9s
article (body) 16ms 18ms 21ms 122ms 149ms 5.3s
article (string inside object) 51ms 54ms 61ms 154ms 811ms 8.7s
article (string inside array) 66ms 69ms 80ms 171ms 1639ms 9.6s
recipe (name) 32ms 35ms 39ms 86ms 103ms 10.3s
recipe (description) 29ms 30ms 34ms 60ms 22ms 6.3s
recipe (instructions) 6ms 9ms 10ms 32ms 0ms 3.4s
synthetic (title) 66ms 70ms 76ms 362ms 2042ms 18.1s
synthetic (string inside object) 63ms 67ms 72ms 107ms 1778ms 9.8s

🧪 Experiment result

The performance result of this branch

Benchmark latency p75 p90 p99 blocking time test duration
article (title) 54ms 59ms 81ms 158ms 1073ms 16.1s
article (body) 17ms 19ms 22ms 172ms 283ms 5.7s
article (string inside object) 53ms 55ms 60ms 168ms 945ms 8.6s
article (string inside array) 69ms 74ms 83ms 191ms 1821ms 10.0s
recipe (name) 33ms 35ms 43ms 89ms 70ms 10.3s
recipe (description) 28ms 31ms 51ms 89ms 32ms 6.3s
recipe (instructions) 6ms 8ms 9ms 20ms 0ms 3.3s
synthetic (title) 68ms 69ms 79ms 157ms 1817ms 18.1s
synthetic (string inside object) 64ms 68ms 133ms 404ms 2188ms 10.2s

📚 Glossary

column definitions

  • benchmark — the name of the test, e.g. "article", followed by the label of the field being measured, e.g. "(title)".
  • latency — the time between when a key was pressed and when it was rendered. derived from a set of samples. the median (p50) is shown to show the most common latency.
  • p75 — the 75th percentile of the input latency in the test run. 75% of the sampled inputs in this benchmark were processed faster than this value. this provides insight into the upper range of typical performance.
  • p90 — the 90th percentile of the input latency in the test run. 90% of the sampled inputs were faster than this. this metric helps identify slower interactions that occurred less frequently during the benchmark.
  • p99 — the 99th percentile of the input latency in the test run. only 1% of sampled inputs were slower than this. this represents the worst-case scenarios encountered during the benchmark, useful for identifying potential performance outliers.
  • blocking time — the total time during which the main thread was blocked, preventing user input and UI updates. this metric helps identify performance bottlenecks that may cause the interface to feel unresponsive.
  • test duration — how long the test run took to complete.

@bjoerge bjoerge added this pull request to the merge queue Oct 7, 2024
Merged via the queue into next with commit 13518b2 Oct 7, 2024
57 of 60 checks passed
@bjoerge bjoerge deleted the refactor-rx-swr branch October 7, 2024 08:27
bjoerge added a commit that referenced this pull request Oct 7, 2024
### Description

This PR introduces a new utility function `createSWR` for implementing
stale-while-revalidate caching in RxJS observables. The `createSwr`
function lets you create an operator function that can be used with a
cache key for storing the last emitted value. Once an observable with
the same cache key is subscribed to, it will emit the cached value (if
any) immediately while waiting for the fresh value to arrive.

This PR also refactors the `listenSearchQuery` function to use this new
SWR implementation, replacing the previous LRU cache approach.

### What to review

- The new `rxSwr.ts` file in the `core/util` directory, which contains
the `createSWR` function and related types.
- The changes in `listenSearchQuery.ts`, particularly the removal of the
`memoLRU` function and the integration of the new `swr` utility.

To add SWR to any observable:

```typescript
import {createSWR} from 'sanity'

const swr = createSWR<ObservableValueType>({maxSize: 100})

// In your observable pipeline:
someObservable$.pipe(
  swr('<some cacheKey identifying the observable>'),
  map(({fromCache, value}) => {
    // Handle cached and fresh data
    // `fromCache` is true if `value` was read from cache, otherwise false.
  })
)
```

### Testing

Included some basic unit tests for rxSwr

### Notes for release

n/a internal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants