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

Experiments #1

Open
wants to merge 15 commits into
base: wasm
Choose a base branch
from
Open
205 changes: 198 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Note: Last built with version 1.38.30 of Emscripten
# Note: Last built with version 1.38.31 of Emscripten

# TODO: Emit a file showing which version of emcc and SQLite was used to compile the emitted output.
# TODO: Create a release on Github with these compiled assets rather than checking them in
Expand All @@ -17,10 +17,37 @@ EXTENSION_FUNCTIONS_SHA1 = c68fa706d6d9ff98608044c00212473f9c14892f

EMCC=emcc

CFLAGS=-O2 -DSQLITE_OMIT_LOAD_EXTENSION -DSQLITE_DISABLE_LFS -DLONGDOUBLE_TYPE=double -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS
# TODO: move these settings into a header file to make them easier to add comments to
SQLITE_COMPILATION_FLAGS= \
-DSQLITE_OMIT_LOAD_EXTENSION \
-DSQLITE_DISABLE_LFS \
-DLONGDOUBLE_TYPE=double \
-DSQLITE_ENABLE_FTS3 \
-DSQLITE_ENABLE_FTS3_PARENTHESIS \
-DSQLITE_THREADSAFE=0 \
-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
-DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
-DSQLITE_MAX_EXPR_DEPTH=0 \
-DSQLITE_OMIT_DECLTYPE \
-DSQLITE_OMIT_DEPRECATED \
-DSQLITE_OMIT_PROGRESS_CALLBACK \
-DSQLITE_OMIT_SHARED_CACHE \
-DSQLITE_USE_ALLOCA \
-DSQLITE_DEFAULT_MEMSTATUS=0 \
-DSQLITE_BYTEORDER=1234 \
-DSQLITE_HAVE_ISNAN=1 \
-DHAVE_MALLOC_USABLE_SIZE=1 \
-DHAVE_STRCHRNUL=1 \
-DSQLITE_TEMP_STORE=2 \
-DSQLITE_THREADSAFE=0 \
-DSQLITE_USE_URI=1 \
-Oz

# For the smallest filesize, leave JSON disabled
#-DSQLITE_ENABLE_JSON1=0 \

# When compiling to WASM, enabling memory-growth is not expected to make much of an impact, so we enable it for all builds
# Since tihs is a library and not a standalone executable, we don't want to catch unhandled Node process exceptions
# Since this is a library and not a standalone executable, we don't want to catch unhandled Node process exceptions
# So, we do : `NODEJS_CATCH_EXIT=0`, which fixes issue: https://github.com/kripken/sql.js/issues/173 and https://github.com/kripken/sql.js/issues/262
EMFLAGS = \
--memory-init-file 0 \
Expand All @@ -35,8 +62,7 @@ EMFLAGS_WASM = \
-s ALLOW_MEMORY_GROWTH=1

EMFLAGS_OPTIMIZED= \
-s INLINING_LIMIT=50 \
-O3 \
-Oz \
--closure 1

EMFLAGS_DEBUG = \
Expand Down Expand Up @@ -129,10 +155,10 @@ out/api.js: src/output-pre.js src/api.coffee src/exports.coffee src/api-data.cof

out/sqlite3.bc: sqlite-src/$(SQLITE_AMALGAMATION)
# Generate llvm bitcode
$(EMCC) $(CFLAGS) sqlite-src/$(SQLITE_AMALGAMATION)/sqlite3.c -o $@
$(EMCC) $(SQLITE_COMPILATION_FLAGS) sqlite-src/$(SQLITE_AMALGAMATION)/sqlite3.c -o $@

out/extension-functions.bc: sqlite-src/$(SQLITE_AMALGAMATION)/$(EXTENSION_FUNCTIONS)
$(EMCC) $(CFLAGS) -s LINKABLE=1 sqlite-src/$(SQLITE_AMALGAMATION)/extension-functions.c -o $@
$(EMCC) $(SQLITE_COMPILATION_FLAGS) -s LINKABLE=1 sqlite-src/$(SQLITE_AMALGAMATION)/extension-functions.c -o $@

