Skip to content

Commit

Permalink
Add zeroMemory JS utility function to avoid _memset. NFC (#14441)
Browse files Browse the repository at this point in the history
This avoids the extra export from wasm and the potential
codesize of including memset.  Its probably faster and
smaller over the wire too.

Using TypedArray:fill looks fine on all the current
min browser versions we support:
https://caniuse.com/mdn-javascript_builtins_typedarray_fill

If we need to support even older browsers we can always
ifdef/polyfill at this single location in the future.
  • Loading branch information
sbc100 authored Jun 12, 2021
1 parent 6b80c5d commit 315d362
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
24 changes: 17 additions & 7 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ LibraryManager.library = {
setTempRet0(val);
},

$zeroMemory: function(address, size) {
#if LEGACY_VM_SUPPORT
if (!HEAPU8.fill) {
for (var i = 0; i < size; i++) {
HEAPU8[address + i] = 0;
}
return;
}
#endif
HEAPU8.fill(0, address, address + size);

This comment has been minimized.

Copy link
@vadimkantorov

vadimkantorov Jun 12, 2021

it is vectorized by the browser impl, right?

This comment has been minimized.

Copy link
@sbc100

sbc100 Jun 15, 2021

Author Collaborator

Thats really and implementation detail, but I can't imagine it not being in practice.

},

#if SAFE_HEAP
// Trivial wrappers around runtime functions that make these symbols available
// to native code.
Expand Down Expand Up @@ -1490,12 +1502,13 @@ LibraryManager.library = {
// sys/times.h
// ==========================================================================

times__deps: ['$zeroMemory'],
times: function(buffer) {
// clock_t times(struct tms *buffer);
// http://pubs.opengroup.org/onlinepubs/009695399/functions/times.html
// NOTE: This is fake, since we can't calculate real CPU time usage in JS.
if (buffer !== 0) {
_memset(buffer, 0, {{{ C_STRUCTS.tms.__size__ }}});
zeroMemory(buffer, {{{ C_STRUCTS.tms.__size__ }}});
}
return 0;
},
Expand Down Expand Up @@ -2040,23 +2053,22 @@ LibraryManager.library = {

return { family: family, addr: addr, port: port };
},
$writeSockaddr__deps: ['$Sockets', '$inetPton4', '$inetPton6'],
$writeSockaddr__deps: ['$Sockets', '$inetPton4', '$inetPton6', '$zeroMemory'],
$writeSockaddr: function (sa, family, addr, port, addrlen) {
switch (family) {
case {{{ cDefine('AF_INET') }}}:
addr = inetPton4(addr);
zeroMemory(sa, {{{ C_STRUCTS.sockaddr_in.__size__ }}});
if (addrlen) {
{{{ makeSetValue('addrlen', 0, C_STRUCTS.sockaddr_in.__size__, 'i32') }}};
}
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in.sin_family, 'family', 'i16') }}};
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in.sin_addr.s_addr, 'addr', 'i32') }}};
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in.sin_port, '_htons(port)', 'i16') }}};
/* Use makeSetValue instead of memset to avoid adding memset dependency for all users of writeSockaddr. */
{{{ assert(C_STRUCTS.sockaddr_in.__size__ - C_STRUCTS.sockaddr_in.sin_zero == 8), '' }}}
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in.sin_zero, '0', 'i64') }}};
break;
case {{{ cDefine('AF_INET6') }}}:
addr = inetPton6(addr);
zeroMemory(sa, {{{ C_STRUCTS.sockaddr_in6.__size__ }}});
if (addrlen) {
{{{ makeSetValue('addrlen', 0, C_STRUCTS.sockaddr_in6.__size__, 'i32') }}};
}
Expand All @@ -2066,8 +2078,6 @@ LibraryManager.library = {
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in6.sin6_addr.__in6_union.__s6_addr+8, 'addr[2]', 'i32') }}};
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in6.sin6_addr.__in6_union.__s6_addr+12, 'addr[3]', 'i32') }}};
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in6.sin6_port, '_htons(port)', 'i16') }}};
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in6.sin6_flowinfo, '0', 'i32') }}};
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in6.sin6_scope_id, '0', 'i32') }}};
break;
default:
return {{{ cDefine('EAFNOSUPPORT') }}};
Expand Down
3 changes: 2 additions & 1 deletion src/library_sdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,7 @@ var LibrarySDL = {
return SDL.version;
},

SDL_Init__deps: ['$zeroMemory'],
SDL_Init__proxy: 'sync',
SDL_Init__sig: 'ii',
SDL_Init__docs: '/** @param{number=} initFlags */',
Expand All @@ -1368,7 +1369,7 @@ var LibrarySDL = {

window.addEventListener("unload", SDL.receiveEvent);
SDL.keyboardState = _malloc(0x10000); // Our SDL needs 512, but 64K is safe for older SDLs
_memset(SDL.keyboardState, 0, 0x10000);
zeroMemory(SDL.keyboardState, 0x10000);
// Initialize this structure carefully for closure
SDL.DOMEventToSDLEvent['keydown'] = 0x300 /* SDL_KEYDOWN */;
SDL.DOMEventToSDLEvent['keyup'] = 0x301 /* SDL_KEYUP */;
Expand Down
7 changes: 4 additions & 3 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ var SyscallsLibrary = {
}
},

