Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce array allocations by usage MemoryStream.GetBuffer() instead of ToArray() #1435

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main/DDF/EscherBSERecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public override String ToString()
{
HexDump.Dump(this._remainingData, 0, b, 0);
//extraData = b.ToString();
extraData = Encoding.UTF8.GetString(b.ToArray());
extraData = Encoding.UTF8.GetString(b.GetBuffer(), 0, (int)b.Length);
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion main/DDF/EscherBlipWMFRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public override String ToString()
{
HexDump.Dump(this.field_12_data, 0, b, 0);
//extraData = b.ToString();
extraData = Encoding.UTF8.GetString(b.ToArray());
extraData = Encoding.UTF8.GetString(b.GetBuffer(), 0, (int)b.Length);
}
catch (Exception e)
{
Expand Down
14 changes: 8 additions & 6 deletions main/HPSF/MutableSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,23 +426,25 @@ public int Write(Stream out1)
propertyListStream.Flush();

/* Write the section: */
byte[] pb1 = propertyListStream.ToArray();
byte[] pb2 = propertyStream.ToArray();
byte[] pb1 = propertyListStream.GetBuffer();
byte[] pb2 = propertyStream.GetBuffer();
int pb1Length = (int)propertyListStream.Length;
int pb2Length = (int)propertyStream.Length;

/* Write the section's Length: */
TypeWriter.WriteToStream(out1, LittleEndianConsts.INT_SIZE * 2 +
pb1.Length + pb2.Length);
pb1Length + pb2Length);

/* Write the section's number of properties: */
TypeWriter.WriteToStream(out1, PropertyCount);

/* Write the property list: */
out1.Write(pb1, 0, pb1.Length);
out1.Write(pb1, 0, pb1Length);

/* Write the properties: */
out1.Write(pb2, 0, pb2.Length);
out1.Write(pb2, 0, pb2Length);

int streamLength = LittleEndianConsts.INT_SIZE * 2 + pb1.Length + pb2.Length;
int streamLength = LittleEndianConsts.INT_SIZE * 2 + pb1Length + pb2Length;
return streamLength;
}
}
Expand Down
5 changes: 2 additions & 3 deletions main/HSSF/Record/Crypto/Biff8EncryptionKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,9 @@ internal RC4 CreateRC4(int keyBlockNo)
using (MemoryStream baos = RecyclableMemory.GetStream(4))
{
new LittleEndianOutputStream(baos).WriteInt(keyBlockNo);
byte[] baosToArray = baos.ToArray();
byte[] data = new byte[baosToArray.Length + _keyDigest.Length];
byte[] data = new byte[(int)baos.Length + _keyDigest.Length];
Array.Copy(_keyDigest, 0, data, 0, _keyDigest.Length);
Array.Copy(baosToArray, 0, data, _keyDigest.Length, baosToArray.Length);
Array.Copy(baos.GetBuffer(), 0, data, _keyDigest.Length, (int)baos.Length);

byte[] digest = md5.ComputeHash(data);
return new RC4(digest);
Expand Down
5 changes: 2 additions & 3 deletions main/POIFS/Storage/HeaderBlockWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void WriteBlock(ByteBuffer block)
{
_header_block.WriteData(ms);

block.Write(ms.ToArray());
block.Write(ms.GetBuffer(), 0, (int)ms.Length);
}
}

Expand All @@ -180,8 +180,7 @@ public void WriteBlock(byte[] block)
{
_header_block.WriteData(ms);

byte[] temp = ms.ToArray();
Array.Copy(temp, 0, block, 0, temp.Length);
Array.Copy(ms.GetBuffer(), 0, block, 0, (int)ms.Length);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions main/Util/HexDump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ public static void Dump(Stream inStream, int start, int bytesToDump)
stream.WriteByte((byte)c);
}
}
byte[] data = stream.ToArray();
Dump(data, 0L, null, start, data.Length);
Dump(stream.GetBuffer(), 0L, null, start, (int)stream.Length);
}
}

Expand Down Expand Up @@ -318,8 +317,7 @@ public static void Dump(Stream in1, Stream out1, int start, int bytesToDump )
}
}

byte[] data = buf.ToArray();
Dump(data, 0, out1, start, data.Length);
Dump(buf.GetBuffer(), 0, out1, start, (int)buf.Length);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion ooxml/POIFS/Crypt/Agile/AgileEncryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ protected void marshallEncryptionDocument(EncryptionDocument ed, LittleEndianByt
byte[] buf = Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n");
bos.Write(buf, 0, buf.Length);
ed.Save(bos);
os.Write(bos.ToArray());
os.Write(bos.GetBuffer(), 0, (int)bos.Length);
} catch (IOException e) {
throw new EncryptedDocumentException("error marshalling encryption info document", e);
}
Expand Down
2 changes: 1 addition & 1 deletion openxml4Net/OPC/OPCPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ public PackagePart CreatePart(PackagePartName partName, String contentType,
return null;
}

partOutput.Write(content.ToArray(), 0, (int)content.Length);
partOutput.Write(content.TryGetBuffer(out var buf) ? buf.Array : content.ToArray(), 0, (int)content.Length);
partOutput.Close();

}
Expand Down
Loading