Skip to content

Commit

Permalink
Merge branch 'lightweight-functions'
Browse files Browse the repository at this point in the history
Add support for lightweight functions or "lightfuncs", a plain tagged type
that contains a Duktape/C function and 16-bit flags which contain an 8-bit
signed magic value, 4-byte argument count (with 15 indicating varargs) and
a 4-byte virtual "length" property.  A lightfunc requires no heap allocations,
so it is useful in low memory environments to reduce RAM footprint.

Lightfuncs can be pushed on the value stack using the new API call
duk_push_c_lightfunc().

Also add DUK_OPT_LIGHTFUNC_BUILTINS feature option which causes Duktape to
convert all built-in functions (like Math.cos) into lightfuncs.  This saves
around 14 kB of initial RAM usage (which is about 45kB before the change)
on 32-bit environments, which is useful for at least devices with 128kB
system RAM.

Lightfuncs don't interact well with the non-standard "caller" property
(DUK_OPT_NONSTD_FUNC_CALLER_PROPERTY) so you shouldn't use them at all
if you enable that option.

If you don't use lightfuncs or enable DUK_OPT_LIGHTFUNC_BUILTINS, there
should be no visible changes, except changes in some error message strings.
Internally there are a lot of changes, especially in call handling.
  • Loading branch information
svaarala committed Nov 23, 2014
2 parents 25b6a0b + ba1ada6 commit f6b3e59
Show file tree
Hide file tree
Showing 80 changed files with 6,675 additions and 755 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ xmldoc
FlameGraph
dtrace4linux
flow
massif-*.out
ms_print.*
alljoyn-js
ajtcl
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ CCOPTS_FEATURES += -DDUK_OPT_DEBUG_BUFSIZE=512
#CCOPTS_FEATURES += -DDUK_OPT_NO_ES6_PROXY
#CCOPTS_FEATURES += -DDUK_OPT_NO_ZERO_BUFFER_DATA
#CCOPTS_FEATURES += -DDUK_OPT_USER_INITJS='"this.foo = 123"'
#CCOPTS_FEATURES += -DDUK_OPT_LIGHTFUNC_BUILTINS
CCOPTS_FEATURES += -DDUK_CMDLINE_FANCY
CCOPTS_FEATURES += -DDUK_CMDLINE_ALLOC_LOGGING
CCOPTS_FEATURES += -DDUK_CMDLINE_ALLOC_TORTURE
Expand All @@ -228,6 +229,7 @@ CCOPTS_SHARED += -I./dist/src -I./dist/examples/alloc-logging -I./dist/examples/
CCOPTS_NONDEBUG = $(CCOPTS_SHARED) $(CCOPTS_FEATURES)
CCOPTS_NONDEBUG += -Os -fomit-frame-pointer -g -ggdb
#CCOPTS_NONDEBUG += -DDUK_OPT_ASSERTIONS

CCOPTS_DEBUG = $(CCOPTS_SHARED) $(CCOPTS_FEATURES)
CCOPTS_DEBUG += -O0 -g -ggdb
CCOPTS_DEBUG += -DDUK_OPT_DEBUG
Expand Down Expand Up @@ -294,6 +296,7 @@ clean:
@rm -rf luajs
@rm -f dukweb.js
@rm -rf /tmp/dukweb-test/
@rm -f massif-*.out

.PHONY: cleanall
cleanall: clean
Expand Down Expand Up @@ -794,6 +797,7 @@ ajduk: alljoyn-js ajtcl dist
$(CCOPTS_NONDEBUG) \
-m32 \
-UDUK_CMDLINE_FANCY -DDUK_CMDLINE_AJSHEAP -D_POSIX_C_SOURCE=200809L \
-DDUK_OPT_LIGHTFUNC_BUILTINS \
$(DUKTAPE_SOURCES) $(DUKTAPE_CMDLINE_SOURCES) \
alljoyn-js/ajs_heap.c ajtcl/src/aj_debug.c ajtcl/target/linux/aj_target_util.c \
-lm -lpthread
Expand Down Expand Up @@ -888,3 +892,23 @@ codepolicycheckvim:
.PHONY: big-git-files
big-git-files:
util/find_big_git_files.sh

# Simple heap graph and peak usage using valgrind --tool=massif, for quick
# and dirty baseline comparison. Say e.g. 'make massif-test-dev-hello-world'.
# The target name is intentionally not 'massif-%.out' so that the rule is never
# satisfied and can be executed multiple times without cleaning.
# Grep/sed hacks from:
# http://stackoverflow.com/questions/774556/peak-memory-usage-of-a-linux-unix-process
massif-%: ecmascript-testcases/%.js duk
@rm -f $(@).out
valgrind --tool=massif --peak-inaccuracy=0.0 --massif-out-file=$(@).out ./duk $< >/dev/null 2>/dev/null
@ms_print $(@).out | head -35
@echo "[... clipped... ]"
@echo ""
@echo -n "MAXIMUM: "
@cat $(@).out | grep mem_heap_B | sed -e 's/mem_heap_B=\(.*\)/\1/' | sort -g | tail -n 1

# Convenience targets
massif-helloworld: massif-test-dev-hello-world
massif-deepmerge: massif-test-dev-deepmerge
massif-arcfour: massif-test-dev-arcfour
12 changes: 12 additions & 0 deletions RELEASES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,18 @@ Planned
* Main release goal: improved low memory support to allow Duktape to run
better on devices with 128kB system memory

* Add lightfunc (DUK_TYPE_LIGHTFUNC) primitive type, representing a
Duktape/C function with a plain tagged value without any heap allocations

* Add duk_push_c_lightfunc() API call to push user lightfuncs on the
value stack

* Add duk_is_lightfunc() API call to type check for lightfuncs

* Add feature option DUK_OPT_LIGHTFUNC_BUILTINS which causes Duktape to use
lightfuncs for almost all built-in functions, saving around 14kB of Duktape
heap on 32-bit platforms

* Add duk_is_error() API call to check if a value inherits from Error

* Add duk_get_error_code() API call to check if a value inherits from
Expand Down
Loading

0 comments on commit f6b3e59

Please sign in to comment.