From 7a42187ccd7cf0f500b94aae2e4b3867d80d88e1 Mon Sep 17 00:00:00 2001 From: Chris Miller Date: Mon, 3 Dec 2018 19:03:27 -0500 Subject: [PATCH 1/2] Optimize Base64 encoding of large files to be uploaded to avoid GC pauses. --- notebook/static/tree/js/notebooklist.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index 8469caf73..18122aa68 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -1365,18 +1365,25 @@ define([ reader.onerror = on_error; }; + // This approach avoids triggering multiple GC pauses for large files. + // Borrowed from kanaka's answer at: + // https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string + var Uint8ToString = function(u8a){ + var CHUNK_SZ = 0x8000; + var c = []; + for (var i=0; i < u8a.length; i+=CHUNK_SZ) { + c.push(String.fromCharCode.apply(null, u8a.subarray(i, i+CHUNK_SZ))); + } + return c.join(""); + }; + // These codes to upload file in original class var upload_file = function(item, chunk) { var filedata = item.data('filedata'); if (filedata instanceof ArrayBuffer) { // base64-encode binary file data - var bytes = ''; var buf = new Uint8Array(filedata); - var nbytes = buf.byteLength; - for (var i=0; i Date: Mon, 3 Dec 2018 19:04:12 -0500 Subject: [PATCH 2/2] Tune chunk size for large file uploads to be larger for perf gains. --- notebook/static/tree/js/notebooklist.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/notebook/static/tree/js/notebooklist.js b/notebook/static/tree/js/notebooklist.js index 18122aa68..7753a56dd 100644 --- a/notebook/static/tree/js/notebooklist.js +++ b/notebook/static/tree/js/notebooklist.js @@ -1314,7 +1314,8 @@ define([ var parse_large_file = function (f, item) { // codes inspired by https://stackoverflow.com/a/28318964 - var chunk_size = 1024 * 1024; + // 8MB chunk size chosen to match chunk sizes used by benchmark reference (AWS S3) + var chunk_size = 1024 * 1024 * 8; var offset = 0; var chunk = 0; var chunk_reader = null;