[feat] prep for cffi

Signed-off-by: Anton Nesterov <anton@demiurg.io>
This commit is contained in:
Anton Nesterov 2024-08-27 16:35:17 +02:00
parent ab5de2ae51
commit c0c9b7c9b4
No known key found for this signature in database
GPG key ID: 59121E8AE2851FB5
5 changed files with 68 additions and 14 deletions

5
binding/makefile Normal file
View file

@ -0,0 +1,5 @@
build-lib:
CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build -o ../lib/libdal-arm64.dylib -buildmode=c-shared ./dal.go
for arch in amd64 arm64; do\
CC=x86_64-unknown-linux-gnu-gcc CGO_ENABLED=1 GOOS=linux GOARCH=$${arch} go build -o ../lib/libdal-$${arch}.so -buildmode=c-shared ./dal.go;\
done

View file

@ -1,6 +1,8 @@
const fs = require("fs"); const fs = require("fs");
const dal = require("../../build/Release/dal.node"); const dal = require("../../build/Release/dal.node");
const Mb = (num) => Math.round(num / 1024 / 1024);
class Stats { class Stats {
constructor() { constructor() {
this.calls = 0; this.calls = 0;
@ -26,37 +28,42 @@ class Stats {
print() { print() {
console.log(` console.log(`
AVERAGE: AVERAGE:
rss: ${mb(this.avg_rss)} Mb rss: ${Mb(this.avg_rss)} Mb
external: ${mb(this.avg_external)} Mb external: ${Mb(this.avg_external)} Mb
buffers: ${mb(this.avg_heapUsed)} Mb buffers: ${Mb(this.avg_heapUsed)} Mb
total: ${mb(this.avg_heapTotal)} Mb`); total: ${Mb(this.avg_heapTotal)} Mb`);
} }
} }
const stats = new Stats(); const stats = new Stats();
let prevMem = process.memoryUsage(); let prevMem = process.memoryUsage();
stats.add(prevMem); stats.add(prevMem);
const mb = (num) => Math.round(num / 1024 / 1024);
function MEM(when = "") { function MEM(when = "") {
const mem = process.memoryUsage(); const mem = process.memoryUsage();
stats.add(mem); stats.add(mem);
console.log(` console.log(`
${when} ${when}
rss: ${mb(mem.rss)} Mb [delta> ${mem.rss - prevMem.rss}] rss: ${Mb(mem.rss)} Mb [delta> ${mem.rss - prevMem.rss}]
external: ${mb(mem.external)} Mb [delta> ${mem.external - prevMem.external}] external: ${Mb(mem.external)} Mb [delta> ${mem.external - prevMem.external}]
buffers: ${mb(mem.heapUsed)} Mb [delta> ${mem.heapUsed - prevMem.heapUsed}] buffers: ${Mb(mem.heapUsed)} Mb [delta> ${mem.heapUsed - prevMem.heapUsed}]
total: ${mb(mem.heapTotal)} Mb [delta> ${mem.heapTotal - prevMem.heapTotal}]`); total: ${Mb(mem.heapTotal)} Mb [delta> ${mem.heapTotal - prevMem.heapTotal}]`);
} }
console.time("Time to end"); console.time("Time to end");
MEM("START"); MEM("START");
const buf = fs.readFileSync("./pkg/__test__/proto_test.msgpack"); const buf = fs.readFileSync("./pkg/__test__/proto_test.msgpack");
const iter = dal.RowIterator(buf); const Iterator = dal.RowIterator(buf);
MEM("AFTER INIT"); MEM("AFTER INIT");
let dataTransferedBytes = 0; let dataTransferedBytes = 0;
for (let i = 0; i < 100000000; i++) { for (let i = 0; i < 100000000; i++) {
const b = iter.next(); const b = Iterator.next();
if (b.length === 0) { if (b.length === 0) {
break; break;
} }
@ -68,11 +75,12 @@ for (let i = 0; i < 100000000; i++) {
MEM("AFTER ITERATION"); MEM("AFTER ITERATION");
iter.free(); Iterator.free();
MEM("AFTER CLEANUP"); MEM("AFTER CLEANUP");
console.log("\nData transfered: ", mb(dataTransferedBytes), "Mb"); console.log("\nData transfered: ", Mb(dataTransferedBytes), "Mb");
console.timeEnd("Time to end"); console.timeEnd("Time to end");
setTimeout(() => { setTimeout(() => {
MEM("AFTER SOME TIME"); MEM("AFTER SOME TIME");
stats.avg(); stats.avg();

41
dal/bun.ffi.ts Normal file
View file

@ -0,0 +1,41 @@
import { dlopen, FFIType, suffix, ptr } from "bun:ffi";
import { join } from "node:path";
import fs from "node:fs";
// TODO: build a shared library compatible with cffi
const libname = `libdal-arm64.${suffix}`;
const libpath = join(__dirname, '..', 'lib', libname);
const {
symbols: {
InitSQLite,
CreateRowIterator,
NextRow,
GetLen,
FreeIter,
},
} = dlopen(
libpath,
{
InitSQLite: {
args: [ FFIType.cstring ],
returns: FFIType.void,
},
CreateRowIterator: {
args: [ FFIType.cstring ],
returns: FFIType.i32,
},
NextRow: {
args: [FFIType.i32],
returns: FFIType.cstring,
},
GetLen: {
args: [FFIType.i32],
returns: FFIType.i32,
},
FreeIter : {
args: [FFIType.i32],
returns: FFIType.void,
},
},
);

View file

@ -1,5 +1,5 @@
{ {
"files": ["dal/index.ts", "dal/Binding.ts"], "files": ["dal/index.ts", "dal/Binding.ts", "dal/node.napi.ts"],
"compilerOptions": { "compilerOptions": {
"target": "ESNext", "target": "ESNext",
"module": "ESNext", "module": "ESNext",