Skip to content

Commit

Permalink
make custom BinaryWriter internal, rename, move into proper class and…
Browse files Browse the repository at this point in the history
… folder name
  • Loading branch information
hybridherbst committed May 9, 2022
1 parent c8349b7 commit 513e80c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("UnityGLTFScripts")]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@

namespace UnityGLTF
{
// It seems that BinaryWriter creates a ton of garbage: the implementation allocates a byte[4] for every float write (and for other data types as well)
// It seems that BinaryWriter creates a ton of garbage: the implementation allocates a byte[4] for every float write. (and for double as well)
// foreach (var v in arr)
// {
// _bufferWriter.Write(v); // this will allocate 36B per call
// _bufferWriter.Write(v); // this will allocate 36B per call
// }
// For large accessors, this adds up to hundreds of megabytes of garbage.
// This class adds special implementations that skip BitConverterLE and instead convert with the same logic,
// differentiating between LittleEndian and BigEndian.

// Following https://github.com/mono/mono/blob/4a5ffcabd58d6439e60126f46e0063bcf30e7a47/mcs/class/referencesource/mscorlib/system/io/binarywriter.cs#L381
// Here's the 4-byte allocation we were seeing: https://github.com/mono/mono/blob/4a5ffcabd58d6439e60126f46e0063bcf30e7a47/mcs/class/System.ServiceModel/Mono.Security.Protocol.Ntlm/BitConverterLE.cs#L127
// Here's the 4-byte allocation we're seeing: https://github.com/mono/mono/blob/4a5ffcabd58d6439e60126f46e0063bcf30e7a47/mcs/class/System.ServiceModel/Mono.Security.Protocol.Ntlm/BitConverterLE.cs#L127
// Another discussion of this problem: https://forum.unity.com/threads/binarywriter-floats.1108478/
// The implementations here pull BitConverter.IsLittleEndian out of the loop and directly write converted bytes into the BinaryWriter.
public class FastBinaryWriter : BinaryWriter
internal class BinaryWriterWithLessAllocations : BinaryWriter
{
private static readonly byte[] _buffer = new byte[16]; // temp space for writing primitives to.

public FastBinaryWriter(Stream binStream) : base(binStream) { }
public BinaryWriterWithLessAllocations(Stream binStream) : base(binStream) { }

public unsafe void Write(float[] value)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "UnityGLTF.Helpers",
"rootNamespace": "",
"references": ["GUID:343deaaf83e0cee4ca978e7df0b80d21","GUID:2bafac87e7f4b9b418d9448d219b01ab"],
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": true,
"overrideReferences": false,
"overrideReferences": true,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private struct ImageInfo
private BufferId _bufferId;
private GLTFBuffer _buffer;
#if USE_FAST_BINARY_WRITER
private FastBinaryWriter _bufferWriter;
private BinaryWriterWithLessAllocations _bufferWriter;
#else
private BinaryWriter _bufferWriter;
#endif
Expand Down Expand Up @@ -464,7 +464,7 @@ public void SaveGLBToStream(Stream stream, string sceneName)
Stream jsonStream = new MemoryStream();
_shouldUseInternalBufferForImages = true;

_bufferWriter = new FastBinaryWriter(binStream);
_bufferWriter = new BinaryWriterWithLessAllocations(binStream);

TextWriter jsonWriter = new StreamWriter(jsonStream, Encoding.ASCII);
exportGltfInitMarker.End();
Expand Down Expand Up @@ -611,7 +611,7 @@ public void SaveGLTFandBin(string path, string fileName)
Directory.CreateDirectory(dirName);

var binFile = File.Create(fullPath);
_bufferWriter = new FastBinaryWriter(binFile);
_bufferWriter = new BinaryWriterWithLessAllocations(binFile);
exportGltfInitMarker.End();

_root.Scene = ExportScene(fileName, _rootTransforms);
Expand Down

0 comments on commit 513e80c

Please sign in to comment.