Yeah, the marked line does create a clone of the subarray memory. The only
case where a memory copy is not made is if canOwn was set by caller, which
tells that the write function is allowed to just grab the passed in typed
array as is. If you're trying to optimize out copying, see the call stack
to check if you might be able to get the canOwn field set to true by the
caller, which woud avoid copying.

2016-07-12 14:18 GMT+03:00 awt <[email protected]>:

> jj,
>
> I was looking thru the code and realize that FS_createDataFile will use
> the write method of MEMFS (for my case) to store the data:
>
> write:function (stream, buffer, offset, length, position, canOwn) {
>           if (!length) return 0;
>           var node = stream.node;
>           node.timestamp = Date.now();
>
>           if (buffer.subarray && (!node.contents ||
> node.contents.subarray)) { // This write is from a typed array to a typed
> array?
>             if (canOwn) { // Can we just reuse the buffer we are given?
>               assert(position === 0, 'canOwn must imply no weird position
> inside the file');
>               node.contents = buffer.subarray(offset, offset + length);
>               node.usedBytes = length;
>               return length;
>             } else if (node.usedBytes === 0 && position === 0) { // If
> this is a simple first write to an empty file, do a fast set since we don't
> need to care about old data.
>               node.contents = new Uint8Array(buffer.subarray(offset,
> offset + length));
>               node.usedBytes = length;
>               return length;
>             } else if (position + length <= node.usedBytes) { // Writing
> to an already allocated and used subrange of the file?
>               node.contents.set(buffer.subarray(offset, offset + length),
> position);
>               return length;
>             }
>           }
>
> Since the constructor of Uint8Array is taking in another typedarray
> because of the subarray call, am I right to say that we are actually
> copying the buffer into a new Uint8Array which is then assigned to
> node.contents rather than assigning it to the 'backing store of that
> function' like you mentioned previously? Therefore, this is the same as
> cloning the typed array?
>
> On Friday, August 1, 2014 at 3:58:15 PM UTC+8, jj wrote:
>>
>> Make sure that 'data' comes as a typed array and not e.g. a string. The
>> FS_createDataFile will end up in to library_memfs.js write() function.
>> Debug through that one to ensure that the implementation will not clone the
>> typed array, but just assigns that typed array into the backing store of
>> that function. In that case, the flow should be pretty much as efficient as
>> possible via the filesystem.
>>
>> Note that one shortcoming is that using fread() etc. will cause a jump
>> out of asm.js every time it is called. Therefore fread()ing small amounts
>> of bytes at a time can be slow. Either make sure you have a large reads, or
>> perhaps give a shot at mmap() which will memcpy the typed array into the
>> asm.js HEAP.
>>
>> The most non-intervening route is to allocate/_malloc the memory space in
>> the HEAP, and place the XHR data into the allocated space in HEAP, and then
>> pass a pointer to C code. That gives you a direct C memory access to the
>> data, that you can free() afterwards. This is pretty much what the existing
>> Emscripten wget functions already do.
>>
>>
>> 2014-08-01 6:09 GMT+03:00 caiiiycuk <[email protected]>:
>>
>>> Hi. I have 3d engine that compiled with emscripten. This engine take
>>> models from binary data and then render it. Render cycle looks like:
>>>
>>> loadModels(uint8_t* data, size_t len);
>>> eachFrame -> render();
>>>
>>> I want to query data from web server. What is the best way to pass
>>> binary data to c++ code. For now i use FS api for that. It is look like:
>>>
>>> $.ajax({
>>>             url: "/raw/" + left + "/" + top + "/" + right + "/" + bottom,
>>>             type: 'GET',
>>>             beforeSend: function (xhr) {
>>>               xhr.overrideMimeType("text/plain; charset=x-user-defined");
>>>             },
>>>             success: function( data ) {
>>>               Module['FS_createDataFile']("/tmp", "memfile", data, true,
>>> true);
>>>               Module.ccall('readModels', 'void', ['string'],
>>> ["/tmp/memfile"]);
>>>             }
>>>           });
>>>
>>> readModels(const char* file) {
>>>     FILE *file = fopen(filePath, "rb");
>>>     //... do parse stuff
>>>     fclose(file);
>>>     remove(filePath);
>>> }
>>>
>>> It this method effective?
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "emscripten-discuss" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to