npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sbffi2

v1.0.5

Published

Dynamic C function calls from JS, powered by dyncall.

Downloads

181

Readme

sbffi

A super-quick FFI for Node.js.

dyncall is used to make dynamic calls to native functions. In order to avoid some cost of translating JavaScript values into raw C types, a shared buffer is used for both arguments and return values. Writing values to a buffer turns out to be quite a bit faster than unpacking them in native code.

Usage

sbffi.getNativeFunction(pathToSharedLibrary, functionName, returnType, [argType1, argType2, ...])

All the arguments are strings. The types must be standard C types. See the Types section below for details. When functions take 64-bit types, the parameters must be passed as BigInts. 64-bit return values will also be BigInts.

// adder.c: some C library compiled to libadder.so

uint32_t add(uint32_t a, uint32_t b) {
  return a + b;
}
// index.js

const { getNativeFunction } = require('sbffi');

const libPath = '/path/to/libadder.so';
const add = getNativeFunction(libPath, 'add', 'uint32_t', ['uint32_t', 'uint32_t']);

const result = add(23, 34);
// 57

Types

The following types are supported:

  • (u)int[8|16|32|64]_t
  • bool
  • (unsigned) char
  • (unsigned) short
  • (unsigned) int
  • (unsigned) long
  • (unsigned) long long
  • float
  • double
  • size_t

128-bit types are not yet supported, and while this list may grow over time, for now other types can be used if they're aliases of the above types.

See the section below about pointers.

Pointers

Pointers are currently assumed to be 64-bit, and can be passed to native functions by specifying the type as pointer or referring to any other type with an asterisk in the string, for example: uint8_t *.

You can put raw data into a Buffer, and then get a pointer to the start of that buffer with:

const bufferPointer = sbffi.getBufferPointer(buffer);

Arrays and strings must be passed as pointers.

Callbacks

You can use two different styles of callbacks with sbffi.

  • Simple callbacks are simply passed into the function as normal. They must be called exactly once by the underlying native function.

  • Advanced callbacks must be wrapped with createCallback, and .destroy() must be called on them when the underlying native function will no longer call it. There's a slight performance advantage in using advanced callbacks and re-using them, since simple callbacks create a Napi::ThreadsafeFunction per invocation. In addition, APIs that may call the same callback multiple times may be used with advanced callbacks, but not with simple callbacks.

In either case, to specify a simple callback, identify it in the arguments array passed to getNatveFunction() as [cbReturnType, [cbArgTyp1, cbArgType2, ...]].

For advanced callbacks, after specifying them in the native function signature, you can initialize them as follows:

function myCb (result) { /* ... */ }
const advancedCallback = createCallback(myCb, [cbReturnType, [cbArgTyp1, cbArgType2, ...]]);

You can then pass advancedCallback to a native function that takes in a callbacks with that signature, just as you would any other callback. When the callback is no longer needed, you can call advancedCallback.destroy(). Failure to call .destroy() will keep the Node.js process alive.

Structs

For now, sbfffi doesn't have any built-in support for structs. That being said, there are some helpful libraries like shared-structs and ref-napi (and its family of modules). As long as you can build up a C struct into a Buffer, you can pass pointers to them into C functions. Non-pointer struct arguments or return values are not supported.

Development

Using a non-release version of sbffi requires that cmake is installed in order to compile the native addon.

Benchmarks

A simple benchmark can be run with npm run bench. This will test calling a simple adding function from the test library using the following techniques:

  • ffi-napi: A successor to node-ffi compatible with modern versions of Node.js.
  • sbffi: This library.
  • napi-addon: A very simple/normal Node.js addon using NAPI in C.
  • napi-addon-sb: A NAPI addon using the same shared-buffer technique as sbffi, but with a hard-coded function call, rather than a dynamic/FFI call.
  • wasm: The adding function compiled to WebAssembly.
  • js: Re-implementing the function in plain JavaScript.

Each function will be called 100000 times, in 5 repetitions, timed with console.time(). Here are the results on my machine (AMD Ryzen 9 5900X) with Ubuntu 22.04.1 and Node.js 19.6.0:

ffi-napi ... done!
sbffi ... done!
napi-addon ... done!
napi-addon-sb ... done!
wasm ... done!
js ... done!
---
ffi-napi ... done!
sbffi ... done!
napi-addon ... done!
napi-addon-sb ... done!
wasm ... done!
js ... done!
---
ffi-napi ... done!
sbffi ... done!
napi-addon ... done!
napi-addon-sb ... done!
wasm ... done!
js ... done!
---
ffi-napi ... done!
sbffi ... done!
napi-addon ... done!
napi-addon-sb ... done!
wasm ... done!
js ... done!
---
ffi-napi ... done!
sbffi ... done!
napi-addon ... done!
napi-addon-sb ... done!
wasm ... done!
js ... done!
---
┌───────────────┬──────┬───────────┬────────────┬────────────────────┐
│    (index)    │ min  │    max    │    mean    │       stddev       │
├───────────────┼──────┼───────────┼────────────┼────────────────────┤
│   ffi-napi    │ 3216 │ 122159103 │ 8271.61785 │ 291769.95704110194 │
│     sbffi     │ 120  │  3657727  │ 177.070514 │ 9480.134200345783  │
│  napi-addon   │ 120  │  2877439  │ 181.689724 │ 9197.256781039703  │
│ napi-addon-sb │  80  │  3256319  │ 143.028966 │ 10193.011672112762 │
│     wasm      │  60  │  3317759  │ 170.443644 │ 16172.981925863021 │
│      js       │  70  │  3395583  │ 141.67375  │ 12967.590912612988 │
└───────────────┴──────┴───────────┴────────────┴────────────────────┘

For this benchmark, I generally see roughly similar performance between sbffi and a typical NAPI addon. Of course, YMMV.

Contributing

Please see CONTRIBUTING.md, CODE_OF_CONDUCT.md and TODO.md.

License

Please see LICENSE.txt.