Skip to content

Commit 36ff5ff

Browse files
committed
Optimize BiteArray convertors
1 parent c3f740a commit 36ff5ff

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

Writer/MzMlSpectrumWriter.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,21 +2455,29 @@ private ScanListType ConstructScanList(int scanNumber, Scan scan)
24552455
/// <returns>the byte array</returns>
24562456
private static byte[] Get64BitArray(ICollection<double> array)
24572457
{
2458-
byte[] bytes;
2458+
Array bytes = Array.CreateInstance(typeof(byte), array.Count * sizeof(double));
24592459

2460-
using (var memoryStream = new MemoryStream())
2460+
if (BitConverter.IsLittleEndian) //should be for most modern systems, but maybe..
2461+
{
2462+
Buffer.BlockCopy(array.ToArray(), 0, bytes, 0, bytes.Length); //copy the buffer directly
2463+
}
2464+
else //swap byte order of all elements in array
24612465
{
2462-
foreach (var doubleValue in array)
2466+
using (var memoryStream = new MemoryStream())
24632467
{
2464-
var doubleValueByteArray = BitConverter.GetBytes(doubleValue);
2465-
memoryStream.Write(doubleValueByteArray, 0, doubleValueByteArray.Length);
2466-
}
2468+
foreach (var doubleValue in array)
2469+
{
2470+
var doubleValueByteArray = BitConverter.GetBytes(doubleValue);
2471+
Array.Reverse(doubleValueByteArray);
2472+
memoryStream.Write(doubleValueByteArray, 0, doubleValueByteArray.Length);
2473+
}
24672474

2468-
memoryStream.Position = 0;
2469-
bytes = memoryStream.ToArray();
2470-
}
2475+
memoryStream.Position = 0;
2476+
bytes = memoryStream.ToArray();
2477+
}
2478+
}
24712479

2472-
return bytes;
2480+
return (byte[])bytes;
24732481
}
24742482

24752483
/// <summary>
@@ -2482,17 +2490,13 @@ private static byte[] GetZLib64BitArray(ICollection<double> array)
24822490
// zero length array encoded by GZip produces non-zero length array; some downstream tools do not like it
24832491
if (array.Count == 0) return new byte[0];
24842492

2485-
byte[] bytes;
2493+
byte[] bytes = Get64BitArray(array);
24862494

24872495
using (var memoryStream = new MemoryStream())
24882496
using (var outZStream = new ZOutputStream(memoryStream, zlibConst.Z_DEFAULT_COMPRESSION))
24892497
{
2490-
foreach (var doubleValue in array)
2491-
{
2492-
var doubleValueByteArray = BitConverter.GetBytes(doubleValue);
2493-
outZStream.Write(doubleValueByteArray, 0, doubleValueByteArray.Length);
2494-
}
2495-
2498+
outZStream.Write(bytes, 0, bytes.Length);
2499+
24962500
outZStream.finish();
24972501
memoryStream.Position = 0;
24982502
bytes = memoryStream.ToArray();

Writer/SpectrumWriter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using ThermoFisher.CommonCore.Data.Business;
88
using ThermoFisher.CommonCore.Data.FilterEnums;
99
using ThermoFisher.CommonCore.Data.Interfaces;
10+
using ThermoRawFileParser.Util;
1011

1112
namespace ThermoRawFileParser.Writer
1213
{
@@ -41,7 +42,7 @@ public abstract class SpectrumWriter : ISpectrumWriter
4142
/// <summary>
4243
/// Precursor cache
4344
/// </summary>
44-
private static Util.LimitedSizeDictionary<int, Scan> precursorCache;
45+
private static LimitedSizeDictionary<int, Scan> precursorCache;
4546

4647
/// <summary>
4748
/// Constructor.
@@ -50,7 +51,7 @@ public abstract class SpectrumWriter : ISpectrumWriter
5051
protected SpectrumWriter(ParseInput parseInput)
5152
{
5253
ParseInput = parseInput;
53-
precursorCache = new Util.LimitedSizeDictionary<int, Scan>(10);
54+
precursorCache = new LimitedSizeDictionary<int, Scan>(10);
5455
}
5556

5657
/// <inheritdoc />

0 commit comments

Comments
 (0)