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

experiment fast-json-stringify #222

Closed
wants to merge 1 commit 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
27 changes: 27 additions & 0 deletions benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,33 @@ test('initial indexing', async (t) => {

await ended.promise
})
test('initial indexing leveldb about-self', async (t) => {
await sleep(500) // some silence to make it easier to read the CPU profiler
const ended = DeferredPromise()

const sbot = SecretStack({ appKey: caps.shs })
.use(require('../'))
.use(require('../about-self'))
.call(null, { keys, path: dir })

const start = Date.now()

sbot.db.onDrain('base', () => {
sbot.db.onDrain('aboutSelf', () => {
const duration = Date.now() - start
t.pass(`duration: ${duration}ms`)
fs.appendFileSync(
reportPath,
`| Initial indexing leveldb about-self | ${duration}ms |\n`
)
updateMaxRAM()
global.gc()
sbot.close(() => ended.resolve())
})
})

await ended.promise
})

test('initial indexing maxcpu 86', async (t) => {
rimraf.sync(indexesPath)
Expand Down
31 changes: 30 additions & 1 deletion indexes/about-self.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const bipf = require('bipf')
const pull = require('pull-stream')
const pl = require('pull-level')
const fastJson = require('fast-json-stringify')
const Plugin = require('./plugin')

const bValue = Buffer.from('value')
Expand All @@ -9,9 +10,33 @@ const bContent = Buffer.from('content')
const bType = Buffer.from('type')
const bAbout = Buffer.from('about')

const stringify = fastJson({
title: 'AboutSelf Value',
type: 'object',
properties: {
name: {
type: 'string',
},
image: {
type: 'string',
},
description: {
type: 'string',
},
},
})

const valueEncoding = {
encode: stringify,
decode: JSON.parse,
buffer: false,
type: 'json',
}

// feedId => hydratedAboutObj
module.exports = class AboutSelf extends Plugin {
constructor(log, dir) {
// super(log, dir, 'aboutSelf', 3, undefined, valueEncoding)
super(log, dir, 'aboutSelf', 3, 'json', 'json')
this.profiles = {}
}
Expand Down Expand Up @@ -57,7 +82,11 @@ module.exports = class AboutSelf extends Plugin {
}

updateProfileData(author, content) {
let profile = this.profiles[author] || {}
let profile = this.profiles[author] || {
name: '',
description: '',
image: '',
}

if (content.name) profile.name = content.name

Expand Down
10 changes: 8 additions & 2 deletions indexes/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,18 @@ module.exports = class Plugin {
// doesn't support it, but `level-post` is a dependency in `pull-level`.
// Note, this._keyEncoding and this._valueEncoding are strings.
get keyEncoding() {
if (encodings[this._keyEncoding]) return encodings[this._keyEncoding]
const e = this._keyEncoding
if (typeof e === 'object' && e) return e
else if (typeof e === 'string' && encodings[e]) return encodings[e]
// if (encodings[this._keyEncoding]) return encodings[this._keyEncoding]
else return undefined
}

get valueEncoding() {
if (encodings[this._valueEncoding]) return encodings[this._valueEncoding]
const e = this._valueEncoding
if (typeof e === 'object' && e) return e
else if (typeof e === 'string' && encodings[e]) return encodings[e]
// if (encodings[this._valueEncoding]) return encodings[this._valueEncoding]
else return undefined
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"binary-search-bounds": "^2.0.4",
"bipf": "~1.5.0",
"debug": "~4.3.1",
"fast-json-stringify": "^2.5.2",
"fastintcompression": "0.0.4",
"flumecodec": "0.0.1",
"flumelog-offset": "3.4.4",
Expand Down