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

Improve performance of memoized validator functions. #122

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"test:pw": "pnpm -F @localfirst/automerge-repo-todos test:pw",
"test:pw:log": "pnpm -F @localfirst/automerge-repo-todos test:pw:log",
"test:pw:ui": "pnpm -F @localfirst/automerge-repo-todos test:pw:ui",
"bench": "cross-env DEBUG=localfirst*,automerge* DEBUG_COLORS=1 vitest bench",
"watch": "lerna watch -- lerna run build --scope=\\$LERNA_PACKAGE_NAME --include-dependents"
},
"devDependencies": {
Expand Down
45 changes: 45 additions & 0 deletions packages/auth/src/test/auth.benchmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, bench } from 'vitest'
import * as Auth from '../index.js'

describe('auth', () => {
bench('a new member joining', () => {
const founderUsername = 'founder'
const founderContext = {
user: Auth.createUser(founderUsername, founderUsername),
device: Auth.createDevice({ userId: founderUsername, deviceName: 'laptop' }),
}
const teamName = 'Test'
const team = Auth.createTeam(teamName, founderContext)

//
// Add 100 test users
//

const usernames = [...new Array(100).keys()].map(i => 'user-' + i)

for (const username of usernames) {
const user = Auth.createUser(username, username)
const device = Auth.createDevice({ userId: username, deviceName: 'dev/' + username })
team.addForTesting(user, [], Auth.redactDevice(device))
}

//
// Invite new user and have them join
//

const { seed } = team.inviteMember({ maxUses: 1000 })

const username = 'new-user'
const user = Auth.createUser(username, username)
const device = Auth.createDevice({ userId: username, deviceName: 'laptop' })
const proofOfInvitation = Auth.generateProof(seed)

team.admitMember(proofOfInvitation, Auth.redactKeys(user.keys), user.userName)

const serializedGraph = team.save()
const teamKeyring = team.teamKeyring()
const team2 = new Auth.Team({ source: serializedGraph, context: { user, device }, teamKeyring })

team2.join(teamKeyring)
})
})
2 changes: 1 addition & 1 deletion packages/crdx/src/validator/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const fail = (msg: string, args?: any) => {
const memoizeFunctionMap = (source: ValidatorSet) => {
const result = {} as ValidatorSet
const memoizeResolver = (link: Link<any, any>, graph: Graph<any, any>) => {
return `${hash('memoize', link)}:${hash('memoize', graph)}`
return `${link.hash}:${graph.root}`
}

for (const key in source) result[key] = memoize(source[key], memoizeResolver)
Expand Down
Loading