Last active
September 2, 2024 03:53
-
-
Save ritalin/d155254280eb6673c1b39baf4469913e to your computer and use it in GitHub Desktop.
CacheAPIを使ったWASM版Duckdbのキャッシュ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as duckdb from '@duckdb/duckdb-wasm' | |
import { AsyncDuckDB, type DuckDBBundle } from '@duckdb/duckdb-wasm' | |
// 11 sec @duckdb/duckdb-wasmをバンドルに含めたケース | |
// 7 sec @duckdb/duckdb-wasmをhttps://cdn.jsdelivr.netから取得するようにしたケース | |
// 6 sec @duckdb/duckdb-wasmをhttps://cdn.jsdelivr.netから取得するようにしたケース+CacheAPIによるwasmとworkerのっキャッシュ化 | |
// wasm 30MB | |
// worker 0.7MB | |
// parquet 2.5MB | |
// version: vite/5.2.10 darwin-x64 node-v22.1.0 | |
export const initAsyncDb = async (): Promise<AsyncDuckDB> => { | |
const JSDELIVR_BUNDLES = duckdb.getJsDelivrBundles(); | |
const bundle = await duckdb.selectBundle(JSDELIVR_BUNDLES); | |
console.log(bundle.mainModule) | |
console.log(">>> begin fetch wasm") | |
const c = await caches.open("v2") | |
let res2 = await c.match(bundle.mainModule) | |
if (res2 === undefined) { | |
console.log(">>> wasm cache missed") | |
const res = await fetch(bundle.mainModule) | |
await c.put(bundle.mainModule, res.clone()) | |
res2 = res | |
} | |
else { | |
console.log(">>> wasm cache hit") | |
} | |
const buf_wasm = await res2.blob() | |
console.log(`>>> end fetch wasm ${buf_wasm.size}`) | |
const wasm_url = URL.createObjectURL(buf_wasm) | |
console.log(bundle.mainWorker) | |
console.log(">>> begin fetch worker") | |
let res3 = await c.match(bundle.mainWorker!) | |
if (res3 === undefined) { | |
console.log(">>> worker cache missed") | |
const res = await fetch(bundle.mainWorker!) | |
await c.put(bundle.mainWorker!, res.clone()) | |
res3 = res | |
} | |
else { | |
console.log(">>> worker cache hit") | |
} | |
const buf_worker = await res3.blob() | |
console.log(`>>> end fetch worker ${buf_worker.size}`) | |
const worker_url = URL.createObjectURL(buf_worker); | |
const worker = new Worker(worker_url!) | |
const logger = import.meta.env.PROD ? new duckdb.VoidLogger(): new duckdb.ConsoleLogger() | |
console.log(">>> begin instanciate") | |
const instance = new AsyncDuckDB(logger, worker) | |
await instance.instantiate(wasm_url, bundle.pthreadWorker) | |
console.log(">>> end instanciate") | |
URL.revokeObjectURL(url_worker) | |
URL.revokeObjectURL(url_wasm) | |
return instance | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment