Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.git
node_modules
build
prebuilds
tests/data
tools/w2v-to-json
135 changes: 135 additions & 0 deletions .github/workflows/prebuild.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Builds the N-API prebuilds and tests them on every supported Node version.
#
# - linux-x64 / linux-arm64: built inside public.ecr.aws/lambda/nodejs:22
# (the bot-api Lambda runtime) via the Dockerfile, then checked against the
# Lambda glibc / libstdc++ symbol versions.
# - darwin-arm64 / darwin-x64: built with prebuildify on macOS runners.
#
# The `prebuilds` artifact of a green run holds prebuilds/<platform>-<arch>/
# for all four targets; commit it before publishing. Nothing is published here.
#
# DRAFT: written alongside the 5.0.0 port, never run yet.

name: prebuild

on:
workflow_dispatch:
pull_request:
push:
tags: ['v*']

permissions:
contents: read

# 4.0.1 NAN linux-x64 prebuild, used as the reference by tests/equivalence-test.js.
env:
REFERENCE_COMMIT: dd317b0

jobs:
build-linux:
strategy:
fail-fast: false
matrix:
include:
- arch: x64
platform: linux/amd64
runner: ubuntu-24.04
- arch: arm64
platform: linux/arm64
runner: ubuntu-24.04-arm
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
# Builds in the Lambda image and runs the smoke tests there (see Dockerfile).
- name: Build prebuild in the Lambda nodejs:22 image
run: >
docker buildx build --platform ${{ matrix.platform }} --target prebuilds
--output type=local,dest=prebuilds .
- name: Check glibc / libstdc++ symbol versions
run: ./scripts/check-prebuild-symbols.sh
- uses: actions/upload-artifact@v4
with:
name: prebuild-linux-${{ matrix.arch }}
path: prebuilds/
if-no-files-found: error

