UnityGLTF icon indicating copy to clipboard operation
UnityGLTF copied to clipboard

Stream allocation creating duplicate copy of data

Open calebcannon opened this issue 8 years ago • 3 comments

The DownloadHandlerBuffer implements GetData as a copy operation, so this line is creating two copies of the downloaded model in memory

https://github.com/KhronosGroup/UnityGLTF/blob/21f73d6a68376d22c90cc73fda2a761809a3d30f/UnityGLTF/Assets/UnityGLTF/Scripts/Loader/WebRequestLoader.cs#L65

The documentation for DownloadHandlerBuffer.GetData is here

https://docs.unity3d.com/ScriptReference/Networking.DownloadHandlerBuffer.GetData.html

calebcannon avatar Nov 02 '18 21:11 calebcannon

Thanks for making us aware of this issue - do you have a recommendation for how we can avoid this?

ericob avatar Nov 05 '18 20:11 ericob

In this case the most straightforward way is to copy the data once:

        byte[] downloadedBytes = www.downloadHandler.data;
        LoadedStream = new MemoryStream(downloadedBytes, 0, downloadedBytes.Length, true, true);

My colleague is also working on an alternate direct-to-disk implementation of ILoader that can be used to create a file stream and thus avoid download data in memory entirely. If there's interest we can contribute it back when it's ready.

calebcannon avatar Nov 05 '18 21:11 calebcannon

Please update to the latest version and open a new issue if the problem persists. Thanks!

robertdorn83 avatar Feb 09 '24 09:02 robertdorn83