# TODO: This target appears to be unused. If we re-instatate it, we'll need to add more files inside of the JS folder
# module.tar.gz: test package.json AUTHORS README.md dist/sql-asm.js
Expand Down Expand Up @@ -185,3 +211,168 @@ clean-all:
rm -f out/* dist/* cache/*
rm -rf sqlite-src/

# Notes about file sizes:
#
# Sizes of optimized .wasm based on these compilation flags:
# SQLITE_COMPILATION_FLAGS= \
# -DSQLITE_OMIT_LOAD_EXTENSION \
# -DSQLITE_DISABLE_LFS \
# -DLONGDOUBLE_TYPE=double \
# -DSQLITE_ENABLE_FTS3 \
# -DSQLITE_ENABLE_FTS3_PARENTHESIS \
# -DSQLITE_THREADSAFE=0 \
# -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
# -DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
# -DSQLITE_MAX_EXPR_DEPTH=0 \
# -DSQLITE_OMIT_DECLTYPE \
# -DSQLITE_OMIT_DEPRECATED \
# -DSQLITE_OMIT_PROGRESS_CALLBACK \
# -DSQLITE_OMIT_SHARED_CACHE \
# -DSQLITE_USE_ALLOCA \

# No opt settings on either .bc or .js compilation:
# .bc: 1.8MB, sql.js: 7.5MB, sql.wasm: 1.5MB
# $ node test/test_perf.js
# Loaded and inited ../js/sql.js in 278ms
# Started benchmark function...
# Total time to execute benchmark func: 5073 ms
# $ node test/test_perf.js wasm
# Loaded and inited ../js/sql-wasm.js in 166ms
# Started benchmark function...
# Total time to execute benchmark func: 1177 ms

# No settings on c comp, but -0z on js output:
# .bc: 1.8MB, sql.js: 2.1MB, sql.wasm: 925KB
# ASM.js:
# $ node test/test_perf.js
# Loaded and inited ../js/sql.js in 253ms
# Started benchmark function...
# Total time to execute benchmark func: 1424 ms
# WASM
# $ node test/test_perf.js wasm
# Loaded and inited ../js/sql-wasm.js in 537ms
# Started benchmark function...
# Total time to execute benchmark func: 615 ms

# -Oz on c comp, nothing on js output:
# .bc 1.6, sql.js: 5.2, sql.wasm: 1MB
#ASM.js:
#$ node test/test_perf.js
# Loaded and inited ../js/sql.js in 198ms
# Started benchmark function...
# Total time to execute benchmark func: 4046 ms
#WASM:
# $ node test/test_perf.js wasm
# Loaded and inited ../js/sql-wasm.js in 126ms
# Started benchmark function...
# Total time to execute benchmark func: 1072 ms


# -Oz, -0z: .bc 1.6MB, sql.js: 1.2, .wasm: 571KB
# ASM.js:
# $ node test/test_perf.js
# Loaded and inited ../js/sql.js in 164ms
# Started benchmark function...
# Total time to execute benchmark func: 976 ms
# WASM
# $ node test/test_perf.js wasm
# Loaded and inited ../js/sql-wasm.js in 436ms
# Started benchmark function...
# Total time to execute benchmark func: 513 ms

# -Oz, -03: .bc 1.6MB, sql.js: 1.2, .wasm: 593KB - 535ms -
# ASM.js:
# $ node test/test_perf.js
# Loaded and inited ../js/sql.js in 166ms
# Started benchmark function...
# Total time to execute benchmark func: 948 ms
# WASM
# $ node test/test_perf.js wasm
# Loaded and inited ../js/sql-wasm.js in 438ms
# Started benchmark function...
# Total time to execute benchmark func: 490 ms


# -O3, -O3: .bc: 4.0MB, sql.js 2.9MB, .wasm: 1.4MB
# ASM.js:
# $ node test/test_perf.js
# Loaded and inited ../js/sql.js in 333ms
# Started benchmark function...
# Total time to execute benchmark func: 3407 ms
# WASM
# $ node test/test_perf.js wasm
# Loaded and inited ../js/sql-wasm.js in 832ms
# Started benchmark function...
# Total time to execute benchmark func: 499 ms


# -O3, -Oz: .bc: 1.6MB, sql.js 2.9MB, .wasm: 1.4MB
# ASM.js:
# $ node test/test_perf.js
# Loaded and inited ../js/sql.js in 324ms
# Started benchmark function...
# Total time to execute benchmark func: 3379 ms
# WASM:
# $ node test/test_perf.js wasm
# Loaded and inited ../js/sql-wasm.js in 844ms
# Started benchmark function...
# Total time to execute benchmark func: 497 ms

# -Os, -Os
#1.7MB 1.4MB 583k
# ASM.js:
# $ node test/test_perf.js
# Loaded and inited ../js/sql.js in 169ms
# Started benchmark function...
# Total time to execute benchmark func: 973 ms
# WASM
# $ node test/test_perf.js wasm
# Loaded and inited ../js/sql-wasm.js in 467ms
# Started benchmark function...
# Total time to execute benchmark func: 505 ms


# Sizes now after adding these settings:
# -DSQLITE_BYTEORDER=1234 \
# -DSQLITE_ENABLE_JSON1=1 \
# -DSQLITE_HAVE_ISNAN=1 \
# -DHAVE_MALLOC_USABLE_SIZE=1 \
# -DHAVE_STRCHRNUL=1 \
# -DSQLITE_TEMP_STORE=2 \
# -DSQLITE_THREADSAFE=0 \
# -DSQLITE_USE_URI=1 \
#
# -Oz -Oz:
# .bc:1.7MB sql.js:1.3 sql.wasm:599
#ASM.js:
# $ node test/test_perf.js
# Loaded and inited ../js/sql.js in 171ms
# Started benchmark function...
# Total time to execute benchmark func: 943 ms
#WASM:
# $ node test/test_perf.js wasm
# Loaded and inited ../js/sql-wasm.js in 451ms
# Started benchmark function...
# Total time to execute benchmark func: 516 ms
# WASM - single-file: - sql-wasm.js: 843kb
# $ node test/test_perf.js wasm
# Loaded and inited ../js/sql-wasm.js in 461ms
# Started benchmark function...
# Total time to execute benchmark func: 514 ms

# Taking out the JSON stuff:
# -Oz -Oz .bc: 1.6 sql.js: 1.2 sql.wasm: 571KB
# asm.js:
# $ node test/test_perf.js
# Loaded and inited ../js/sql.js in 154ms
# Started benchmark function...
# Total time to execute benchmark func: 933 ms
# WASM
# $ node test/test_perf.js wasm
# Loaded and inited ../js/sql-wasm.js in 438ms
# Started benchmark function...
# Total time to execute benchmark func: 490 ms

# Conclusion:
# Use -Oz -Oz for a very small, fast build. However, although the runtime is fast, the file takes 500ms to load on the same machine that only takes 154ms to load the asm.js build

14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ There are no C bindings or node-gyp compilation here, sql.js is a simple javascr

SQLite is public domain, sql.js is MIT licensed.

Sql.js predates WebAssembly, and thus started as an [asm.js](https://en.wikipedia.org/wiki/Asm.js) project. It still supports asm.js for backwards compatability.
Sql.js predates WebAssembly, and thus started as an [asm.js](https://en.wikipedia.org/wiki/Asm.js) project. It still supports asm.js for backwards compatibility.

## Version of binaries
Sql.js was last built with:
Expand All @@ -22,7 +22,7 @@ A [full documentation](http://kripken.github.io/sql.js/documentation/#http://kri
## Usage

```javascript
var initSqlJs = require('sql-wasm.js');
var initSqlJs = require('sql.js');
// or if you are in a browser:
//var initSqlJs = window.initSqlJs;

Expand Down Expand Up @@ -114,12 +114,12 @@ The test files provide up to date example of the use of the api.
});
</script>
<body>
Output is in Javscript console
Output is in Javascript console
</body>
</html>
```

#### Creating a database from a file choosen by the user
#### Creating a database from a file chosen by the user
`SQL.Database` constructor takes an array of integer representing a database file as an optional parameter.
The following code uses an HTML input as the source for loading a database:
```javascript
Expand Down Expand Up @@ -237,7 +237,7 @@ So in the past, you would:
or:
```javascript
var SQL = require('sql.js');
var db = new QL.Database();
var db = new SQL.Database();
//...
```

Expand All @@ -263,15 +263,15 @@ initSqlJs().then(function(SQL){
`NOTHING` is now a reserved word in SQLite, whereas previously it was not. This could cause errors like `Error: near "nothing": syntax error`

### Downloading/Using: ###
Although asm.js files were distributed as a single Javascript file, WebAssembly libraries are most efficiently distributed as a pair of files, the `.js` loader and the `.wasm` file, like [dist/sql-wasm.js]([dist/sql-wasm.js]) and [dist/sql-wasm.wasm]([dist/sql-wasm.wasm]). The `.js` file is reponsible for wrapping/loading the `.wasm` file.
Although asm.js files were distributed as a single Javascript file, WebAssembly libraries are most efficiently distributed as a pair of files, the `.js` loader and the `.wasm` file, like [dist/sql-wasm.js]([dist/sql-wasm.js]) and [dist/sql-wasm.wasm]([dist/sql-wasm.wasm]). The `.js` file is responsible for wrapping/loading the `.wasm` file.




## Versions of sql.js included in `dist/`
- `sql-wasm.js` : The Web Assembly version of Sql.js. Minified and suitable for production. Use this. If you use this, you will need to include/ship `sql-wasm.wasm` as well.
- `sql-wasm-debug.js` : The Web Assembly, Debug version of Sql.js. Larger, with assertions turned on. Useful for local development. You will need to include/ship `sql-wasm-debug.wasm` if you use this.
- `sql-asm.js` : The older asm.js version of Sql.js. Slower and larger. Provided for compatiblity reasons.
- `sql-asm.js` : The older asm.js version of Sql.js. Slower and larger. Provided for compatibility reasons.
- `sql-asm-memory-growth.js` : Asm.js doesn't allow for memory to grow by default, because it is slower and de-optimizes. If you are using sql-asm.js and you see this error (`Cannot enlarge memory arrays`), use this file.
- `sql-asm-debug.js` : The _Debug_ asm.js version of Sql.js. Use this for local development.
- `worker.*` - Web Worker versions of the above libraries. More limited API. See [examples/GUI/gui.js](examples/GUI/gui.js) for a good example of this.
Expand Down
Loading