Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lyra): Add WebAssembly support #194

Merged
merged 38 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
57dc9f5
feat(lyra): extract intersectTokenScores into the "lyra-utils-wasm" R…
jkomyno Nov 22, 2022
10a94dc
chore: add bash scripts to build wasm artifacts
jkomyno Nov 22, 2022
6cd1179
feat(lyra): replaced "intersectTokenScores" with its Wasm counterpart
jkomyno Nov 22, 2022
5129e66
chore: add experimental nix script to build Rust / Wasm
jkomyno Nov 22, 2022
e71b499
ci: add experimental wasm workflow
jkomyno Nov 22, 2022
ad7782d
chore: swapped scripts interpreter to "/bin/bash"
jkomyno Nov 22, 2022
8804f47
ci: attempt to fix "bad interpreter: no such file or directory " error
jkomyno Nov 22, 2022
5225e95
chore: removed nix
jkomyno Nov 24, 2022
f56d8c6
ci: replaced bash scripts with node.js scripts
jkomyno Nov 24, 2022
5fb6822
ci: added Dockerfile for Wasm generation pipeline
jkomyno Nov 24, 2022
eb99522
ci: replaced nix with Docker
jkomyno Nov 24, 2022
4dea2f9
chore: add examples of optimized wasm and textual wat representation
jkomyno Nov 24, 2022
b5688f2
chore: swapped order in test to match Rust's lexicographic order when…
jkomyno Nov 24, 2022
4e32323
chore: removed leftover from Dockerfile
jkomyno Nov 24, 2022
1becdca
ci: trigger wasm workflow on PRs
jkomyno Nov 24, 2022
d1f51a0
ci: fix typo
jkomyno Nov 24, 2022
1f0469d
ci: fix typo in "docker cp" command
jkomyno Nov 24, 2022
75fb53b
test: removes generated files from tests
micheleriva Nov 24, 2022
240fb39
build: wip: moves generated files to dist
micheleriva Nov 24, 2022
67519f3
build(wasm): wip on wasm bundling
micheleriva Nov 29, 2022
473891c
build(wasm): fixes wasm build system
micheleriva Dec 1, 2022
ea31125
build(wasm): fixes wasm build system
micheleriva Dec 1, 2022
856fe29
chore: removes rc from package version
micheleriva Dec 1, 2022
c564847
build: fixes build system via dockerfile
micheleriva Dec 3, 2022
52656bf
build: wip on build system
micheleriva Dec 3, 2022
bf46ead
build: removes post-install build
micheleriva Dec 3, 2022
397efcc
test(wasm): fix tests
micheleriva Dec 3, 2022
d21ae83
build(docker): fixes dockerfile
micheleriva Dec 3, 2022
5e03604
build(wasm): removes rust compilation process from bundling system
micheleriva Dec 3, 2022
0043cdc
Merge branch 'main' into feat/add-wasm
micheleriva Dec 4, 2022
7dfa3b5
build(wasm): wip on wasm build system
micheleriva Dec 6, 2022
9958fba
build(wasm): adds rust build command
micheleriva Dec 7, 2022
edd6fa7
fix: Fixed WASM loader.
ShogunPanda Dec 7, 2022
ebad3c9
feat: wip on build system
micheleriva Dec 7, 2022
b883437
Merge branch 'main' into feat/add-wasm
micheleriva Dec 7, 2022
8561bac
Merge branch 'main' into feat/add-wasm
micheleriva Dec 9, 2022
f09a459
Merge branch 'main' into feat/add-wasm
micheleriva Dec 14, 2022
341cd6a
feat(lyra): adds components
micheleriva Dec 14, 2022
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
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ get-imdb-dataset.mjs
*.vue
stemmer/**/*.js
stemmer/**/*.d.ts
*.dic
*.dic
src/wasm
rust/scripts
62 changes: 62 additions & 0 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: wasm

on:
push:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
build:
name: Compile Rust to WebAssembly with Docker
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to include OSX and Windows here


env:
DOCKER_BUILDKIT: 1
COMPOSE_DOCKER_CLI_BUILD: 1

runs-on: ${{ matrix.os }}
steps:
- name: Set git to use LF
run: |
git config --global core.autocrlf false
git config --global core.eol lf

- uses: actions/checkout@v3
with:
ref: ${{ github.event.inputs.enginesHash }}

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1

- name: Set up cache
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ matrix.os }}-buildx-${{ hashFiles('**/Dockerfile') }}

- name: Build Docker image
uses: docker/build-push-action@v2
with:
build-contexts: rust=rust
context: .
file: ./Dockerfile
builder: ${{ steps.buildx.outputs.name }}
load: true
tags: lyrasearch/lyra-wasm
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache

- name: Create Wasm artifacts
run: |
docker create --name tmp lyrasearch/lyra-wasm
docker cp tmp:/opt/app/src/wasm ./src/wasm
docker rm -f tmp

- name: Show Wasm artifacts
run: ls -l ./src/wasm
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,22 @@ $RECYCLE.BIN/
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/node,osx,windows,linux,intellij

### Rust ###
# Generated by Cargo
# will have compiled files and executables
debug/
target/
result/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
# Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

