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

Performance optimization: make PREFERRED_TRIM_SIZE configurable #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
11 changes: 7 additions & 4 deletions src/y-indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Observable } from 'lib0/observable'
const customStoreName = 'custom'
const updatesStoreName = 'updates'

export const PREFERRED_TRIM_SIZE = 500
export const DEFAULT_PREFERRED_TRIM_SIZE = 500

/**
* @param {IndexeddbPersistence} idbPersistence
Expand Down Expand Up @@ -36,7 +36,7 @@ export const fetchUpdates = (idbPersistence, beforeApplyUpdatesCallback = () =>
export const storeState = (idbPersistence, forceStore = true) =>
fetchUpdates(idbPersistence)
.then(updatesStore => {
if (forceStore || idbPersistence._dbsize >= PREFERRED_TRIM_SIZE) {
if (forceStore || idbPersistence._dbsize >= idbPersistence.PREFERRED_TRIM_SIZE) {
idb.addAutoKey(updatesStore, Y.encodeStateAsUpdate(idbPersistence.doc))
.then(() => idb.del(updatesStore, idb.createIDBKeyRangeUpperBound(idbPersistence._dbref, true)))
.then(() => idb.count(updatesStore).then(cnt => { idbPersistence._dbsize = cnt }))
Expand All @@ -55,14 +55,17 @@ export class IndexeddbPersistence extends Observable {
/**
* @param {string} name
* @param {Y.Doc} doc
* @param {Object} [options={}]
* @param {number} [options.PREFERRED_TRIM_SIZE=500]
frontend-sensei marked this conversation as resolved.
Show resolved Hide resolved
*/
constructor (name, doc) {
constructor (name, doc, options = {}) {
super()
this.doc = doc
this.name = name
this._dbref = 0
this._dbsize = 0
this._destroyed = false
this.PREFERRED_TRIM_SIZE = options.PREFERRED_TRIM_SIZE || DEFAULT_PREFERRED_TRIM_SIZE
frontend-sensei marked this conversation as resolved.
Show resolved Hide resolved
/**
* @type {IDBDatabase|null}
*/
Expand Down Expand Up @@ -108,7 +111,7 @@ export class IndexeddbPersistence extends Observable {
if (this.db && origin !== this) {
const [updatesStore] = idb.transact(/** @type {IDBDatabase} */ (this.db), [updatesStoreName])
idb.addAutoKey(updatesStore, update)
if (++this._dbsize >= PREFERRED_TRIM_SIZE) {
if (++this._dbsize >= this.PREFERRED_TRIM_SIZE) {
// debounce store call
if (this._storeTimeoutId !== null) {
clearTimeout(this._storeTimeoutId)
Expand Down
10 changes: 5 additions & 5 deletions tests/y-indexeddb.tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import * as Y from 'yjs'
import { IndexeddbPersistence, clearDocument, PREFERRED_TRIM_SIZE, fetchUpdates } from '../src/y-indexeddb.js'
import { IndexeddbPersistence, clearDocument, DEFAULT_PREFERRED_TRIM_SIZE, fetchUpdates } from '../src/y-indexeddb.js'
import * as t from 'lib0/testing.js'
import * as promise from 'lib0/promise.js'

Expand Down Expand Up @@ -42,12 +42,12 @@ export const testIdbUpdateAndMerge = async tc => {
await persistence2.whenSynced
t.assert(calledObserver)
t.assert(arr2.length === 2)
for (let i = 2; i < PREFERRED_TRIM_SIZE + 1; i++) {
for (let i = 2; i < DEFAULT_PREFERRED_TRIM_SIZE + 1; i++) {
arr1.insert(i, [i])
}
await promise.wait(100)
await fetchUpdates(persistence2)
t.assert(arr2.length === PREFERRED_TRIM_SIZE + 1)
t.assert(arr2.length === DEFAULT_PREFERRED_TRIM_SIZE + 1)
t.assert(persistence1._dbsize === 1) // wait for dbsize === 0. db should be concatenated
}

Expand All @@ -70,11 +70,11 @@ export const testIdbConcurrentMerge = async tc => {
await persistence2.whenSynced
t.assert(arr2.length === 2)
arr1.insert(0, ['left'])
for (let i = 0; i < PREFERRED_TRIM_SIZE + 1; i++) {
for (let i = 0; i < DEFAULT_PREFERRED_TRIM_SIZE + 1; i++) {
arr1.insert(i, [i])
}
arr2.insert(0, ['right'])
for (let i = 0; i < PREFERRED_TRIM_SIZE + 1; i++) {
for (let i = 0; i < DEFAULT_PREFERRED_TRIM_SIZE + 1; i++) {
arr2.insert(i, [i])
}
await promise.wait(100)
Expand Down
Loading