-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QuickJS as a Javascript engine option
https://bellard.org/quickjs Some benefits over SM: * Small. We're using 6 or so C files vs 700+ SM91 C++ files. * Built with Apache CouchDB as opposed having to maintain a separate SM package, like for RHEL9, for instance, where they dropped support for SM already. (see #4154). * Embedding friendly. Designed from ground-up for embedding. SM has been updating the C++ API such that we have to keep copy-pasting new versions of our C++ code every year or so. (see #4305). * Easy to modify to accept Spidermonkey 1.8.5 top level functions for map/reduce code so we don't have have to parse the JS, AST transform it, and then re-compile it. * Configurable runtime feature set - can disable workers, promises and other API and features which may not work well in a backend JS environment. Some users may want more, some may want to disable even Date(time) features to hedge again Spectre-style attacks (spectreattack.com). * Allows granular time (reduction) tracking if we wanted to provide a runtime allowance for each function. * Better sandboxing. Creating a whole JSRuntime takes only 300 microseconds, so we can afford to do that on reset. JSRuntimes cannot share JS data or object between them. * Seems to be faster in preliminary benchmarking with small concurrent VDU and view builds: https://gist.github.com/nickva/ed239651114794ebb138b1f16c5f6758 Results seem promising: - 4x faster than SM 1.8.5 - 5x faster than SM 91 - 6x reduced memory usage per couchjs process (5MB vs 30MB) * Allows compiling JS bytecode ahead of time a C array of bytes. QuickJS can be built alongside Spidermonkey and toggled on/off at runtime: ``` ./configure --dev --js-engine=quickjs ``` This makes it the default engine. But Spidermonkey can still be set in the config option. ``` [couchdb] js_engine = spidermonkey | quickjs ``` To test individual views, without switching the default use the `javascript_quickjs` language in the design docs. To keep using Spidermonkey engine after switching the default, can use `javascript_spidermonkey` language in design docs. However, language selection will reset the view and the view will have to be rebuilt. It's also possible to build without Spidermonkey support completely by using: ``` ./configure --disable-spidermonkey ``` Issue: #4448
- Loading branch information
Showing
65 changed files
with
87,157 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,12 +32,14 @@ WITH_NOUVEAU="false" | |
WITH_CLOUSEAU=0 | ||
ERLANG_MD5="false" | ||
SKIP_DEPS=0 | ||
WITH_SPIDERMONKEY="true" | ||
|
||
run_erlang() { | ||
erl -noshell -eval "$1" -eval "halt()." | ||
} | ||
|
||
COUCHDB_USER="$(whoami 2>/dev/null || echo couchdb)" | ||
JS_ENGINE=${JS_ENGINE:-"spidermonkey"} | ||
SM_VSN=${SM_VSN:-"91"} | ||
CLOUSEAU_MTH=${CLOUSEAU_MTH:-"dist"} | ||
CLOUSEAU_URI=${CLOUSEAU_URI:-"https://github.com/cloudant-labs/clouseau/releases/download/%s/clouseau-%s-dist.zip"} | ||
|
@@ -75,6 +77,8 @@ Options: | |
--rebar=PATH use rebar by specified path (version >=2.6.0 && <3.0 required) | ||
--rebar3=PATH use rebar3 by specified path | ||
--erlfmt=PATH use erlfmt by specified path | ||
--js-engine=ENGINE default js engine: spidermonkey or quickjs | ||
--disable-spidermonkey disable spidermonkey, don't try to build it | ||
EOF | ||
} | ||
|
||
|
@@ -221,6 +225,28 @@ parse_opts() { | |
printf 'ERROR: "--spidermonkey-version" requires a non-empty argument.\n' >&2 | ||
exit 1 | ||
;; | ||
--js-engine) | ||
if [ -n "$2" ]; then | ||
eval JS_ENGINE=$2 | ||
shift 2 | ||
continue | ||
else | ||
printf 'ERROR: "--js-engine" requires a non-empty argument.\n' >&2 | ||
exit 1 | ||
fi | ||
;; | ||
--js-engine=?*) | ||
eval JS_ENGINE=${1#*=} | ||
;; | ||
--js-engine=) | ||
printf 'ERROR: "--js-engine" requires a non-empty argument.\n' >&2 | ||
exit 1 | ||
;; | ||
--disable-spidermonkey) | ||
WITH_SPIDERMONKEY="false" | ||
shift | ||
continue | ||
;; | ||
|
||
--clouseau-version) | ||
if [ -n "$2" ]; then | ||
|
@@ -293,12 +319,12 @@ parse_opts() { | |
|
||
parse_opts $@ | ||
|
||
if [ "${ARCH}" = "aarch64" ] && [ "${SM_VSN}" = "60" ]; then | ||
if [ "${WITH_SPIDERMONKEY}" = "true" ] && [ "${ARCH}" = "aarch64" ] && [ "${SM_VSN}" = "60" ]; then | ||
echo "ERROR: SpiderMonkey 60 is known broken on ARM 64 (aarch64). Use another version instead." | ||
exit 1 | ||
fi | ||
|
||
if [ "${ERLANG_OS}" = "unix" ]; then | ||
if [ "${WITH_SPIDERMONKEY}" = "true" ] && [ "${ERLANG_OS}" = "unix" ]; then | ||
case "${SM_VSN}" in | ||
1.8.5) | ||
SM_HEADERS="js" | ||
|
@@ -316,11 +342,24 @@ if [ "${ERLANG_OS}" = "unix" ]; then | |
fi | ||
fi | ||
|
||
# If spidermonkey was disabled but JS_ENGINE set to "spidermonkey", reset it to "quickjs" | ||
if [ "${WITH_SPIDERMONKEY}" = "false" ] && [ "${JS_ENGINE}" = "spidermonkey" ]; then | ||
echo "NOTICE: Spidermonkey was disabled, but JS_ENGINE=spidermonkey. Setting JS_ENGINE=quickjs" | ||
JS_ENGINE="quickjs" | ||
fi | ||
|
||
# If we're in a release tarball and we don't have proper, then mark it as skipped | ||
if [ ! -d .git ] && [ "$WITH_PROPER" = "true" ] && [ ! -d src/proper ]; then | ||
WITH_PROPER="false" | ||
fi | ||
|
||
# If we're in a release tarball and we don't have spidermonkey, then mark it as skipped and enable quickjs | ||
if [ ! -d .git ] && [ "$WITH_SPIDERMONKEY" = "true" ] && [ ! -d src/couch/priv/couch_js ]; then | ||
echo "NOTICE: Spidermonkey was disabled in release tarball. Setting JS_ENGINE=quickjs" | ||
WITH_SPIDERMONKEY="false" | ||
JS_ENGINE="quickjs" | ||
fi | ||
|
||
echo "==> configuring couchdb in rel/couchdb.config" | ||
cat > rel/couchdb.config << EOF | ||
% Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
|
@@ -350,7 +389,9 @@ cat > rel/couchdb.config << EOF | |
{log_file, "$LOG_FILE"}. | ||
{fauxton_root, "./share/www"}. | ||
{user, "$COUCHDB_USER"}. | ||
{js_engine, "$JS_ENGINE"}. | ||
{spidermonkey_version, "$SM_VSN"}. | ||
{with_spidermonkey, "$WITH_SPIDERMONKEY"}. | ||
{node_name, "-name [email protected]"}. | ||
{cluster_port, 5984}. | ||
{backend_port, 5986}. | ||
|
@@ -380,13 +421,17 @@ with_nouveau = $WITH_NOUVEAU | |
with_clouseau = $WITH_CLOUSEAU | ||
user = $COUCHDB_USER | ||
js_engine = $JS_ENGINE | ||
spidermonkey_version = $SM_VSN | ||
with_spidermonkey = $WITH_SPIDERMONKEY | ||
EOF | ||
|
||
cat > $rootdir/config.erl << EOF | ||
{with_proper, $WITH_PROPER}. | ||
{erlang_md5, $ERLANG_MD5}. | ||
{js_engine, "$JS_ENGINE"}. | ||
{spidermonkey_version, "$SM_VSN"}. | ||
{with_spidermonkey, "$WITH_SPIDERMONKEY"}. | ||
EOF | ||
|
||
install_local_rebar() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,13 @@ | |
-DisableFauxton request build process skip building Fauxton (default false) | ||
-DisableDocs request build process skip building documentation (default false) | ||
-DisableSpiderMonkey do not use SpiderMonkey as JS engine (default false) | ||
-EnableNouveau enable the new experiemtal search module (default false) | ||
-EnableClouseau enable the Clouseau search module (default false) | ||
-SkipDeps do not update Erlang dependencies (default false) | ||
-CouchDBUser USER set the username to run as (defaults to current user) | ||
-SpiderMonkeyVersion VSN select the version of SpiderMonkey to use (default 91) | ||
-JSEngine ENGINE select JS engine to use (spidermonkey or quickjs) (default spidermonkey) | ||
-ClouseauVersion VSN select the version of Clouseau to use (default 2.22.0) | ||
-ClouseauMethod MTH method for Clouseau to deploy: git or dist (default dist) | ||
-ClouseauUri URI location for retrieving Clouseau (default https://github.com/cloudant-labs/clouseau/releases/download/2.22.0/clouseau-2.22.0-dist.zip) | ||
|
@@ -52,13 +54,16 @@ Param( | |
[switch]$EnableClouseau = $false, # do not use Clouseau by default | ||
[switch]$SkipDeps = $false, # do not update erlang dependencies | ||
[switch]$DisableProper = $false, # a compilation pragma. proper is a kind of automated test suite | ||
[switch]$DisableSpiderMonkey = $false, # do not use SpiderMonkey as JS engine | ||
[switch]$EnableErlangMD5 = $false, # don't use Erlang for md5 hash operations by default | ||
|
||
[ValidateNotNullOrEmpty()] | ||
[string]$CouchDBUser = [Environment]::UserName, # set the username to run as (defaults to current user) | ||
[ValidateNotNullOrEmpty()] | ||
[string]$SpiderMonkeyVersion = "91", # select the version of SpiderMonkey to use (default 91) | ||
[ValidateNotNullOrEmpty()] | ||
[string]$JSEngine = "spidermonkey", # select the JS engine (spidermonkey | quickjs) to use (default spidermonkey) | ||
[ValidateNotNullOrEmpty()] | ||
[string]$ClouseauMethod = "dist", # method for Clouseau to deploy: git or dist (default dist) | ||
[ValidateNotNullOrEmpty()] | ||
[string]$ClouseauVersion = "2.22.0", # select the version of Clouseau to use (default 2.22.0) | ||
|
@@ -145,6 +150,17 @@ $WithClouseau = $(If ($EnableClouseau) {1} else {0}) | |
$Hostname = [System.Net.Dns]::GetHostEntry([string]"localhost").HostName | ||
$WithProper = (-not $DisableProper).ToString().ToLower() | ||
$ErlangMD5 = ($EnableErlangMD5).ToString().ToLower() | ||
$WithSpiderMonkey = (-not $DisableSpiderMonkey).ToString().ToLower() | ||
|
||
if ($JSEngine -eq "quickjs") { | ||
$WithSpiderMonkey = "false" | ||
} | ||
|
||
# If spidermonkey was disabled but JS_ENGINE set to "spidermonkey", reset it to "quickjs" | ||
if ( ($WithSpiderMonkey -eq "false" ) -and ($JS_ENGINE -eq "spidermonkey" ) ) { | ||
Write-Verbose "NOTICE: Spidermonkey was disabled, but JS_ENGINE=spidermonkey. Setting JS_ENGINE=quickjs" | ||
$JS_ENGINE = "quickjs" | ||
} | ||
|
||
Write-Verbose "==> configuring couchdb in rel\couchdb.config" | ||
$CouchDBConfig = @" | ||
|
@@ -175,7 +191,9 @@ $CouchDBConfig = @" | |
{log_file, ""}. | ||
{fauxton_root, "./share/www"}. | ||
{user, "$CouchDBUser"}. | ||
{js_engine, "$JSEngine"}. | ||
{spidermonkey_version, "$SpiderMonkeyVersion"}. | ||
{with_spidermonkey, "$WithSpiderMonkey"}. | ||
{node_name, "-name [email protected]"}. | ||
{cluster_port, 5984}. | ||
{backend_port, 5986}. | ||
|
@@ -221,14 +239,19 @@ with_nouveau = $WithNouveau | |
with_clouseau = $WithClouseau | ||
user = $CouchDBUser | ||
js_engine = $JSEngine | ||
spidermonkey_version = $SpiderMonkeyVersion | ||
with_spidermonkey = $WithSpiderMonkey | ||
"@ | ||
$InstallMk | Out-File "$rootdir\install.mk" -encoding ascii | ||
|
||
$ConfigERL = @" | ||
{with_proper, $WithProper}. | ||
{erlang_md5, $ErlangMD5}. | ||
{js_engine, "$JSEngine"}. | ||
{spidermonkey_version, "$SpiderMonkeyVersion"}. | ||
{with_spidermonkey, "$WithSpiderMonkey"}. | ||
"@ | ||
$ConfigERL | Out-File "$rootdir\config.erl" -encoding ascii | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.