3,860
edits
m (→Q & A) |
m (Lakrits moved page FirefoxOS/MappedArrayBuffer to Firefox OS/MappedArrayBuffer: The official spelling of "Firefox OS" leaves a space between the two parts of the name. It's easier to find a page if the spelling of its name is consistent with th...) |
||
(16 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
== Memory-mapped array buffer for XHR response == | == Memory-mapped array buffer for XHR response == | ||
The feature allows to read data in packaged app by XHR with array buffer type as memory-mapped. | : The feature allows to read data in packaged app by XHR with array buffer type as memory-mapped. It helps to save RAM memory usage, especially for big data files in a packaged app. | ||
: For example, the English word suggestion/auto correction or the JSZhuyin IME database in the keyboard APP are read as array buffer, which are good candidates to use this feature on a small RAM device. | |||
: Main bug for this feature at https://bugzilla.mozilla.org/show_bug.cgi?id=945152 | |||
== Support status == | == Support status == | ||
Line 28: | Line 21: | ||
** Uncompressed | ** Uncompressed | ||
** Aligned to | ** Aligned to | ||
*** 8 bytes minimum, and page size[1] recommended | *** 8 bytes minimum, and page size[1] recommended | ||
*** After [ | *** After [2] landed, page size aligment is mandatory | ||
* If requirements not meet | * If requirements not meet | ||
** Fall back to be normal array buffer | ** Fall back to be normal array buffer | ||
: | : Note: The memory-mapped array buffer is mapped as copy-on-write. If starting/ending position of the file are not page aligned, the contents within the page fragmentation will be initilized to zero for security(this would introduce heap memory overhead for maximum two page size). Although page alignment of target file in the zip may increase the zip package size a little, it saves you one page heap memory overhead. | ||
[[File:Arraybuffer mapping.png|640px]] | |||
:[ | |||
:[1] Page size on 32-bits Linux is 4K bytes. | |||
:[2] https://bugzilla.mozilla.org/show_bug.cgi?id=855669 | |||
== Use it or not? == | |||
To determine whether to use it, you may want to consider these factors: | |||
* RAM size | |||
* Flash size | |||
* File size | |||
** There might be no memory saving when the file size belows 8K bytes. | |||
* File compression ratio | |||
** If both RAM and Flash size are limited, you may or may not want to compress the file, depending on the compression ratio. | |||
== How to use it? == | == How to use it? == | ||
Line 70: | Line 74: | ||
* '''How to know if the array buffer is memory-mapped in an XHR response?''' | * '''How to know if the array buffer is memory-mapped in an XHR response?''' | ||
: The Content-Type header is "application/mem-mapped" in such response. | : The Content-Type header is "application/mem-mapped" in such response. | ||
var xhr = new XMLHttpRequest(); | |||
xhr.onreadystatechange = function() { | |||
if (xhr.readyState == xhr.DONE && xhr.status == 200) { | |||
var ct = xhr.getResponseHeader("Content-Type"); | |||
if (ct.indexOf("mem-mapped") != -1) { | |||
dump("Data is memory-mapped!\n"); | |||
} | |||
} | |||
} | |||
xhr.open('GET', 'database.data'); | |||
xhr.responseType = 'arraybuffer'; | |||
xhr.send() | |||
* '''How to check memory usage?''' | * '''How to check memory usage?''' | ||
Line 108: | Line 125: | ||
* '''Can we write to a memory-mapped array buffer?''' | * '''Can we write to a memory-mapped array buffer?''' | ||
: The array buffer is mapped as copy-on-write by private mapping. If you write to a memory-mapped array buffer, it's not visible to other processes mapping the same file, and the updates are not carried through to the underlying file. However, you should avoid that because it introduces heap memory allocation when writing to the address of each page in the virtual address space. | : The array buffer is mapped as copy-on-write by private mapping. If you write to a memory-mapped array buffer, it's not visible to other processes mapping the same file, and the updates are not carried through to the underlying file. However, you should avoid that because it introduces heap memory allocation when writing to the address of each page in the virtual address space. | ||
* '''Can we pass a memory-mapped array buffer between main/worker thread?''' | |||
: Yes, there are two ways to pass array buffer by postMessage(): | |||
:1. Structured clone | |||
::* The destination array buffer becomes a normal array buffer | |||
worker.postMessage(ab); | |||
:2. Transferable objects | |||
::* Ownership transfered to destination array buffer, still memory-mapped | |||
::* Source array buffer is neutered after transfer | |||
worker.postMessage(ab, [ab]); | |||
* '''Is it possible to apply memory-mapped array buffer on blobs coming from IndexedDB?''' | * '''Is it possible to apply memory-mapped array buffer on blobs coming from IndexedDB?''' | ||
: Yes, there is a follow up at https://bugzilla.mozilla.org/show_bug.cgi?id=988815 | :Yes, there is a follow up at https://bugzilla.mozilla.org/show_bug.cgi?id=988815 |
edits