Skip to content

Commit 9d5d9cd

Browse files
authored
Merge pull request #474 from metrico/micro_gc
Micro gc#2
2 parents c2640d1 + 2a0cce8 commit 9d5d9cd

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

wasm_parts/main.js

+27-12
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,32 @@ const getWasm = (() => {
1515
let wasm = null
1616
let cnt = 0
1717
let run = false
18+
let int
1819
async function init () {
1920
go = new _Go();
2021
run = true
2122
const _wasm = await WebAssembly.instantiate(
2223
gunzipSync(fs.readFileSync(WASM_URL)), go.importObject)
23-
setInterval(() => {
24+
int && clearInterval(int)
25+
int = setInterval(() => {
2426
console.log(`WASM SIZE: ${Math.floor(wasm.exports.memory.buffer.byteLength / 1024 / 1024)} MB`)
2527
}, 5000)
2628
go.run(_wasm.instance)
2729
wasm = _wasm.instance
2830
wasm.exports.setMaxSamples(process.env.ADVANCED_PROMETHEUS_MAX_SAMPLES || 5000000)
31+
wasm.exportsWrap = Object.fromEntries(
32+
Object.entries(wasm.exports).map(([_k, _v]) => {
33+
return [_k, (...args) => {
34+
const _wasm = wasm
35+
try {
36+
return _wasm.exports[_k].bind(_wasm)(...args)
37+
} catch (e) {
38+
_wasm === wasm && init()
39+
throw e
40+
}
41+
}]
42+
})
43+
)
2944
cnt = 0
3045
run = false
3146
}
@@ -56,7 +71,7 @@ module.exports.pqlRangeQuery = async (query, startMs, endMs, stepMs, getData) =>
5671
const end = endMs || Date.now()
5772
const step = stepMs || 15000
5873
return await pql(query,
59-
(ctx) => _wasm.exports.pqlRangeQuery(ctx.id, start, end, step),
74+
(ctx) => _wasm.exportsWrap.pqlRangeQuery(ctx.id, start, end, step),
6075
(matchers) => getData(matchers, start, end))
6176
}
6277

@@ -71,7 +86,7 @@ module.exports.pqlInstantQuery = async (query, timeMs, getData) => {
7186
const time = timeMs || Date.now()
7287
const _wasm = getWasm()
7388
return await pql(query,
74-
(ctx) => _wasm.exports.pqlInstantQuery(ctx.id, time),
89+
(ctx) => _wasm.exportsWrap.pqlInstantQuery(ctx.id, time),
7590
(matchers) => getData(matchers, time - 300000, time))
7691
}
7792

@@ -82,7 +97,7 @@ module.exports.pqlMatchers = (query) => {
8297
ctx.create()
8398
try {
8499
ctx.write(query)
85-
const res1 = _wasm.exports.pqlSeries(id)
100+
const res1 = _wasm.exportsWrap.pqlSeries(id)
86101
if (res1 !== 0) {
87102
throw new WasmError(ctx.read())
88103
}
@@ -124,7 +139,7 @@ module.exports.TranspileTraceQL = (request) => {
124139
_ctx = new Ctx(id, _wasm)
125140
_ctx.create()
126141
_ctx.write(JSON.stringify(request))
127-
let res = _wasm.exports.transpileTraceQL(id)
142+
let res = _wasm.exportsWrap.transpileTraceQL(id)
128143
if (res !== 0) {
129144
throw new WasmError(_ctx.read())
130145
}
@@ -168,7 +183,7 @@ const pql = async (query, wasmCall, getData) => {
168183
writer.writeBytes([data])
169184
}
170185
ctx.write(writer.buffer())
171-
_wasm.exports.onDataLoad(reqId)
186+
_wasm.exportsWrap.onDataLoad(reqId)
172187
return ctx.read()
173188
} finally {
174189
ctx && ctx.destroy()
@@ -183,7 +198,7 @@ class Ctx {
183198

184199
create () {
185200
try {
186-
this.wasm.exports.createCtx(this.id)
201+
this.wasm.exportsWrap.createCtx(this.id)
187202
this.created = true
188203
} catch (err) {
189204
throw err
@@ -192,7 +207,7 @@ class Ctx {
192207

193208
destroy () {
194209
try {
195-
if (this.created) this.wasm.exports.dealloc(this.id)
210+
if (this.created) this.wasm.exportsWrap.dealloc(this.id)
196211
} catch (err) {
197212
throw err
198213
}
@@ -206,8 +221,8 @@ class Ctx {
206221
if (typeof data === 'string') {
207222
data = (new TextEncoder()).encode(data)
208223
}
209-
this.wasm.exports.alloc(this.id, data.length)
210-
const ptr = this.wasm.exports.alloc(this.id, data.length)
224+
this.wasm.exportsWrap.alloc(this.id, data.length)
225+
const ptr = this.wasm.exportsWrap.alloc(this.id, data.length)
211226
new Uint8Array(this.wasm.exports.memory.buffer).set(data, ptr)
212227
}
213228

@@ -216,8 +231,8 @@ class Ctx {
216231
*/
217232
read() {
218233
const [resPtr, resLen] = [
219-
this.wasm.exports.getCtxResponse(this.id),
220-
this.wasm.exports.getCtxResponseLen(this.id)
234+
this.wasm.exportsWrap.getCtxResponse(this.id),
235+
this.wasm.exportsWrap.getCtxResponseLen(this.id)
221236
]
222237
return new TextDecoder().decode(new Uint8Array(this.wasm.exports.memory.buffer).subarray(resPtr, resPtr + resLen))
223238
}

wasm_parts/main.wasm.gz

295 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)