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

Micro gc#2 #474

Merged
merged 2 commits into from
Mar 11, 2024
Merged
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
39 changes: 27 additions & 12 deletions wasm_parts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,32 @@ const getWasm = (() => {
let wasm = null
let cnt = 0
let run = false
let int
async function init () {
go = new _Go();
run = true
const _wasm = await WebAssembly.instantiate(
gunzipSync(fs.readFileSync(WASM_URL)), go.importObject)
setInterval(() => {
int && clearInterval(int)
int = setInterval(() => {
console.log(`WASM SIZE: ${Math.floor(wasm.exports.memory.buffer.byteLength / 1024 / 1024)} MB`)
}, 5000)
go.run(_wasm.instance)
wasm = _wasm.instance
wasm.exports.setMaxSamples(process.env.ADVANCED_PROMETHEUS_MAX_SAMPLES || 5000000)
wasm.exportsWrap = Object.fromEntries(
Object.entries(wasm.exports).map(([_k, _v]) => {
return [_k, (...args) => {
const _wasm = wasm
try {
return _wasm.exports[_k].bind(_wasm)(...args)
} catch (e) {
_wasm === wasm && init()
throw e
}
}]
})
)
cnt = 0
run = false
}
Expand Down Expand Up @@ -56,7 +71,7 @@ module.exports.pqlRangeQuery = async (query, startMs, endMs, stepMs, getData) =>
const end = endMs || Date.now()
const step = stepMs || 15000
return await pql(query,
(ctx) => _wasm.exports.pqlRangeQuery(ctx.id, start, end, step),
(ctx) => _wasm.exportsWrap.pqlRangeQuery(ctx.id, start, end, step),
(matchers) => getData(matchers, start, end))
}

Expand All @@ -71,7 +86,7 @@ module.exports.pqlInstantQuery = async (query, timeMs, getData) => {
const time = timeMs || Date.now()
const _wasm = getWasm()
return await pql(query,
(ctx) => _wasm.exports.pqlInstantQuery(ctx.id, time),
(ctx) => _wasm.exportsWrap.pqlInstantQuery(ctx.id, time),
(matchers) => getData(matchers, time - 300000, time))
}

Expand All @@ -82,7 +97,7 @@ module.exports.pqlMatchers = (query) => {
ctx.create()
try {
ctx.write(query)
const res1 = _wasm.exports.pqlSeries(id)
const res1 = _wasm.exportsWrap.pqlSeries(id)
if (res1 !== 0) {
throw new WasmError(ctx.read())
}
Expand Down Expand Up @@ -124,7 +139,7 @@ module.exports.TranspileTraceQL = (request) => {
_ctx = new Ctx(id, _wasm)
_ctx.create()
_ctx.write(JSON.stringify(request))
let res = _wasm.exports.transpileTraceQL(id)
let res = _wasm.exportsWrap.transpileTraceQL(id)
if (res !== 0) {
throw new WasmError(_ctx.read())
}
Expand Down Expand Up @@ -168,7 +183,7 @@ const pql = async (query, wasmCall, getData) => {
writer.writeBytes([data])
}
ctx.write(writer.buffer())
_wasm.exports.onDataLoad(reqId)
_wasm.exportsWrap.onDataLoad(reqId)
return ctx.read()
} finally {
ctx && ctx.destroy()
Expand All @@ -183,7 +198,7 @@ class Ctx {

create () {
try {
this.wasm.exports.createCtx(this.id)
this.wasm.exportsWrap.createCtx(this.id)
this.created = true
} catch (err) {
throw err
Expand All @@ -192,7 +207,7 @@ class Ctx {

destroy () {
try {
if (this.created) this.wasm.exports.dealloc(this.id)
if (this.created) this.wasm.exportsWrap.dealloc(this.id)
} catch (err) {
throw err
}
Expand All @@ -206,8 +221,8 @@ class Ctx {
if (typeof data === 'string') {
data = (new TextEncoder()).encode(data)
}
this.wasm.exports.alloc(this.id, data.length)
const ptr = this.wasm.exports.alloc(this.id, data.length)
this.wasm.exportsWrap.alloc(this.id, data.length)
const ptr = this.wasm.exportsWrap.alloc(this.id, data.length)
new Uint8Array(this.wasm.exports.memory.buffer).set(data, ptr)
}

Expand All @@ -216,8 +231,8 @@ class Ctx {
*/
read() {
const [resPtr, resLen] = [
this.wasm.exports.getCtxResponse(this.id),
this.wasm.exports.getCtxResponseLen(this.id)
this.wasm.exportsWrap.getCtxResponse(this.id),
this.wasm.exportsWrap.getCtxResponseLen(this.id)
]
return new TextDecoder().decode(new Uint8Array(this.wasm.exports.memory.buffer).subarray(resPtr, resPtr + resLen))
}
Expand Down
Binary file modified wasm_parts/main.wasm.gz
Binary file not shown.
Loading