From d7ef8f38fe95c7bc2274473e4ec02fec410872d5 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 28 Aug 2020 02:54:30 -0700 Subject: [PATCH] Move static allocation for fetch queue from JS to C++ code (#12049) This avoids the runtime allocation on the JS side. See: #12040 --- src/library_fetch.js | 8 +----- system/lib/fetch/emscripten_fetch.cpp | 38 +++++++++++++-------------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/library_fetch.js b/src/library_fetch.js index 6dadc36aa..f5dc9c12c 100644 --- a/src/library_fetch.js +++ b/src/library_fetch.js @@ -12,13 +12,7 @@ var LibraryFetch = { #else $Fetch__postset: 'Fetch.staticInit();', #endif - $fetchWorkQueue: '0', $Fetch: Fetch, - _emscripten_get_fetch_work_queue__deps: ['$fetchWorkQueue'], - _emscripten_get_fetch_work_queue: function() { - if (!fetchWorkQueue) fetchWorkQueue = _malloc(12); - return fetchWorkQueue; - }, _emscripten_fetch_get_response_headers_length: _fetch_get_response_headers_length, _emscripten_fetch_get_response_headers: _fetch_get_response_headers, _emscripten_fetch_free: _fetch_free, @@ -35,7 +29,7 @@ var LibraryFetch = { #if FETCH_SUPPORT_INDEXEDDB '$__emscripten_fetch_cache_data', '$__emscripten_fetch_load_cached_data', '$__emscripten_fetch_delete_cached_data', #endif - '_emscripten_get_fetch_work_queue', 'emscripten_is_main_browser_thread'] + 'emscripten_is_main_browser_thread'] }; mergeInto(LibraryManager.library, LibraryFetch); diff --git a/system/lib/fetch/emscripten_fetch.cpp b/system/lib/fetch/emscripten_fetch.cpp index 2b9f886ae..5750ff73d 100644 --- a/system/lib/fetch/emscripten_fetch.cpp +++ b/system/lib/fetch/emscripten_fetch.cpp @@ -18,37 +18,35 @@ extern "C" { // Uncomment the following and clear the cache with emcc --clear-cache to rebuild this file to // enable internal debugging. #define FETCH_DEBUG -struct __emscripten_fetch_queue { +static void fetch_free(emscripten_fetch_t* fetch); + +// APIs defined in JS +void emscripten_start_fetch(emscripten_fetch_t* fetch); +int32_t _emscripten_fetch_get_response_headers_length(int32_t fetchID); +int32_t _emscripten_fetch_get_response_headers(int32_t fetchID, int32_t dst, int32_t dstSizeBytes); +void _emscripten_fetch_free(unsigned int); + +struct emscripten_fetch_queue { emscripten_fetch_t** queuedOperations; int numQueuedItems; int queueSize; }; -static void fetch_free(emscripten_fetch_t* fetch); +emscripten_fetch_queue* _emscripten_get_fetch_queue() { + static thread_local emscripten_fetch_queue g_queue; -extern "C" { -void emscripten_start_fetch(emscripten_fetch_t* fetch); -__emscripten_fetch_queue* _emscripten_get_fetch_work_queue(); - -__emscripten_fetch_queue* _emscripten_get_fetch_queue() { - __emscripten_fetch_queue* queue = _emscripten_get_fetch_work_queue(); - if (!queue->queuedOperations) { - queue->queueSize = 64; - queue->numQueuedItems = 0; - queue->queuedOperations = - (emscripten_fetch_t**)malloc(sizeof(emscripten_fetch_t*) * queue->queueSize); + if (!g_queue.queuedOperations) { + g_queue.queueSize = 64; + g_queue.numQueuedItems = 0; + g_queue.queuedOperations = + (emscripten_fetch_t**)malloc(sizeof(emscripten_fetch_t*) * g_queue.queueSize); } - return queue; -} - -int32_t _emscripten_fetch_get_response_headers_length(int32_t fetchID); -int32_t _emscripten_fetch_get_response_headers(int32_t fetchID, int32_t dst, int32_t dstSizeBytes); -void _emscripten_fetch_free(unsigned int); + return &g_queue; } void emscripten_proxy_fetch(emscripten_fetch_t* fetch) { // TODO: mutex lock - __emscripten_fetch_queue* queue = _emscripten_get_fetch_queue(); + emscripten_fetch_queue* queue = _emscripten_get_fetch_queue(); // TODO handle case when queue->numQueuedItems >= queue->queueSize queue->queuedOperations[queue->numQueuedItems++] = fetch; #ifdef FETCH_DEBUG