build-darwin:
strategy:
fail-fast: false
matrix:
include:
- arch: arm64
runner: macos-15
- arch: x64
runner: macos-15-intel
runs-on: ${{ matrix.runner }}
env:
MACOSX_DEPLOYMENT_TARGET: '11.0'
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- run: npm ci --ignore-scripts
- run: npx prebuildify --napi --strip
- name: Check architecture
run: file prebuilds/darwin-${{ matrix.arch }}/*.node
- uses: actions/upload-artifact@v4
with:
name: prebuild-darwin-${{ matrix.arch }}
path: prebuilds/
if-no-files-found: error

test:
needs: [build-linux, build-darwin]
strategy:
fail-fast: false
matrix:
runner: [ubuntu-24.04, ubuntu-24.04-arm, macos-15, macos-15-intel]
node: [22, 24, 26]
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # the equivalence test reads the 4.0.1 prebuild from history
# Node 22 first, only to keep its path for the equivalence test: the 4.0.1
# NAN reference only loads on Node 22 (ABI 127).
- uses: actions/setup-node@v4
with:
node-version: 22
- run: echo "REFERENCE_NODE=$(command -v node)" >> "$GITHUB_ENV"
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: node --version && npm ci --ignore-scripts
- uses: actions/download-artifact@v4
with:
pattern: prebuild-*
path: prebuilds
merge-multiple: true
- name: Make sure the prebuild (not a local compile) is what gets loaded
run: |
rm -rf build
node -e "const p = require('node-gyp-build').path('.'); console.log(p); if (!p.includes('/prebuilds/')) process.exit(1)"
- run: make test
- name: Equivalence with the 4.0.1 NAN binding (linux-x64 only)
if: matrix.runner == 'ubuntu-24.04'
run: |
git show "$REFERENCE_COMMIT:prebuilds/linux-x64/@calldesk+annoy-node.node" > "$RUNNER_TEMP/annoy-4.0.1.node"
make equivalence-test REFERENCE_BINDING="$RUNNER_TEMP/annoy-4.0.1.node" REFERENCE_NODE="$REFERENCE_NODE"

collect:
needs: test
runs-on: ubuntu-24.04
steps:
- uses: actions/download-artifact@v4
with:
pattern: prebuild-*
path: prebuilds
merge-multiple: true
- run: find prebuilds -type f -exec ls -l {} +
- uses: actions/upload-artifact@v4
with:
name: prebuilds
path: prebuilds/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ tests/data/*.db
tools/w2v-to-json
build
.npmignore
tools
47 changes: 30 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
FROM public.ecr.aws/lambda/nodejs:22

ENV LAMBDA_ENV=true
# Builds the Linux N-API prebuild inside the AWS Lambda nodejs:22 image
# (Amazon Linux 2023: glibc 2.34, GCC 11), so the binary only needs symbols
# the Lambda runtime provides. One build per architecture:
#
# docker buildx build --platform linux/amd64 --target prebuilds --output type=local,dest=prebuilds .
# docker buildx build --platform linux/arm64 --target prebuilds --output type=local,dest=prebuilds .
#
# (npm run prebuild:linux runs both, then scripts/check-prebuild-symbols.sh.)
# The result is prebuilds/linux-<arch>/@calldesk+annoy-node.napi.node, which
# node-gyp-build loads on any Node version with N-API >= 8.

FROM public.ecr.aws/lambda/nodejs:22 AS build

ENTRYPOINT []

WORKDIR /build

# Install build tools (Python required by node-gyp)
RUN dnf install -y gcc gcc-c++ make python3 && \
# node-gyp needs Python, make and a C++ compiler; prebuildify --strip needs binutils.
RUN dnf install -y gcc gcc-c++ make python3 binutils && \
dnf clean all

# Copy files
COPY package*.json ./
COPY . .

# Install dependencies without running install script (no prebuilds yet)
COPY package.json package-lock.json ./
RUN npm ci --ignore-scripts

# Compile using local Node 22 headers, then place in prebuilds directory
RUN npx node-gyp rebuild && \
mkdir -p prebuilds/linux-x64 && \
cp build/Release/addon.node prebuilds/linux-x64/@calldesk+annoy-node.node

CMD ["echo", "Linux prebuild created"]
COPY binding.gyp addon.cc annoyindexwrapper.cc annoyindexwrapper.h annoylib.h kissrandom.h index.js ./
RUN npx prebuildify --napi --strip && \
rm -rf build

# Smoke-test the prebuild (loaded through index.js / node-gyp-build, build/
# removed above) in the runtime it targets.
COPY tests ./tests
RUN mkdir -p tests/data
RUN node tests/smalltest.js && \
node tests/smalltest-manhattan.js && \
node tests/wrong-input-test.js && \
node tests/undefined-behaviour-test.js

FROM scratch AS prebuilds
COPY --from=build /build/prebuilds/ /
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,30 @@ TESTDATADIR = tests/data
build-wrapper:
node-gyp rebuild

.PHONY: test big-test equivalence-test check-prebuilds

test: tests/data/text8-vector.json
node tests/smalltest.js
node tests/worker-thread-test.js
node tests/smalltest-manhattan.js
node tests/basictests.js basic-config.js
node tests/wrong-input-test.js
node tests/typed-array-test.js
node tests/undefined-behaviour-test.js
node --expose-gc tests/leak-test.js
node tests/external-memory-test.js
npm run test:types

# Compares the binding under test with the 4.0.1 NAN prebuild (Node 22 only):
# make equivalence-test REFERENCE_BINDING=/path/to/4.0.1.node REFERENCE_NODE=/path/to/node22
REFERENCE_NODE ?= $(shell command -v node)
equivalence-test:
@test -n "$(REFERENCE_BINDING)" || (echo "set REFERENCE_BINDING=<4.0.1 addon.node>" && exit 1)
ANNOY_REFERENCE_BINDING="$(REFERENCE_BINDING)" ANNOY_REFERENCE_NODE="$(REFERENCE_NODE)" node tests/equivalence-test.js

# Fails if a prebuild needs a glibc / libstdc++ newer than the Lambda nodejs:22 image.
check-prebuilds:
./scripts/check-prebuild-symbols.sh

big-test: tests/data/GoogleNews-vectors-negative300.json
node tests/basictests.js very-big-config.js
Expand Down
47 changes: 35 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ annoy-node

Node bindings for [Annoy](https://github.com/spotify/annoy), an efficient Approximate Nearest Neighbors implementation written in C++.

**Version 4.0.1** (@calldesk fork): upgraded `nan` to `^2.27.0` for Node 22+ compatibility. Ships prebuilts for `darwin-arm64` and `linux-x64` targeting Node 22 — no build tools required on install.
**Version 5.0.0** (@calldesk fork): the binding is written against [N-API](https://nodejs.org/api/n-api.html) through [node-addon-api](https://github.com/nodejs/node-addon-api) (`NAPI_VERSION=10`, the latest stable Node-API) instead of NAN. One binary per platform loads on every Node.js release with Node-API 10 (Node >= 22.14; tested on 22, 24 and 26). The package only serves bot-api, which follows the AWS Lambda Node.js runtime, so it targets the current Node-API rather than older Node releases; nothing is compiled on install where a prebuild exists. JS API, results and the on-disk index format are unchanged from 4.0.1 (see "Compatibility with 4.x" below), with two additions: numeric typed arrays are accepted as vectors, and index memory is reported to V8.

Status: Tests pass, including one that loads 3 million vectors, but API coverage is not complete. Run on OS X and Linux with Node 8, 10, and 12. Not tried on Windows yet. Support for Node 6.3 and 4.6 ended at version 2.0.1 of this package.

Expand All @@ -30,17 +30,34 @@ There are a few minor differences in behavior:
Installation
------------

npm install annoy
npm install @calldesk/annoy-node

This package ships prebuilts for `darwin-arm64` and `linux-x64` (Node 22). No compiler or Python required on these platforms.
Prebuilds ship for:

To rebuild prebuilts after a Node version change:
| Platform | Built in | Notes |
|---|---|---|
| `linux-x64` | `public.ecr.aws/lambda/nodejs:22` (amd64) | glibc >= 2.14 |
| `linux-arm64` | `public.ecr.aws/lambda/nodejs:22` (arm64) | glibc >= 2.17 |
| `darwin-arm64`, `darwin-x64` | GitHub Actions macOS runners | built by `.github/workflows/prebuild.yml` (its `prebuilds` artifact) |

# darwin-arm64 (run on Apple Silicon, Node 22)
npm run build:prebuilt
Elsewhere (Windows, musl/Alpine, other architectures) `npm install` falls back to compiling with node-gyp, which needs Python and a C++ toolchain.

# linux-x64 (via Docker, matches Lambda environment)
npm run docker:build
### How prebuilds are built

The Linux prebuilds are compiled inside the AWS Lambda `nodejs:22` image (Amazon Linux 2023, glibc 2.34) so they never need a newer glibc or libstdc++ than the Lambda runtime. Never commit a `.node` file compiled on a workstation: a recent distribution links against symbols the Lambda does not have (e.g. `sqrtf@GLIBC_2.43`).

npm run prebuild:linux # linux-x64 + linux-arm64 via Docker buildx (arm64 under QEMU), then the symbol check
npm run check:prebuilds # fails if a Linux prebuild needs > GLIBC_2.34 or > GLIBCXX_3.4.33

Both Docker builds run the smoke tests inside the Lambda image. The darwin prebuilds (and all four, with tests on Node 22/24/26) are built by the `prebuild` GitHub Actions workflow: download its `prebuilds` artifact into `prebuilds/` before publishing.

### Compatibility with 4.x

- Same class, methods, argument coercion, defaults, return values and errors; `tests/wrong-input-test.js` passes against both the 4.0.1 NAN binary and 5.0.0.
- Bit-identical results and `.annoy` files: `tests/equivalence-test.js` compares 5.0.0 with the 4.0.1 prebuild (`make equivalence-test REFERENCE_BINDING=... REFERENCE_NODE=<node 22>`). Indexes saved by either version load in the other.
- **Behaviour change: typed arrays are accepted as vectors.** `addItem` and `getNNsByVector` take a `Float32Array` (copied directly, no per-component conversion) or any other numeric typed array, with the same results as the equivalent `Array` (`tests/typed-array-test.js`). 4.0.1 ignored them: `addItem` added nothing and `getNNsByVector` returned `undefined`. BigInt arrays are still ignored. On 5000 vectors of 200 dimensions, `addItem` with `Float32Array` takes 2.6 ms instead of 46.6 ms with arrays, and `getNNsByVector` saves ~8 µs per query.
- **Index memory is reported to V8** (`napi_adjust_external_memory`): the heap nodes of a built index or the mapped file of a loaded one. The GC now collects indexes that are no longer referenced; with 4.0.1 a process loading a fresh index per request kept every dropped mapping until an unrelated GC (`tests/external-memory-test.js`: 1000 loads of a 4 MB index keep ~20 alive instead of ~360).
- Inputs that were undefined behaviour in 4.0.1 are now defined (`tests/undefined-behaviour-test.js`): asking for more neighbours than available returns only the real results (4.0.1 padded with garbage), querying an index without trees returns empty arrays (4.0.1 crashed), short vectors are zero-padded, and exceptions thrown while coercing arguments propagate to JS (4.0.1 aborted the process).

Usage
-----
Expand Down Expand Up @@ -79,10 +96,10 @@ Usage
Development
------------

npm install -g node-gyp
node-gyp rebuild
npm ci
npx node-gyp rebuild # local build in build/Release, preferred over prebuilds by node-gyp-build

Run `eslint .` and `make prettier` before committing.
Remove `build/` to test the prebuilds again. Run `eslint .` and `make prettier` before committing.

Tests
-----
Expand All @@ -92,7 +109,13 @@ Run tests with `make test`.
You can also run tests individually:

- This is a short baseline test: `node tests/smalltest.js`
- This is a test that uses 70K 200-dimension vectors: `node tests/basictests.js`
- This is a test that uses 70K 200-dimension vectors: `node tests/basictests.js basic-config.js`
- TypeScript declarations (`index.d.ts`, 5.0.0): `npm run test:types` compiles `tests/types.ts` under `--strict`; misuse (unknown metric, non-numeric vector, wrong result type) must fail to type-check
- Typed array vectors (5.0.0): `node tests/typed-array-test.js`
- External memory reporting (5.0.0): `node tests/external-memory-test.js`
- API contract and wrong input, identical to 4.0.1: `node tests/wrong-input-test.js` (set `ANNOY_BINDING=<file.node>` to run it against another binary)
- Native memory is released: `node --expose-gc tests/leak-test.js`
- Equivalence with 4.0.1: `make equivalence-test REFERENCE_BINDING=<4.0.1 linux-x64 .node> REFERENCE_NODE=<node 22 binary>` (the 4.0.1 file is in git history at commit `dd317b0`)

There is also a `big-test` target that is not a dependency of the `test` target. It loads about 3 million 300-dimension vectors. It takes about six minutes to run on good-for-2016 hardware. Before you can run it, you need to download [GoogleNews-vectors-negative300.bin](https://drive.google.com/file/d/0B7XkCwpI5KDYNlNUTTlSS21pQmM/edit?usp=sharing) to `tests/data`.

Expand Down
12 changes: 6 additions & 6 deletions addon.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <nan.h>
#include <napi.h>
#include "annoyindexwrapper.h"

void InitAll(v8::Local<v8::Object> exports) {
AnnoyIndexWrapper::Init(exports);
// Context-aware by construction (NODE_API_MODULE): the addon keeps no
// per-process JS state, so it can be loaded in several worker threads.
Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
return AnnoyIndexWrapper::Init(env, exports);
}

NODE_MODULE_INIT() {
InitAll(exports);
}
NODE_API_MODULE(addon, InitAll)
Loading
Loading