$syscallMmap2__deps: ['$SYSCALLS',
$syscallMmap2__deps: ['$SYSCALLS', '$zeroMemory',
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
'$FS',
#endif
Expand All @@ -247,7 +247,7 @@ var SyscallsLibrary = {
if ((flags & {{{ cDefine('MAP_ANONYMOUS') }}}) !== 0) {
ptr = _memalign({{{ WASM_PAGE_SIZE }}}, len);
if (!ptr) return -{{{ cDefine('ENOMEM') }}};
_memset(ptr, 0, len);
zeroMemory(ptr, len);
allocated = true;
} else {
#if FILESYSTEM && SYSCALLS_REQUIRE_FILESYSTEM
Expand Down Expand Up @@ -494,11 +494,12 @@ var SyscallsLibrary = {
__sys_setrlimit: function(varargs) {
return 0; // no-op
},
__sys_getrusage__deps: ['$zeroMemory'],
__sys_getrusage: function(who, usage) {
#if SYSCALL_DEBUG
err('warning: untested syscall');
#endif
_memset(usage, 0, {{{ C_STRUCTS.rusage.__size__ }}});
zeroMemory(usage, {{{ C_STRUCTS.rusage.__size__ }}});
{{{ makeSetValue('usage', C_STRUCTS.rusage.ru_utime.tv_sec, '1', 'i32') }}}; // fake some values
{{{ makeSetValue('usage', C_STRUCTS.rusage.ru_utime.tv_usec, '2', 'i32') }}};
{{{ makeSetValue('usage', C_STRUCTS.rusage.ru_stime.tv_sec, '3', 'i32') }}};
Expand Down
3 changes: 2 additions & 1 deletion src/library_uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

mergeInto(LibraryManager.library, {
// Clear a 'compact' UUID.
uuid_clear__deps: ['$zeroMemory'],
uuid_clear: function(uu) {
// void uuid_clear(uuid_t uu);
_memset(uu, 0, 16);
zeroMemory(uu, 16);
},

// Compare whether or not two 'compact' UUIDs are the same.
Expand Down
1 change: 1 addition & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ var POLYFILL_OLD_MATH_FUNCTIONS = 0;
// * Work around old Chromium WebGL 1 bug (-s WORKAROUND_OLD_WEBGL_UNIFORM_UPLOAD_IGNORED_OFFSET_BUG=1)
// * Disable WebAssembly. (Must be paired with -s WASM=0)
// * Adjusts MIN_X_VERSION settings to 0 to include support for all browser versions.
// * Avoid TypedArray.fill, if necessary, in zeroMemory utility function.
// You can also configure the above options individually.
// [link]
var LEGACY_VM_SUPPORT = 0;
Expand Down
7 changes: 2 additions & 5 deletions tools/deps_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
'Mix_LoadWAV_RW': ['fileno'],
'SDL_CreateRGBSurface': ['malloc', 'free'],
'SDL_GL_GetProcAddress': ['malloc'],
'SDL_Init': ['malloc', 'free', 'memset', 'memcpy'],
'SDL_Init': ['malloc', 'free', 'memcpy'],
'SDL_LockSurface': ['malloc', 'free'],
'SDL_OpenAudio': ['malloc', 'free'],
'SDL_PushEvent': ['malloc', 'free'],
Expand Down Expand Up @@ -160,7 +160,6 @@
'getnameinfo': ['htons', 'ntohs'],
'getpeername': ['htons'],
'getsockname': ['htons'],
'getrusage': ['memset'],
'glGetString': ['malloc'],
'glGetStringi': ['malloc'],
'glMapBufferRange': ['malloc'],
Expand All @@ -173,7 +172,7 @@
'localtime': ['_get_tzname', '_get_daylight', '_get_timezone', 'malloc'],
'localtime_r': ['_get_tzname', '_get_daylight', '_get_timezone', 'malloc'],
'mktime': ['_get_tzname', '_get_daylight', '_get_timezone', 'malloc'],
'mmap': ['memalign', 'memset', 'malloc'],
'mmap': ['memalign', 'malloc'],
'munmap': ['malloc', 'free'],
'pthread_create': ['malloc', 'free', 'emscripten_main_thread_process_queued_calls'],
'readdir': ['malloc'],
Expand All @@ -193,11 +192,9 @@
'setgroups': ['sysconf'],
'syslog': ['malloc', 'ntohs'],
'timegm': ['_get_tzname', '_get_daylight', '_get_timezone', 'malloc'],
'times': ['memset'],
'tmpnam': ['malloc'],
'ttyname': ['malloc'],
'tzset': ['_get_tzname', '_get_daylight', '_get_timezone', 'malloc'],
'uuid_clear': ['memset'],
'uuid_compare': ['memcmp'],
'uuid_copy': ['memcpy'],
'wgpuBufferGetMappedRange': ['malloc', 'free'],
Expand Down

0 comments on commit 315d362

Please sign in to comment.