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

buffer-tie

v0.0.2

Published

Low-level memory mapping tricks to tie ArrayBuffer contents together

Downloads

4

Readme

Zero-copy marshalling of data into WebAssembly

This module uses dirty tricks to remap external data into WebAssembly modules without copying.

let tie = require('buffer-tie')
let src = tie.openMemFDBuffer('my data', 1<<20);
// A Memory with just one 64kB page.
let dst = new WebAssembly.Memory({initial:1,maximum:1})

// Zero-copy alias of the first 32 kB of src into the last 32 kB of dst.
src.tie(dst, 32*1024, 32*1024);

Motivation

Sometimes when you embed WebAssembly in a C++ environment, there can be a large amount of external data that you don't want to copy into the WebAssembly module.

With simple JavaScript we don't have this a problem currently because web engines can allow their ArrayBuffer instances to use memory allocated external to the JS engine. But for an already-instantiated WebAssembly module, all the data has to be in the WebAssembly module's linear memory.

Horrible solution

On Linux, one page in physical memory can have many addresses in virtual memory. This is a feature of virtual memory in hardware. Unfortunately the full capabilities of the virtual memory system aren't exported to userspace by Linux; you need a file descriptor for the source memory before you can alias it. So in this module we create array buffers that are backed by memfd, and actually we never unmap the corresponding memory. In some production system you would do this differently :)

Then to actually alias the memory into the target, we stomple on V8's memory by remapping our data into the WebAssembly memory. Because we know that WebAssembly memories are allocated on page-aligned boundaries, a page-aligned offset inside the array buffer will be page-aligned when treated as an absolute address too. You can choose the offset from the "outside", or the WebAssembly program could allocate page-aligned memory using posix_memalign, and then call out to an import to stomple it.

MREMAP_DONTUNMAP?

There is another possible implementation, to use MREMAP_DONTUNMAP for anonymous memory, but in that case it requires you to know that the memory is anonymous, and apparently mremap is quite slow.