From 871b949c6ee3d51ecc0e54bad2734b12d46ebd8c Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Fri, 26 Jan 2024 08:53:59 +0100 Subject: [PATCH 01/19] Migrate to cargo zigbuild --- .github/workflows/build.yml | 10 ++++--- Makefile | 54 +++++++++++-------------------------- README.md | 8 ++++++ zigar | 4 --- zigcc | 26 ------------------ 5 files changed, 31 insertions(+), 71 deletions(-) delete mode 100755 zigar delete mode 100755 zigcc diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6121185f4e..74a40b5a98 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,7 +13,11 @@ on: type: string release: required: false +<<<<<<< HEAD type: string +======= + type: boolean +>>>>>>> 05ddd44 (Migrate to cargo zigbuild) jobs: build: @@ -56,6 +60,9 @@ jobs: uses: dtolnay/rust-toolchain@v1 with: toolchain: nightly + - name: Install cargo-zigbuild + run: | + cargo install cargo-zigbuild || true - name: Cargo cache uses: actions/cache@v4 continue-on-error: false @@ -79,9 +86,6 @@ jobs: CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUNNER: qemu-aarch64 run: | sudo apt-get install -y \ - libc6-arm64-cross \ - libc6-dev-arm64-cross \ - crossbuild-essential-arm64 \ qemu-system-arm \ qemu-efi-aarch64 \ qemu-utils \ diff --git a/Makefile b/Makefile index 3120df0f98..91358b5a2b 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,18 @@ -TARGET_linux_x86_64 = x86_64-unknown-linux-gnu -TARGET_linux_arm64 = aarch64-unknown-linux-gnu +TARGET_clib = gnu +TARGET_linux_x86_64 = x86_64-unknown-linux-$(TARGET_clib) +TARGET_linux_arm64 = aarch64-unknown-linux-$(TARGET_clib) TARGET_darwin_x86_64 = x86_64-apple-darwin TARGET_darwin_arm64 = aarch64-apple-darwin RUST_VERSION = nightly TOOLCHAIN = +$(RUST_VERSION) +ZIGBUILD_ARG = $(TOOLCHAIN) zigbuild -r BUILD_ARG = $(TOOLCHAIN) build -r BUILD_DIR = ./target/release BUNDLE_DIR = bundle -ZSTD_LIB_ARGS = -j lib-nomt CC="$(CURDIR)/zigcc -s -O3 -flto" AR="zig ar" UNAME=Linux ZSTD_LIB_COMPRESSION=0 ZSTD_LIB_DICTBUILDER=0 +ZSTD_LIB_ARGS = -j lib-nomt UNAME=Linux ZSTD_LIB_COMPRESSION=0 ZSTD_LIB_DICTBUILDER=0 AR="zig ar" +ZSTD_LIB_CC_ARGS = -s -O3 -flto +ZSTD_LIB_CC_arm64 = CC="zig cc -target aarch64-linux-musl $(ZSTD_LIB_CC_ARGS)" +ZSTD_LIB_CC_x64 = CC="zig cc -target aarch64-linux-musl $(ZSTD_LIB_CC_ARGS)" TS_SOURCES = $(wildcard src/js/*.ts) $(wildcard src/js/@llrt/*.ts) $(wildcard tests/*.ts) STD_JS_FILE = $(BUNDLE_DIR)/@llrt/std.js @@ -27,29 +32,9 @@ else ARCH := $(shell uname -m) endif -ifeq ($(DETECTED_OS),darwin) - export AR = $(CURDIR)/zigar - export CC_aarch64_unknown_linux_gnu = $(CURDIR)/zigcc - export CCX_aarch64_unknown_linux_gnu = $(CURDIR)/zigcc - export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = $(CURDIR)/zigcc - export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS = -Ctarget-feature=+lse -Ctarget-cpu=neoverse-n1 - - export CC_x86_64_unknown_linux_gnu = $(CURDIR)/zigcc - export CXX_x86_64_unknown_linux_gnu = $(CURDIR)/zigcc - export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = $(CURDIR)/zigcc -else ifeq ($(DETECTED_OS),linux) - export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER = x86_64-linux-gnu-gcc - - export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS = -Clink-arg=-Wl,--allow-multiple-definition -Ctarget-feature=+lse -Ctarget-cpu=neoverse-n1 - export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = aarch64-linux-gnu-gcc - - RUNFLAGS = RUSTFLAGS="-Clink-arg=-Wl,--allow-multiple-definition -Ctarget-feature=+crt-static" -endif CURRENT_TARGET ?= $(TARGET_$(DETECTED_OS)_$(ARCH)) -export COMPILE_TARGET = $(CURRENT_TARGET) - lambda-all: libs $(RELEASE_ZIPS) release-all: | lambda-all llrt-linux-x64.zip llrt-linux-arm64.zip llrt-darwin-x64.zip llrt-darwin-arm64.zip release: llrt-$(DETECTED_OS)-$(ARCH).zip @@ -65,16 +50,16 @@ llrt-darwin-arm64.zip: | clean-js js zip -j $@ target/$(TARGET_darwin_arm64)/release/llrt llrt-linux-x64.zip: | clean-js js - cargo $(BUILD_ARG) --target $(TARGET_linux_x86_64) + cargo $(ZIGBUILD_ARG) --target $(TARGET_linux_x86_64) zip -j $@ target/$(TARGET_linux_x86_64)/release/llrt llrt-linux-arm64.zip: | clean-js js - cargo $(BUILD_ARG) --target $(TARGET_linux_arm64) + cargo $(ZIGBUILD_ARG) --target $(TARGET_linux_arm64) zip -j $@ target/$(TARGET_linux_arm64)/release/llrt define release_template -release-${1}: js - COMPILE_TARGET=$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})) cargo $$(BUILD_ARG) --target $$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})) --features lambda -vv +release-${1}: | clean-js js + cargo $$(ZIGBUILD_ARG) --target $$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})) --features lambda -vv ./pack target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/llrt target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap @rm -rf llrt-lambda-${1}.zip zip -j llrt-lambda-${1}.zip target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap @@ -132,13 +117,6 @@ run: export _HANDLER = index.handler run: | clean-js js cargo run -r -vv -run-release: export _HANDLER = fixtures/local.handler -run-release: js - cargo build - time target/release/llrt - time target/release/llrt - time target/release/llrt - run-ssr: export AWS_LAMBDA_RUNTIME_API = localhost:3000 run-ssr: export TABLE_NAME=quickjs-table run-ssr: export AWS_REGION = us-east-1 @@ -161,8 +139,8 @@ test: js test-ci: export JS_MINIFY = 0 test-ci: clean-js | toolchain js - $(RUNFLAGS) cargo $(TOOLCHAIN) -Z panic-abort-tests test --target $(CURRENT_TARGET) - $(RUNFLAGS) cargo $(TOOLCHAIN) run -r --target $(CURRENT_TARGET) -- test -d bundle + cargo $(TOOLCHAIN) -Z panic-abort-tests test --target $(CURRENT_TARGET) + cargo $(TOOLCHAIN) run -r --target $(CURRENT_TARGET) -- test -d bundle libs-arm64: lib/arm64/libzstd.a lib/zstd.h libs-x64: lib/x64/libzstd.a lib/zstd.h @@ -175,13 +153,13 @@ lib/zstd.h: lib/arm64/libzstd.a: mkdir -p $(dir $@) rm -f zstd/lib/-.o - cd zstd/lib && make clean && COMPILE_TARGET="aarch64-unknown-linux-musl" make $(ZSTD_LIB_ARGS) + cd zstd/lib && make clean && make $(ZSTD_LIB_ARGS) $(ZSTD_LIB_CC_arm64) cp zstd/lib/libzstd.a $@ lib/x64/libzstd.a: mkdir -p $(dir $@) rm -f zstd/lib/-.o - cd zstd/lib && make clean && COMPILE_TARGET="x86_64-unknown-linux-musl" make $(ZSTD_LIB_ARGS) + cd zstd/lib && make clean && make $(ZSTD_LIB_ARGS) $(ZSTD_LIB_CC_x64) cp zstd/lib/libzstd.a $@ bench: diff --git a/README.md b/README.md index 9fdf83768e..a8bfe66e0d 100644 --- a/README.md +++ b/README.md @@ -189,11 +189,19 @@ Install dependencies brew install zig make zstd node corepack # Ubuntu +<<<<<<< HEAD sudo apt -y install make zstd gcc \ libc6-arm64-cross \ libc6-dev-arm64-cross \ crossbuild-essential-arm64 +======= + sudo apt -y install make zstd +>>>>>>> 05ddd44 (Migrate to cargo zigbuild) sudo snap install zig --classic --beta + +Install cargo zigbuild + + cargo install cargo-zigbuild Clone code and cd to directory diff --git a/zigar b/zigar deleted file mode 100755 index 4a5011d578..0000000000 --- a/zigar +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -set -e - -zig ar "$@" \ No newline at end of file diff --git a/zigcc b/zigcc deleted file mode 100755 index 7b274676b1..0000000000 --- a/zigcc +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -set -e - -if [ -z ${COMPILE_TARGET+x} ]; then - COMPILE_TARGET="${CARGO_CFG_TARGET_ARCH}-${CARGO_CFG_TARGET_VENDOR}-${CARGO_CFG_TARGET_OS}-${CARGO_CFG_TARGET_ENV}" -fi - -CC_TARGET="" - -if [[ $COMPILE_TARGET == "x86_64-unknown-linux-gnu" ]]; then - CC_TARGET="x86_64-linux-gnu" -elif [[ $COMPILE_TARGET == "aarch64-unknown-linux-gnu" ]]; then - CC_TARGET="aarch64-linux-gnu" -elif [[ $COMPILE_TARGET == "x86_64-unknown-linux-musl" ]]; then - CC_TARGET="x86_64-linux-musl" -elif [[ $COMPILE_TARGET == "aarch64-unknown-linux-musl" ]]; then - CC_TARGET="aarch64-linux-musl" -fi - -new_array=() -for value in "$@" -do - [[ $value != *self-contained/*crt* ]] && new_array+=($value) -done - -zig cc -target $CC_TARGET "${new_array[@]}" \ No newline at end of file From 207251bbf76837d066bbe0e9660dccdbcd46ff0b Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Fri, 26 Jan 2024 09:28:33 +0100 Subject: [PATCH 02/19] Update makefile --- .github/workflows/build.yml | 15 +++------------ Makefile | 10 ++++++++++ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 74a40b5a98..c8d381be1f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,11 +13,7 @@ on: type: string release: required: false -<<<<<<< HEAD type: string -======= - type: boolean ->>>>>>> 05ddd44 (Migrate to cargo zigbuild) jobs: build: @@ -92,21 +88,16 @@ jobs: qemu-user make CURRENT_TARGET=aarch64-unknown-linux-gnu test-ci + - name: Build Linux binaries - name: Build Linux binaries if: inputs.release && inputs.platform == 'linux' run: | - make libs-${{ inputs.release }} - make llrt-linux-${{ inputs.release }}.zip - make release-${{ inputs.release }} - - name: Build Darwin binaries - if: inputs.release && inputs.platform == 'darwin' - run: | - make llrt-darwin-${{ inputs.release }}.zip + make release-lambda - name: Upload artifacts if: inputs.release uses: actions/upload-artifact@v4 with: - name: artifacts-${{ inputs.platform }}-${{ inputs.arch }} + name: artifacts-${{ matrix.platform }} path: | *.zip - name: Upload changelog diff --git a/Makefile b/Makefile index 91358b5a2b..4e08fc4b79 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ ZSTD_LIB_ARGS = -j lib-nomt UNAME=Linux ZSTD_LIB_COMPRESSION=0 ZSTD_LIB_DICTBUIL ZSTD_LIB_CC_ARGS = -s -O3 -flto ZSTD_LIB_CC_arm64 = CC="zig cc -target aarch64-linux-musl $(ZSTD_LIB_CC_ARGS)" ZSTD_LIB_CC_x64 = CC="zig cc -target aarch64-linux-musl $(ZSTD_LIB_CC_ARGS)" +CARGO_CMD = cargo TS_SOURCES = $(wildcard src/js/*.ts) $(wildcard src/js/@llrt/*.ts) $(wildcard tests/*.ts) STD_JS_FILE = $(BUNDLE_DIR)/@llrt/std.js @@ -32,6 +33,10 @@ else ARCH := $(shell uname -m) endif +ifeq ($(DETECTED_OS),darwin) + CARGO_CMD = cargo-zigbuild +endif + CURRENT_TARGET ?= $(TARGET_$(DETECTED_OS)_$(ARCH)) @@ -139,8 +144,13 @@ test: js test-ci: export JS_MINIFY = 0 test-ci: clean-js | toolchain js +<<<<<<< HEAD cargo $(TOOLCHAIN) -Z panic-abort-tests test --target $(CURRENT_TARGET) cargo $(TOOLCHAIN) run -r --target $(CURRENT_TARGET) -- test -d bundle +======= + $(CARGO_CMD) $(TOOLCHAIN) -Z panic-abort-tests test --target $(CURRENT_TARGET) + $(CARGO_CMD) $(TOOLCHAIN) run -r --target $(CURRENT_TARGET) -- test -d bundle +>>>>>>> 58ce148 (Update makefile) libs-arm64: lib/arm64/libzstd.a lib/zstd.h libs-x64: lib/x64/libzstd.a lib/zstd.h From eb922cc708c1ccb51bac1df85545e6637c66d2c9 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Fri, 26 Jan 2024 09:32:29 +0100 Subject: [PATCH 03/19] Restore cache before install --- .github/workflows/build.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c8d381be1f..c894be29fd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -56,9 +56,6 @@ jobs: uses: dtolnay/rust-toolchain@v1 with: toolchain: nightly - - name: Install cargo-zigbuild - run: | - cargo install cargo-zigbuild || true - name: Cargo cache uses: actions/cache@v4 continue-on-error: false @@ -71,6 +68,9 @@ jobs: target/ key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} restore-keys: ${{ runner.os }}-cargo- + - name: Install cargo-zigbuild + run: | + cargo install cargo-zigbuild || true - name: Run tests if: inputs.arch != 'aarch64' || inputs.platform != 'linux' run: | From 725504dfc9b33598e21b7e1020d45658e450ced1 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Fri, 26 Jan 2024 09:33:37 +0100 Subject: [PATCH 04/19] Change test command --- Makefile | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 4e08fc4b79..726ac6ebda 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ -TARGET_clib = gnu -TARGET_linux_x86_64 = x86_64-unknown-linux-$(TARGET_clib) -TARGET_linux_arm64 = aarch64-unknown-linux-$(TARGET_clib) +TARGET_linux_x86_64 = x86_64-unknown-linux-gnu +TARGET_linux_arm64 = aarch64-unknown-linux-gnu TARGET_darwin_x86_64 = x86_64-apple-darwin TARGET_darwin_arm64 = aarch64-apple-darwin RUST_VERSION = nightly @@ -13,7 +12,7 @@ ZSTD_LIB_ARGS = -j lib-nomt UNAME=Linux ZSTD_LIB_COMPRESSION=0 ZSTD_LIB_DICTBUIL ZSTD_LIB_CC_ARGS = -s -O3 -flto ZSTD_LIB_CC_arm64 = CC="zig cc -target aarch64-linux-musl $(ZSTD_LIB_CC_ARGS)" ZSTD_LIB_CC_x64 = CC="zig cc -target aarch64-linux-musl $(ZSTD_LIB_CC_ARGS)" -CARGO_CMD = cargo +CARGO_TEST_CMD = cargo-zigbuild TS_SOURCES = $(wildcard src/js/*.ts) $(wildcard src/js/@llrt/*.ts) $(wildcard tests/*.ts) STD_JS_FILE = $(BUNDLE_DIR)/@llrt/std.js @@ -34,7 +33,7 @@ else endif ifeq ($(DETECTED_OS),darwin) - CARGO_CMD = cargo-zigbuild + CARGO_TEST_CMD = cargo endif @@ -144,13 +143,9 @@ test: js test-ci: export JS_MINIFY = 0 test-ci: clean-js | toolchain js -<<<<<<< HEAD - cargo $(TOOLCHAIN) -Z panic-abort-tests test --target $(CURRENT_TARGET) - cargo $(TOOLCHAIN) run -r --target $(CURRENT_TARGET) -- test -d bundle -======= - $(CARGO_CMD) $(TOOLCHAIN) -Z panic-abort-tests test --target $(CURRENT_TARGET) - $(CARGO_CMD) $(TOOLCHAIN) run -r --target $(CURRENT_TARGET) -- test -d bundle ->>>>>>> 58ce148 (Update makefile) + $(CARGO_TEST_CMD) $(TOOLCHAIN) -Z panic-abort-tests test --target $(CURRENT_TARGET) + $(CARGO_TEST_CMD) $(TOOLCHAIN) run -r --target $(CURRENT_TARGET) -- test -d bundle + libs-arm64: lib/arm64/libzstd.a lib/zstd.h libs-x64: lib/x64/libzstd.a lib/zstd.h From d0d16b9ac614f6345839e750bc1c3363ec34499b Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Fri, 26 Jan 2024 09:52:57 +0100 Subject: [PATCH 05/19] Update CC env --- .cargo/config.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.cargo/config.toml b/.cargo/config.toml index b148603255..4aedaf1800 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,7 @@ [unstable] build-std = ["core", "compiler_builtins", "alloc", "std", "panic_abort"] build-std-features = ["panic_immediate_abort"] + +[env] +CC_aarch64_unknown_linux_gnu = "zig cc -target aarch64-linux-gnu" +CC_x86_64_unknown_linux_gnu = "zig cc -target x86_64-linux-gnu" From 0804ade9cb6aad308c17512d9d9b70a47a850a07 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Fri, 26 Jan 2024 14:18:08 +0100 Subject: [PATCH 06/19] Change glibc version --- .cargo/config.toml | 4 ---- Makefile | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 4aedaf1800..b148603255 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,7 +1,3 @@ [unstable] build-std = ["core", "compiler_builtins", "alloc", "std", "panic_abort"] build-std-features = ["panic_immediate_abort"] - -[env] -CC_aarch64_unknown_linux_gnu = "zig cc -target aarch64-linux-gnu" -CC_x86_64_unknown_linux_gnu = "zig cc -target x86_64-linux-gnu" diff --git a/Makefile b/Makefile index 726ac6ebda..530063e08c 100644 --- a/Makefile +++ b/Makefile @@ -63,7 +63,7 @@ llrt-linux-arm64.zip: | clean-js js define release_template release-${1}: | clean-js js - cargo $$(ZIGBUILD_ARG) --target $$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})) --features lambda -vv + cargo $$(ZIGBUILD_ARG) --target $$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})).2.26 --features lambda -vv ./pack target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/llrt target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap @rm -rf llrt-lambda-${1}.zip zip -j llrt-lambda-${1}.zip target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap From aac4770d6adb2cc4f951c69ead47bc1d27facbfe Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Fri, 26 Jan 2024 14:28:48 +0100 Subject: [PATCH 07/19] Update build --- .github/workflows/build.yml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c894be29fd..0cb0675795 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -88,16 +88,21 @@ jobs: qemu-user make CURRENT_TARGET=aarch64-unknown-linux-gnu test-ci - - name: Build Linux binaries - name: Build Linux binaries if: inputs.release && inputs.platform == 'linux' run: | - make release-lambda + make libs-${{ inputs.release }} + make llrt-linux-${{ inputs.release }}.zip + make release-${{ inputs.release }} + - name: Build Darwin binaries + if: inputs.release && inputs.platform == 'darwin' + run: | + make llrt-darwin-${{ inputs.release }}.zip - name: Upload artifacts if: inputs.release uses: actions/upload-artifact@v4 with: - name: artifacts-${{ matrix.platform }} + name: artifacts-${{ inputs.platform }}-${{ inputs.arch }} path: | *.zip - name: Upload changelog From 5c5944e7a2d91a8698527aca3d8084024b513eaa Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 29 Jan 2024 14:56:28 +0100 Subject: [PATCH 08/19] Use mimalloc allocator & fix gc --- .cargo/config.toml | 3 +++ Cargo.lock | 11 +++++++++++ Cargo.toml | 3 +++ Makefile | 8 ++++---- src/allocator.rs | 44 +++++++++++++++++++++++++++++++++++++++++ src/js/@llrt/runtime.ts | 1 + src/main.c | 21 +++++++++++++++++++- src/main.rs | 5 +++++ src/vm.rs | 31 ++++++++++++++++++++++++++++- 9 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 src/allocator.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index b148603255..ed78d4adcf 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,6 @@ [unstable] build-std = ["core", "compiler_builtins", "alloc", "std", "panic_abort"] build-std-features = ["panic_immediate_abort"] + +[target.aarch64-unknown-linux-musl] +rustflags = ["-Ctarget-feature=+lse,+crt-static", "-Ctarget-cpu=neoverse-n1"] diff --git a/Cargo.lock b/Cargo.lock index eaa9e37c33..d80fd8ffee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -739,6 +739,16 @@ version = "0.2.152" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" +[[package]] +name = "libmimalloc-sys" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3979b5c37ece694f1f5e51e7ecc871fdb0f517ed04ee45f88d15d6d553cb9664" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "llrt" version = "0.1.4-beta" @@ -758,6 +768,7 @@ dependencies = [ "itoa", "jwalk", "libc", + "libmimalloc-sys", "md-5", "nanoid", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index 5648162583..0c40849b69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ md-5 = { version = "0.10.6", features = ["asm"] } rquickjs = { git = "https://github.com/DelSkayn/rquickjs", rev = "60696e88dfb903d8f5cd81b2667fb3f64f9e9f67", features = [ "full-async", "parallel", + "rust-alloc", ], default-features = false } tokio = { version = "1", features = ["full"] } tracing = { version = "0.1.40", features = ["log"] } @@ -66,10 +67,12 @@ tokio-rustls = { version = "0.25.0", features = [ "ring", ], default-features = false } ring = "0.17.7" +libmimalloc-sys = "0.1.35" [build-dependencies] rquickjs = { git = "https://github.com/DelSkayn/rquickjs", rev = "60696e88dfb903d8f5cd81b2667fb3f64f9e9f67", features = [ "full-async", + "rust-alloc", ], default-features = false } relative-path = "1.9.0" tokio = { version = "1", features = ["full"] } diff --git a/Makefile b/Makefile index 530063e08c..3ff37df0f4 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -TARGET_linux_x86_64 = x86_64-unknown-linux-gnu -TARGET_linux_arm64 = aarch64-unknown-linux-gnu +TARGET_linux_x86_64 = x86_64-unknown-linux-musl +TARGET_linux_arm64 = aarch64-unknown-linux-musl TARGET_darwin_x86_64 = x86_64-apple-darwin TARGET_darwin_arm64 = aarch64-apple-darwin RUST_VERSION = nightly @@ -63,10 +63,10 @@ llrt-linux-arm64.zip: | clean-js js define release_template release-${1}: | clean-js js - cargo $$(ZIGBUILD_ARG) --target $$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})).2.26 --features lambda -vv + cargo $$(ZIGBUILD_ARG) --target $$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})) --features lambda -vv ./pack target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/llrt target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap @rm -rf llrt-lambda-${1}.zip - zip -j llrt-lambda-${1}.zip target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap + zip -j llrt-lambda-${1}.zip target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap index.mjs llrt-lambda-${1}: release-${1} endef diff --git a/src/allocator.rs b/src/allocator.rs new file mode 100644 index 0000000000..b3b41f1f69 --- /dev/null +++ b/src/allocator.rs @@ -0,0 +1,44 @@ +use libmimalloc_sys as ffi; + +use core::alloc::{GlobalAlloc, Layout}; +use core::ffi::c_void; +use core::sync::atomic::AtomicUsize; +use ffi::*; +use std::sync::atomic::Ordering; + +pub static USED_MEM: AtomicUsize = AtomicUsize::new(0); + +pub struct TrackingMiMalloc; + +unsafe impl GlobalAlloc for TrackingMiMalloc { + #[inline] + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + let size = layout.size(); + USED_MEM.fetch_add(size, Ordering::Relaxed); + mi_malloc_aligned(size, layout.align()) as *mut u8 + } + + #[inline] + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { + let size = layout.size(); + USED_MEM.fetch_add(size, Ordering::Relaxed); + mi_zalloc_aligned(size, layout.align()) as *mut u8 + } + + #[inline] + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + USED_MEM.fetch_sub(layout.size(), Ordering::Relaxed); + mi_free(ptr as *mut c_void); + } + + #[inline] + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { + let size = layout.size(); + if new_size > size { + USED_MEM.fetch_add(new_size - size, Ordering::Relaxed); + } else { + USED_MEM.fetch_sub(size - new_size, Ordering::Relaxed); + } + mi_realloc_aligned(ptr as *mut c_void, new_size, layout.align()) as *mut u8 + } +} diff --git a/src/js/@llrt/runtime.ts b/src/js/@llrt/runtime.ts index 9856e0ed4e..1ef892a95b 100644 --- a/src/js/@llrt/runtime.ts +++ b/src/js/@llrt/runtime.ts @@ -198,6 +198,7 @@ const startProcessEvents = async ( } iterations++; } + global.__gc(); } }; diff --git a/src/main.c b/src/main.c index 1437eb8f6f..81cb1b2628 100644 --- a/src/main.c +++ b/src/main.c @@ -298,8 +298,27 @@ int main(int argc, char *argv[]) char startTimeStr[16]; sprintf(startTimeStr, "%lu", startTime); + char *memorySizeStr = getenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE"); + int memorySize = memorySizeStr ? atoi(memorySizeStr) : 128; + double memoryFactor = 0.8; + if (memorySize > 512) + { + memoryFactor = 0.9; + } + if (memorySize > 1024) + { + memoryFactor = 0.92; + } + if (memorySize > 2048) + { + memoryFactor = 0.95; + } + + char mimallocReserveMemoryMb[16]; + sprintf(mimallocReserveMemoryMb, "%iMiB", (int)(memorySize * memoryFactor)); + setenv("_START_TIME", startTimeStr, false); - setenv("MIMALLOC_RESERVE_OS_MEMORY", "120m", false); + setenv("MIMALLOC_RESERVE_OS_MEMORY", mimallocReserveMemoryMb, false); setenv("MIMALLOC_LIMIT_OS_ALLOC", "1", false); logInfo("Starting app\n"); diff --git a/src/main.rs b/src/main.rs index 7e68b1099c..d18d7a35af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ #[macro_use] mod macros; +mod allocator; mod buffer; mod bytearray_buffer; mod child_process; @@ -32,6 +33,7 @@ mod uuid; mod vm; mod xml; +use allocator::TrackingMiMalloc; use minimal_tracer::MinimalTracer; use rquickjs::{AsyncContext, Module}; use std::{ @@ -57,6 +59,9 @@ use crate::{ const VERSION: &str = env!("CARGO_PKG_VERSION"); +#[global_allocator] +pub static ALLOCATOR: TrackingMiMalloc = TrackingMiMalloc; + #[tokio::main] async fn main() -> Result<(), Box> { MinimalTracer::register()?; diff --git a/src/vm.rs b/src/vm.rs index b57426323c..a6bcd1bfe6 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -8,7 +8,7 @@ use std::{ path::{Component, Path, PathBuf}, process::exit, result::Result as StdResult, - sync::{Arc, Mutex}, + sync::{atomic::Ordering, Arc, Mutex}, }; use once_cell::sync::Lazy; @@ -33,6 +33,7 @@ use zstd::{bulk::Decompressor, dict::DecoderDictionary}; include!("./bytecode_cache.rs"); use crate::{ + allocator::USED_MEM, buffer::BufferModule, child_process::ChildProcessModule, console, @@ -94,6 +95,14 @@ create_modules!( "uuid" => UuidModule ); +static GC_THRESHOLD: Lazy = Lazy::new(|| { + let gc_threshold_mb: usize = env::var("LLRT_GC_THRESHOLD_MB") + .unwrap_or("20".into()) + .parse() + .unwrap_or(20); + gc_threshold_mb * 1024 * 1024 +}); + struct ModuleInfo { name: &'static str, module: T, @@ -344,6 +353,9 @@ impl Vm { .fill(&mut [0; 8]) .expect("Failed to initialize SystemRandom"); + //inited lazy + *GC_THRESHOLD > 0; + let mut file_resolver = FileResolver::default(); let mut binary_resolver = BinaryResolver::default(); let mut paths: Vec<&str> = Vec::with_capacity(10); @@ -386,6 +398,8 @@ impl Vm { let ctx = AsyncContext::full(&runtime).await?; ctx.with(|ctx| { + ctx.globals().set("__gc", Func::from(run_gc))?; + crate::console::init(&ctx)?; crate::encoding::init(&ctx)?; crate::http::init(&ctx)?; @@ -467,6 +481,21 @@ fn json_parse_string<'js>(ctx: Ctx<'js>, value: Value<'js>) -> Result json_parse(&ctx, bytes) } +fn run_gc(ctx: Ctx<'_>) { + println!("ALLOCATED: {:?}", USED_MEM.load(Ordering::Relaxed)); + + if USED_MEM.load(Ordering::Relaxed) < *GC_THRESHOLD { + return; + } + + trace!("Running GC"); + + unsafe { + let rt = qjs::JS_GetRuntime(ctx.as_raw().as_ptr()); + qjs::JS_RunGC(rt); + }; +} + fn init(ctx: &Ctx<'_>, module_names: HashSet<&'static str>) -> Result<()> { let globals = ctx.globals(); From 9b44dd117200acdc8efe295357f51ca7b81a0bb9 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 29 Jan 2024 15:28:44 +0100 Subject: [PATCH 09/19] Fix warning --- src/vm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm.rs b/src/vm.rs index a6bcd1bfe6..f6a94452b2 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -354,7 +354,7 @@ impl Vm { .expect("Failed to initialize SystemRandom"); //inited lazy - *GC_THRESHOLD > 0; + let _ = *GC_THRESHOLD > 0; let mut file_resolver = FileResolver::default(); let mut binary_resolver = BinaryResolver::default(); From 12dbe1f168367777887f8a37d53fd01503d1cf49 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 29 Jan 2024 15:31:30 +0100 Subject: [PATCH 10/19] Fix target --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0cb0675795..eb3f3195d5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -87,7 +87,7 @@ jobs: qemu-utils \ qemu-user - make CURRENT_TARGET=aarch64-unknown-linux-gnu test-ci + make CURRENT_TARGET=aarch64-unknown-linux-musl test-ci - name: Build Linux binaries if: inputs.release && inputs.platform == 'linux' run: | From 8c3bce25f4b29c1eb4622a66038b2e05fccd3b5f Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 29 Jan 2024 15:49:30 +0100 Subject: [PATCH 11/19] Disable cache --- .github/workflows/build.yml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index eb3f3195d5..7e5d6b01e1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -56,18 +56,18 @@ jobs: uses: dtolnay/rust-toolchain@v1 with: toolchain: nightly - - name: Cargo cache - uses: actions/cache@v4 - continue-on-error: false - with: - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - target/ - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - restore-keys: ${{ runner.os }}-cargo- + # - name: Cargo cache + # uses: actions/cache@v4 + # continue-on-error: false + # with: + # path: | + # ~/.cargo/bin/ + # ~/.cargo/registry/index/ + # ~/.cargo/registry/cache/ + # ~/.cargo/git/db/ + # target/ + # key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + # restore-keys: ${{ runner.os }}-cargo- - name: Install cargo-zigbuild run: | cargo install cargo-zigbuild || true From e230c651e14e33985c8acdc92a87604e6d64d28b Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 29 Jan 2024 16:01:30 +0100 Subject: [PATCH 12/19] Tmp disable rustflags --- .cargo/config.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index ed78d4adcf..89595512af 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -2,5 +2,5 @@ build-std = ["core", "compiler_builtins", "alloc", "std", "panic_abort"] build-std-features = ["panic_immediate_abort"] -[target.aarch64-unknown-linux-musl] -rustflags = ["-Ctarget-feature=+lse,+crt-static", "-Ctarget-cpu=neoverse-n1"] +# [target.aarch64-unknown-linux-musl] +# rustflags = ["-Ctarget-feature=+lse,+crt-static", "-Ctarget-cpu=neoverse-n1"] From e87a7ae08a0e2c15280699764148bb353864755a Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 29 Jan 2024 19:48:02 +0100 Subject: [PATCH 13/19] Use zig wrapper for compilation --- .cargo/config.toml | 24 ++++++++++++++++++++++-- .github/workflows/build.yml | 27 ++++++++++++--------------- Makefile | 8 +++----- example/functions/src/ssr.ts | 3 ++- src/vm.rs | 2 -- zigcc | 10 ++++++++++ 6 files changed, 49 insertions(+), 25 deletions(-) create mode 100755 zigcc diff --git a/.cargo/config.toml b/.cargo/config.toml index 89595512af..a3dba95c4e 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,6 +1,26 @@ +[target.aarch64-unknown-linux-musl] +rustflags = [ + "-Zpre-link-args=-target aarch64-linux-musl", + "-Ctarget-feature=+lse,+crt-static", + "-Ctarget-cpu=neoverse-n1", +] +linker = "./zigcc" +ar = "zig ar" + +[target.x86_64-unknown-linux-musl] +rustflags = [ + "-Zpre-link-args=-target x86_64-linux-musl", + "-Ctarget-feature=+crt-static", +] +linker = "./zigcc" +ar = "zig ar" + [unstable] build-std = ["core", "compiler_builtins", "alloc", "std", "panic_abort"] build-std-features = ["panic_immediate_abort"] -# [target.aarch64-unknown-linux-musl] -# rustflags = ["-Ctarget-feature=+lse,+crt-static", "-Ctarget-cpu=neoverse-n1"] +[env] +CC_aarch64_unknown_linux_musl = "zig cc -target aarch64-linux-musl" +CXX_aarch64_unknown_linux_musl = "zig cc -target aarch64-linux-musl" +CC_x86_64_unknown_linux_musl = "zig cc -target x86_64-linux-musl" +CXX_x86_64_unknown_linux_musl = "zig cc -target x86_64-linux-musl" diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7e5d6b01e1..ba64477c32 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -56,21 +56,18 @@ jobs: uses: dtolnay/rust-toolchain@v1 with: toolchain: nightly - # - name: Cargo cache - # uses: actions/cache@v4 - # continue-on-error: false - # with: - # path: | - # ~/.cargo/bin/ - # ~/.cargo/registry/index/ - # ~/.cargo/registry/cache/ - # ~/.cargo/git/db/ - # target/ - # key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - # restore-keys: ${{ runner.os }}-cargo- - - name: Install cargo-zigbuild - run: | - cargo install cargo-zigbuild || true + - name: Cargo cache + uses: actions/cache@v4 + continue-on-error: false + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + restore-keys: ${{ runner.os }}-cargo- - name: Run tests if: inputs.arch != 'aarch64' || inputs.platform != 'linux' run: | diff --git a/Makefile b/Makefile index 494dee5945..07572c5739 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,6 @@ TARGET_darwin_x86_64 = x86_64-apple-darwin TARGET_darwin_arm64 = aarch64-apple-darwin RUST_VERSION = nightly TOOLCHAIN = +$(RUST_VERSION) -ZIGBUILD_ARG = $(TOOLCHAIN) zigbuild -r BUILD_ARG = $(TOOLCHAIN) build -r BUILD_DIR = ./target/release BUNDLE_DIR = bundle @@ -12,7 +11,6 @@ ZSTD_LIB_ARGS = -j lib-nomt UNAME=Linux ZSTD_LIB_COMPRESSION=0 ZSTD_LIB_DICTBUIL ZSTD_LIB_CC_ARGS = -s -O3 -flto ZSTD_LIB_CC_arm64 = CC="zig cc -target aarch64-linux-musl $(ZSTD_LIB_CC_ARGS)" ZSTD_LIB_CC_x64 = CC="zig cc -target aarch64-linux-musl $(ZSTD_LIB_CC_ARGS)" -CARGO_TEST_CMD = cargo-zigbuild TS_SOURCES = $(wildcard src/js/*.ts) $(wildcard src/js/@llrt/*.ts) $(wildcard tests/*.ts) STD_JS_FILE = $(BUNDLE_DIR)/@llrt/std.js @@ -54,16 +52,16 @@ llrt-darwin-arm64.zip: | clean-js js zip -j $@ target/$(TARGET_darwin_arm64)/release/llrt llrt-linux-x64.zip: | clean-js js - cargo $(ZIGBUILD_ARG) --target $(TARGET_linux_x86_64) + cargo $(BUILD_ARG) --target $(TARGET_linux_x86_64) zip -j $@ target/$(TARGET_linux_x86_64)/release/llrt llrt-linux-arm64.zip: | clean-js js - cargo $(ZIGBUILD_ARG) --target $(TARGET_linux_arm64) + cargo $(BUILD_ARG) --target $(TARGET_linux_arm64) zip -j $@ target/$(TARGET_linux_arm64)/release/llrt define release_template release-${1}: | clean-js js - cargo $$(ZIGBUILD_ARG) --target $$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})) --features lambda -vv + cargo $$(BUILD_ARG) --target $$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})) --features lambda -vv ./pack target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/llrt target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap @rm -rf llrt-lambda-${1}.zip zip -j llrt-lambda-${1}.zip target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap index.mjs diff --git a/example/functions/src/ssr.ts b/example/functions/src/ssr.ts index ff09515b7d..a1dc0275d6 100644 --- a/example/functions/src/ssr.ts +++ b/example/functions/src/ssr.ts @@ -147,7 +147,8 @@ const assetResponse = async (path: string) => { }; export const handler = async (event: any) => { - const { method, path: eventPath } = event?.requestContext?.http || {}; + const { method = "GET", path: eventPath = "/" } = + event?.requestContext?.http || {}; try { const reqSegments: string[] = (eventPath as string) diff --git a/src/vm.rs b/src/vm.rs index f6a94452b2..ee3d468dce 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -482,8 +482,6 @@ fn json_parse_string<'js>(ctx: Ctx<'js>, value: Value<'js>) -> Result } fn run_gc(ctx: Ctx<'_>) { - println!("ALLOCATED: {:?}", USED_MEM.load(Ordering::Relaxed)); - if USED_MEM.load(Ordering::Relaxed) < *GC_THRESHOLD { return; } diff --git a/zigcc b/zigcc new file mode 100755 index 0000000000..e024d48882 --- /dev/null +++ b/zigcc @@ -0,0 +1,10 @@ +#!/bin/bash +set -e + +new_array=() +for value in "$@" +do + [[ $value != *self-contained/*crt* ]] && new_array+=($value) +done + +zig cc "${new_array[@]}" \ No newline at end of file From ae5bc7a686d01fb17a7cadb674bf14520c6a3433 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 29 Jan 2024 19:54:29 +0100 Subject: [PATCH 14/19] Fix typo --- Makefile | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 07572c5739..50ba1e22dd 100644 --- a/Makefile +++ b/Makefile @@ -30,11 +30,6 @@ else ARCH := $(shell uname -m) endif -ifeq ($(DETECTED_OS),darwin) - CARGO_TEST_CMD = cargo -endif - - CURRENT_TARGET ?= $(TARGET_$(DETECTED_OS)_$(ARCH)) lambda-all: libs $(RELEASE_ZIPS) @@ -141,8 +136,8 @@ test: js test-ci: export JS_MINIFY = 0 test-ci: clean-js | toolchain js - $(CARGO_TEST_CMD) $(TOOLCHAIN) -Z panic-abort-tests test --target $(CURRENT_TARGET) - $(CARGO_TEST_CMD) $(TOOLCHAIN) run -r --target $(CURRENT_TARGET) -- test -d bundle + cargo $(TOOLCHAIN) -Z panic-abort-tests test --target $(CURRENT_TARGET) + cargo $(TOOLCHAIN) run -r --target $(CURRENT_TARGET) -- test -d bundle libs-arm64: lib/arm64/libzstd.a lib/zstd.h libs-x64: lib/x64/libzstd.a lib/zstd.h From f14e1616c2da0903bf7177f50230c211f34df358 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 29 Jan 2024 21:44:26 +0100 Subject: [PATCH 15/19] Misc fixes --- README.md | 7 ------- src/vm.rs | 4 ++-- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a8bfe66e0d..cb71b9f388 100644 --- a/README.md +++ b/README.md @@ -189,14 +189,7 @@ Install dependencies brew install zig make zstd node corepack # Ubuntu -<<<<<<< HEAD - sudo apt -y install make zstd gcc \ - libc6-arm64-cross \ - libc6-dev-arm64-cross \ - crossbuild-essential-arm64 -======= sudo apt -y install make zstd ->>>>>>> 05ddd44 (Migrate to cargo zigbuild) sudo snap install zig --classic --beta Install cargo zigbuild diff --git a/src/vm.rs b/src/vm.rs index ee3d468dce..92ddbcbff5 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -398,8 +398,6 @@ impl Vm { let ctx = AsyncContext::full(&runtime).await?; ctx.with(|ctx| { - ctx.globals().set("__gc", Func::from(run_gc))?; - crate::console::init(&ctx)?; crate::encoding::init(&ctx)?; crate::http::init(&ctx)?; @@ -497,6 +495,8 @@ fn run_gc(ctx: Ctx<'_>) { fn init(ctx: &Ctx<'_>, module_names: HashSet<&'static str>) -> Result<()> { let globals = ctx.globals(); + globals.set("__gc", Func::from(run_gc))?; + let number: Function = globals.get(PredefinedAtom::Number)?; let number_proto: Object = number.get(PredefinedAtom::Prototype)?; number_proto.set(PredefinedAtom::ToString, Func::from(number_to_string))?; From dcc2294d75e964b4c01a85de423913a6060b300d Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 29 Jan 2024 21:46:48 +0100 Subject: [PATCH 16/19] Update version & readme --- CHANGELOG.md | 42 +++--------------------------------------- Cargo.lock | 2 +- Cargo.toml | 2 +- 3 files changed, 5 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa9f58d77c..8f2c279f0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,39 +1,3 @@ -* Over 2x faster JSON parsing & stringify: - - Size 2580: - parse: 49.333µs vs. 89.792µs - stringify: 31µs vs. 92.375µs - Size 51701: - parse: 494.458µs vs. 1.160125ms - stringify: 427.791µs vs. 1.097625ms - Size 517001: - parse: 4.932875ms vs. 11.911375ms - stringify: 3.925875ms vs. 10.853125ms - Size 5170901: - parse: 56.0855ms vs. 126.783833ms - stringify: 38.671083ms vs. 107.312875ms - Size 51718901: - parse: 731.7205ms vs. 1.285825541s - stringify: 395.82225ms vs. 1.39267225s - Size 517288901: - parse: 6.886183416s vs. 14.985707583s - stringify: 3.957781167s vs. 10.885577917s - -* 7x faster integer and float toString(): - - Benchmark 1: target/release/llrt - Time (mean ± σ): 1.568 s ± 0.016 s [User: 1.555 s, System: 0.007 s] - Range (min … max): 1.558 s … 1.610 s 10 runs - - Benchmark 2: target/release/llrt-next - Time (mean ± σ): 205.1 ms ± 3.1 ms [User: 196.9 ms, System: 2.2 ms] - Range (min … max): 200.0 ms … 213.1 ms 14 runs - - Summary - target/release/llrt-next ran - 7.65 ± 0.14 times faster than target/release/llrt - -* Improved logging: - * LLRT now supports [advanced logging controls](https://aws.amazon.com/blogs/compute/introducing-advanced-logging-controls-for-aws-lambda-functions/) for AWS Lambda - * `requestId` is now captured and outputted with logging - * Console has some performance improvements by reusing String and avoiding allocations +* Use mimalloc allocator for improved performance. +* Bug fixes +* Dependency upgrades \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index d80fd8ffee..da7cd37a23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -751,7 +751,7 @@ dependencies = [ [[package]] name = "llrt" -version = "0.1.4-beta" +version = "0.1.5-beta" dependencies = [ "async-trait", "base64-simd", diff --git a/Cargo.toml b/Cargo.toml index 0c40849b69..8633bec9ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "llrt" -version = "0.1.4-beta" +version = "0.1.5-beta" edition = "2021" [features] From 1121283b45c0b26983287eda7f949d9ec92c4015 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 29 Jan 2024 21:47:07 +0100 Subject: [PATCH 17/19] Spelling --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f2c279f0b..4d6d3eb067 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,3 @@ -* Use mimalloc allocator for improved performance. +* Use MiMalloc allocator for improved performance. * Bug fixes * Dependency upgrades \ No newline at end of file From 5dac5b2605f9a0e1f7aada1da923e97fbed23684 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 29 Jan 2024 21:48:44 +0100 Subject: [PATCH 18/19] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d6d3eb067..a6e5946800 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ * Use MiMalloc allocator for improved performance. +* Switch libc from gnu to musl * Bug fixes * Dependency upgrades \ No newline at end of file From f5e8b6b6361cd7a52c0368ad777736d2230718e3 Mon Sep 17 00:00:00 2001 From: Richard Davison Date: Mon, 29 Jan 2024 21:58:46 +0100 Subject: [PATCH 19/19] Update readme --- Makefile | 2 +- README.md | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 50ba1e22dd..6867134f7c 100644 --- a/Makefile +++ b/Makefile @@ -59,7 +59,7 @@ release-${1}: | clean-js js cargo $$(BUILD_ARG) --target $$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1})) --features lambda -vv ./pack target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/llrt target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap @rm -rf llrt-lambda-${1}.zip - zip -j llrt-lambda-${1}.zip target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap index.mjs + zip -j llrt-lambda-${1}.zip target/$$(TARGET_linux_$$(RELEASE_ARCH_NAME_${1}))/release/bootstrap llrt-lambda-${1}: release-${1} endef diff --git a/README.md b/README.md index cb71b9f388..d41dc6dce1 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,11 @@ B) Without the JIT overhead, LLRT conserves both CPU and memory resources that c ## Building from source +Clone code and cd to directory + + git clone git@github.com:awslabs/llrt.git --recursive + cd llrt + Install rust curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y @@ -191,15 +196,6 @@ Install dependencies # Ubuntu sudo apt -y install make zstd sudo snap install zig --classic --beta - -Install cargo zigbuild - - cargo install cargo-zigbuild - -Clone code and cd to directory - - git clone git@github.com:awslabs/llrt.git --recursive - cd llrt Install Node.js packages @@ -210,12 +206,16 @@ Install generate libs and setup rust targets & toolchains make stdlib && make libs -Build release +Build release for Lambda make release-arm64 # or for x86, use make release-x86 +Optionally build for your local machine (Mac or Linux) + + make release + You should now have a `llrt-lambda-arm64.zip` or `llrt-lambda-x86.zip`. You can manually upload this as a Lambda layer or use it via your Infrastructure-as-code pipeline ## Running Lambda emulator