Skip to content

Commit d7ef8f3

Browse files
authored
Move static allocation for fetch queue from JS to C++ code (#12049)
This avoids the runtime allocation on the JS side. See: #12040
1 parent 44170ce commit d7ef8f3

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

src/library_fetch.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ var LibraryFetch = {
1212
#else
1313
$Fetch__postset: 'Fetch.staticInit();',
1414
#endif
15-
$fetchWorkQueue: '0',
1615
$Fetch: Fetch,
17-
_emscripten_get_fetch_work_queue__deps: ['$fetchWorkQueue'],
18-
_emscripten_get_fetch_work_queue: function() {
19-
if (!fetchWorkQueue) fetchWorkQueue = _malloc(12);
20-
return fetchWorkQueue;
21-
},
2216
_emscripten_fetch_get_response_headers_length: _fetch_get_response_headers_length,
2317
_emscripten_fetch_get_response_headers: _fetch_get_response_headers,
2418
_emscripten_fetch_free: _fetch_free,
@@ -35,7 +29,7 @@ var LibraryFetch = {
3529
#if FETCH_SUPPORT_INDEXEDDB
3630
'$__emscripten_fetch_cache_data', '$__emscripten_fetch_load_cached_data', '$__emscripten_fetch_delete_cached_data',
3731
#endif
38-
'_emscripten_get_fetch_work_queue', 'emscripten_is_main_browser_thread']
32+
'emscripten_is_main_browser_thread']
3933
};
4034

4135
mergeInto(LibraryManager.library, LibraryFetch);

system/lib/fetch/emscripten_fetch.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,35 @@ extern "C" {
1818
// Uncomment the following and clear the cache with emcc --clear-cache to rebuild this file to
1919
// enable internal debugging. #define FETCH_DEBUG
2020

21-
struct __emscripten_fetch_queue {
21+
static void fetch_free(emscripten_fetch_t* fetch);
22+
23+
// APIs defined in JS
24+
void emscripten_start_fetch(emscripten_fetch_t* fetch);
25+
int32_t _emscripten_fetch_get_response_headers_length(int32_t fetchID);
26+
int32_t _emscripten_fetch_get_response_headers(int32_t fetchID, int32_t dst, int32_t dstSizeBytes);
27+
void _emscripten_fetch_free(unsigned int);
28+
29+
struct emscripten_fetch_queue {
2230
emscripten_fetch_t** queuedOperations;
2331
int numQueuedItems;
2432
int queueSize;
2533
};
2634

27-
static void fetch_free(emscripten_fetch_t* fetch);
35+
emscripten_fetch_queue* _emscripten_get_fetch_queue() {
36+
static thread_local emscripten_fetch_queue g_queue;
2837

29-
extern "C" {
30-
void emscripten_start_fetch(emscripten_fetch_t* fetch);
31-
__emscripten_fetch_queue* _emscripten_get_fetch_work_queue();
32-
33-
__emscripten_fetch_queue* _emscripten_get_fetch_queue() {
34-
__emscripten_fetch_queue* queue = _emscripten_get_fetch_work_queue();
35-
if (!queue->queuedOperations) {
36-
queue->queueSize = 64;
37-
queue->numQueuedItems = 0;
38-
queue->queuedOperations =
39-
(emscripten_fetch_t**)malloc(sizeof(emscripten_fetch_t*) * queue->queueSize);
38+
if (!g_queue.queuedOperations) {
39+
g_queue.queueSize = 64;
40+
g_queue.numQueuedItems = 0;
41+
g_queue.queuedOperations =
42+
(emscripten_fetch_t**)malloc(sizeof(emscripten_fetch_t*) * g_queue.queueSize);
4043
}
41-
return queue;
42-
}
43-
44-
int32_t _emscripten_fetch_get_response_headers_length(int32_t fetchID);
45-
int32_t _emscripten_fetch_get_response_headers(int32_t fetchID, int32_t dst, int32_t dstSizeBytes);
46-
void _emscripten_fetch_free(unsigned int);
44+
return &g_queue;
4745
}
4846

4947
void emscripten_proxy_fetch(emscripten_fetch_t* fetch) {
5048
// TODO: mutex lock
51-
__emscripten_fetch_queue* queue = _emscripten_get_fetch_queue();
49+
emscripten_fetch_queue* queue = _emscripten_get_fetch_queue();
5250
// TODO handle case when queue->numQueuedItems >= queue->queueSize
5351
queue->queuedOperations[queue->numQueuedItems++] = fetch;
5452
#ifdef FETCH_DEBUG

0 commit comments

Comments
 (0)