Skip to content

Commit 45f1142

Browse files
authored
Reduce memory allocation (mini-software#427)
* Reduce memory allocation when using MemoryStream * Add System.Memory pacakge * Reduce memory allocation in GetImageFormat()
1 parent 2e20881 commit 45f1142

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

src/MiniExcel/MiniExcelLibs.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@ Todo : https://github.com/shps951023/MiniExcel/projects/1?fullscreen=true
4242
<ItemGroup>
4343
<None Include="icon.png" Pack="true" PackagePath="\" />
4444
</ItemGroup>
45+
<ItemGroup Condition="'$(TargetFramework)' == 'net45' Or '$(TargetFramework)' == 'netstandard2.0'">
46+
<PackageReference Include="System.Memory" Version="4.5.5" />
47+
</ItemGroup>
4548
</Project>

src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,12 +678,12 @@ private void ConvertCellValue(string rawValue, string aT, int xfIndex, out objec
678678
if (v != null && v.StartsWith("@@@fileid@@@,",StringComparison.Ordinal))
679679
{
680680
var path = v.Substring(13);
681-
var stream = _archive.GetEntry(path).Open();
682-
byte[] bytes;
683-
using (var ms = new MemoryStream())
681+
var entry = _archive.GetEntry(path);
682+
byte[] bytes = new byte[entry.Length];
683+
using (var stream = entry.Open())
684+
using (var ms = new MemoryStream(bytes))
684685
{
685686
stream.CopyTo(ms);
686-
bytes = ms.ToArray();
687687
}
688688
value = bytes;
689689
}

src/MiniExcel/Utils/ImageHelper.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace MiniExcelLibs.Utils
22
{
3+
using System;
34
using System.Linq;
45
using System.Text;
56

@@ -15,36 +16,35 @@ public enum ImageFormat
1516
unknown
1617
}
1718

18-
public static ImageFormat GetImageFormat(byte[] bytes)
19+
public static ImageFormat GetImageFormat(ReadOnlySpan<byte> bytes)
1920
{
20-
// see http://www.mikekunz.com/image_file_header.html
21-
var bmp = Encoding.ASCII.GetBytes("BM"); // BMP
22-
var gif = Encoding.ASCII.GetBytes("GIF"); // GIF
23-
var png = new byte[] { 137, 80, 78, 71 }; // PNG
24-
var tiff = new byte[] { 73, 73, 42 }; // TIFF
25-
var tiff2 = new byte[] { 77, 77, 42 }; // TIFF
26-
var jpeg = new byte[] { 255, 216, 255, 224 }; // jpeg
27-
var jpeg2 = new byte[] { 255, 216, 255, 225 }; // jpeg canon
28-
29-
if (bmp.SequenceEqual(bytes.Take(bmp.Length)))
21+
ReadOnlySpan<byte> bmp = stackalloc byte[] { (byte)'B', (byte)'M' }; // BMP
22+
ReadOnlySpan<byte> gif = stackalloc byte[] { (byte)'G', (byte)'I', (byte)'F' }; // GIF
23+
ReadOnlySpan<byte> png = stackalloc byte[] { 137, 80, 78, 71 }; // PNG
24+
ReadOnlySpan<byte> tiff = stackalloc byte[] { 73, 73, 42 }; // TIFF
25+
ReadOnlySpan<byte> tiff2 = stackalloc byte[] { 77, 77, 42 }; // TIFF
26+
ReadOnlySpan<byte> jpeg = stackalloc byte[] { 255, 216, 255, 224 }; // jpeg
27+
ReadOnlySpan<byte> jpeg2 = stackalloc byte[] { 255, 216, 255, 225 }; // jpeg canon
28+
29+
if (bytes.StartsWith(bmp))
3030
return ImageFormat.bmp;
3131

32-
if (gif.SequenceEqual(bytes.Take(gif.Length)))
32+
if (bytes.StartsWith(gif))
3333
return ImageFormat.gif;
3434

35-
if (png.SequenceEqual(bytes.Take(png.Length)))
35+
if (bytes.StartsWith(png))
3636
return ImageFormat.png;
3737

38-
if (tiff.SequenceEqual(bytes.Take(tiff.Length)))
38+
if (bytes.StartsWith(tiff))
3939
return ImageFormat.tiff;
4040

41-
if (tiff2.SequenceEqual(bytes.Take(tiff2.Length)))
41+
if (bytes.StartsWith(tiff2))
4242
return ImageFormat.tiff;
4343

44-
if (jpeg.SequenceEqual(bytes.Take(jpeg.Length)))
44+
if (bytes.StartsWith(jpeg))
4545
return ImageFormat.jpg;
4646

47-
if (jpeg2.SequenceEqual(bytes.Take(jpeg2.Length)))
47+
if (bytes.StartsWith(jpeg2))
4848
return ImageFormat.jpg;
4949

5050
return ImageFormat.unknown;

0 commit comments

Comments
 (0)