summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJukka Jylänki <jujjyl@gmail.com>2014-06-05 06:18:42 +0300
committerJukka Jylänki <jujjyl@gmail.com>2014-06-05 06:18:42 +0300
commit7da4dc2f2978579bad0a34d0d9a7215bf6e5e2d2 (patch)
treef271dfb6c8f38dc7fb846cabb9482be69846c433 /src
parent92cab32f5ffcc00779b521588bec62f597c98aa7 (diff)
Convert MEMFS files to typed arrays before storing to IDBFS for better IDBFS performance. Add testing for -s MEMFS_APPEND_TO_TYPED_ARRAYS=1 option.
Diffstat (limited to 'src')
-rw-r--r--src/library_idbfs.js3
-rw-r--r--src/library_memfs.js10
-rw-r--r--src/settings.js4
3 files changed, 15 insertions, 2 deletions
diff --git a/src/library_idbfs.js b/src/library_idbfs.js
index 91015e77..8082c196 100644
--- a/src/library_idbfs.js
+++ b/src/library_idbfs.js
@@ -135,6 +135,9 @@ mergeInto(LibraryManager.library, {
if (FS.isDir(stat.mode)) {
return callback(null, { timestamp: stat.mtime, mode: stat.mode });
} else if (FS.isFile(stat.mode)) {
+ // Performance consideration: storing a normal JavaScript array to a IndexedDB is much slower than storing a typed array.
+ // Therefore always convert the file contents to a typed array first before writing the data to IndexedDB.
+ node.contents = MEMFS.getFileDataAsTypedArray(node);
return callback(null, { timestamp: stat.mtime, mode: stat.mode, contents: node.contents });
} else {
return callback(new Error('node type not supported'));
diff --git a/src/library_memfs.js b/src/library_memfs.js
index 4b56ebbb..df1d306e 100644
--- a/src/library_memfs.js
+++ b/src/library_memfs.js
@@ -98,6 +98,16 @@ mergeInto(LibraryManager.library, {
return node.contents; // No-op, the file contents are already in a JS array. Return as-is.
},
+#if USE_TYPED_ARRAYS == 2
+ // Given a file node, returns its file data converted to a typed array.
+ getFileDataAsTypedArray: function(node) {
+ if (node.contents && node.contents.subarray) return node.contents; // No-op get if data already is in a typed array.
+ var ta = new Uint8Array(new ArrayBuffer(node.usedBytes));
+ for(var i = 0; i < node.usedBytes; ++i) ta[i] = node.contents[i];
+ return ta;
+ },
+#endif
+
// Allocates a new backing store for the given node so that it can fit at least newSize amount of bytes.
// May allocate more, to provide automatic geometric increase and amortized linear performance appending writes.
// Never shrinks the storage.
diff --git a/src/settings.js b/src/settings.js
index 7d9d1b57..a8761d90 100644
--- a/src/settings.js
+++ b/src/settings.js
@@ -324,8 +324,8 @@ var FS_LOG = 0; // Log all FS operations. This is especially helpful when you'r
var CASE_INSENSITIVE_FS = 0; // If set to nonzero, the provided virtual filesystem if treated case-insensitive, like
// Windows and OSX do. If set to 0, the VFS is case-sensitive, like on Linux.
var MEMFS_APPEND_TO_TYPED_ARRAYS = 0; // If set to nonzero, MEMFS will always utilize typed arrays as the backing store
- // for writing to files. The default behavior is to use typed arrays for files
- // when the file size doesn't change (appending writes), and for files that do
+ // for appending data to files. The default behavior is to use typed arrays for files
+ // when the file size doesn't change after initial creation, and for files that do
// change size, use normal JS arrays instead.
var USE_BSS = 1; // https://en.wikipedia.org/wiki/.bss