#!! ERROR: typescript is undefined. Use list command to see defined gitignore types !!#
94 changes: 94 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
########
# docker buildx build --load -f Dockerfile --build-context rust=rust . -t lyrasearch/lyra-wasm --progress plain
# docker create --name tmp lyrasearch/lyra-wasm
# docker cp tmp:/opt/app/src/wasm ./src/wasm
# docker rm -f tmp
########

ARG RUST_VERSION="1.65.0"
ARG ALPINE_VERSION="3.17.0"

#######################
#### Wasm tools builder
#######################
FROM alpine:${ALPINE_VERSION} as wasm-tools
WORKDIR /tmp

RUN apk update \
&& apk add --no-cache bash curl git
RUN git clone https://github.com/WebAssembly/binaryen.git wasm-tools

WORKDIR /tmp/wasm-tools

RUN apk add build-base cmake git python3 py3-pip clang ninja \
&& pip3 install -r requirements-dev.txt

RUN git submodule init \
&& git submodule update \
&& cmake . -G Ninja -DCMAKE_INSTALL_PREFIX=out/install -DCMAKE_CXX_FLAGS="-static" -DCMAKE_C_FLAGS="-static" -DCMAKE_BUILD_TYPE=Release -DBUILD_STATIC_LIB=ON -DCMAKE_INSTALL_PREFIX=install \
&& ninja install

RUN cp ./bin/wasm-opt /usr/bin/wasm-opt

########################
#### Rust + Wasm builder
########################
FROM rust:${RUST_VERSION}-slim as builder

ARG WASM_BINDGEN_VERSION="0.2.83"

# Rust compilation profile
ARG LYRA_WASM_PROFILE="release"

# Rust+Wasm compilation target (e.g. nodejs, deno, etc.)
ARG LYRA_WASM_TARGET="nodejs"

# When set to "1", attempt Wasm optimization via wasm-opt
ARG LYRA_WASM_OPT="1"

##
# Install Rust and Node.js tools
##
WORKDIR /opt/app/rust

# Install Node.js (used for running wasm-bindgen scripts only)
RUN apt-get update -y \
&& apt-get install -y curl libssl-dev pkg-config \
&& curl -sL https://deb.nodesource.com/setup_18.x | bash - \
&& apt-get install -y nodejs

# Add Wasm target support
RUN rustup target add wasm32-unknown-unknown
RUN cargo install -f wasm-bindgen-cli@${WASM_BINDGEN_VERSION}

# Import the Rust project
COPY --from=rust . ./

# Run unit tests (and cache dependencies)
RUN cargo test

##
# Build the Wasm artifacts running wasm-bindgen via Node.js scripts
##
WORKDIR /opt/app/rust/scripts

ENV LYRA_WASM_PROFILE ${LYRA_WASM_PROFILE}
ENV LYRA_WASM_TARGET ${LYRA_WASM_TARGET}
ENV LYRA_WASM_OPT ${LYRA_WASM_OPT}

# Pull in the wasm-opt optimizer compiled in a previous stage
COPY --from=wasm-tools /usr/bin/wasm-opt /usr/bin/wasm-opt

# Install Node.js dependencies
RUN npm i
WORKDIR /opt/app
RUN (cd rust && node ./scripts/wasmAll.mjs)

################
#### Wasm output
################
FROM alpine:${ALPINE_VERSION} as output

WORKDIR /opt/app/src/wasm

COPY --from=builder /opt/app/src/wasm ./
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lyrasearch/lyra",
"version": "0.3.1",
"version": "0.3.2-rc2",
"description": "Fast, in-memory, typo-tolerant, full-text search engine written in TypeScript",
"main": "./dist/cjs/src/lyra.js",
"module": "./dist/esm/src/lyra.js",
Expand All @@ -25,7 +25,9 @@
"build:cjs": "tsc --project tsconfig.cjs.json",
"build:module": "tsc --project tsconfig.esm.json",
"build:browser": "tsc --project tsconfig.browser.json",
"build": "rimraf ./dist && concurrently 'npm:build:module' 'npm:build:cjs' 'npm:build:browser'",
"build:wasm_bindings": "node ./rust/scripts/packArtifacts.mjs",
"build:all": "rimraf ./dist && concurrently 'npm:build:module' 'npm:build:cjs' 'npm:build:browser'",
"build": "npm run build:all && npm run build:wasm_bindings",
"test": "c8 -c tests/config/c8-local.json tap --rcfile=tests/config/tap.yml tests/*.test.ts",
"test:ci": "c8 -c tests/config/c8-ci.json tap --rcfile=tests/config/tap.yml --no-color tests/*.test.ts",
"test:smoke": "tap --rcfile=tests/config/tap.yml tests/smoke/*.test.ts",
Expand All @@ -47,6 +49,8 @@
}
},
"devDependencies": {
"@iarna/toml": "^2.2.5",
"@types/iarna__toml": "^2.0.2",
"@types/node": "^18.6.2",
"@types/tap": "^15.0.7",
"@typescript-eslint/eslint-plugin": "^5.31.0",
Expand All @@ -56,7 +60,9 @@
"commitizen": "^4.2.5",
"concurrently": "^7.3.0",
"cronometro": "^1.1.4",
"esbuild": "^0.15.15",
"eslint": "^8.20.0",
"execa": "^6.1.0",
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
"prettier": "^2.7.1",
Expand Down
Loading