Skip to content

Commit

Permalink
perf: use custom sqlite library on unix (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy authored Sep 13, 2022
1 parent 9952e04 commit f405cb3
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 14 deletions.
33 changes: 30 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
deno-version: 'v1.x'

- name: Check Formatting
run: deno fmt --check
run: deno fmt --check --ignore=sqlite

lint:
runs-on: ubuntu-latest
Expand All @@ -33,7 +33,7 @@ jobs:
deno-version: 'v1.x'

- name: Lint
run: deno lint
run: deno lint --ignore=sqlite

test:
strategy:
Expand All @@ -45,11 +45,38 @@ jobs:
steps:
- name: Checkout repo
uses: actions/checkout@v2
with:
submodules: true

- name: Install Deno
uses: denoland/setup-deno@main
with:
deno-version: 'v1.x'

- name: Build sqlite3 (unix)
if: matrix.os != 'windows-latest'
run: |
mkdir -p build
make
- name: Set DENO_SQLITE_PATH (linux)
if: matrix.os == 'ubuntu-latest'
run: echo "DENO_SQLITE_PATH=$(pwd)/build/libsqlite3.so" >> $GITHUB_ENV

- name: Set DENO_SQLITE_PATH (macos)
if: matrix.os == 'macos-latest'
run: echo "DENO_SQLITE_PATH=$(pwd)/build/libsqlite3.dylib" >> $GITHUB_ENV

- name: Run Tests
run: deno task test
run: deno task test

- name: Release
uses: softprops/action-gh-release@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: "release draft"
draft: true
files: |
bin/libsqlite3.so
bin/libsqlite3.dylib
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
package-lock.json
*.sqlite
node_modules/
build/
# C benchmark binary
bench/bench
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "sqlite"]
path = sqlite
url = https://github.com/sqlite/sqlite
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
OS := $(shell uname -s)
ifeq ($(OS),Darwin)
EXT = .dylib
else
EXT = .so
endif

build/libsqlite3.dylib:
mkdir -p build
cd sqlite && mkdir -p build && cd build && ../configure && make
cp sqlite/build/.libs/libsqlite3$(EXT) build/libsqlite3$(EXT)

clean:
rm -rf build/
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,32 @@ Check out the complete API reference
## Native Library

By default, this module will look for existing SQLite3 dynamic library on your
path, which is `sqlite3.dll` on Windows, `libsqlite3.so` on Linux, and
`libsqlite3.dylib` on macOS. If the library you want to use is not on path, then
you can use the `DENO_SQLITE_PATH` environment variable. You will have to
install SQLite3 separately if it's not already installed, since it is not
bundled with this module.
path, which is `sqlite3.dll` on Windows.

On Linux and macOS, this module will download and cache a prebuilt shared
library from Github releases.

If the library you want to use is not on path, then you can use the
`DENO_SQLITE_PATH` environment variable. You will have to install SQLite3
separately if it's not already installed, since it is not bundled with this
module.

## Contributing

On Linux and macOS, you need to build sqlite3 from source. Make sure that you
have the submodule (`git submodule update --init --recursive`).

```sh
mkdir -p build/
make
```

When running tests and benchmarks, you need to use the `DENO_SQLITE_PATH` env
variable otherwise it won't use to local compiled shared library.

```sh
DENO_SQLITE_PATH=build/libsqlite3.dylib deno task bench
```

## Related

Expand Down
1 change: 1 addition & 0 deletions sqlite
Submodule sqlite added at 3e4e69
26 changes: 20 additions & 6 deletions src/ffi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { prepare } from "https://deno.land/x/[email protected]/plug.ts";

const symbols = {
sqlite3_open_v2: {
parameters: [
Expand Down Expand Up @@ -422,12 +424,24 @@ const symbols = {
let lib: Deno.DynamicLibrary<typeof symbols>["symbols"];

try {
const filename = Deno.env.get("DENO_SQLITE_PATH") || {
windows: "sqlite3",
darwin: "libsqlite3.dylib",
linux: "libsqlite3.so",
}[Deno.build.os];
lib = Deno.dlopen(filename, symbols).symbols;
const customPath = Deno.env.get("DENO_SQLITE_PATH");
// TODO(@littledivy): Ship prebuilt shared library on Windows.
if (customPath || Deno.build.os === "windows") {
lib = Deno.dlopen(customPath || "sqlite3", symbols).symbols;
} else {
const url =
"https://github.com/denodrivers/sqlite3/releases/download/v0.5.3/";
lib = (await prepare({
name: "sqlite3",
urls: {
darwin: {
aarch64: url + "libsqlite3_aarch64.dylib",
x86_64: url + "libsqlite3.dylib",
},
linux: url + "libsqlite3.so",
},
}, symbols)).symbols;
}
} catch (e) {
if (e instanceof Deno.errors.PermissionDenied) {
throw e;
Expand Down

0 comments on commit f405cb3

Please sign in to comment.