Skip to content
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
16 changes: 9 additions & 7 deletions Asn1Parser/Asn1Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ namespace SysadminsLV.Asn1Parser;
/// </remarks>
public class Asn1Reader {
// a list of primitive tags. Source: http://en.wikipedia.org/wiki/Distinguished_Encoding_Rules#DER_encoding
static readonly List<Byte> _excludedTags = [ 0, 1, 2, 5, 6, 9, 10, 13 ];
// although we actively do lookups, it is NOT recommended to use sets (HashSet<T>), because at current collection size
// lookups in HashSet are around 3-4x times slower than in plain list.
static readonly List<Byte> _excludedTags = [ 0, 1, 2, 5, 6, 9, 10, 12, 13, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ];
static readonly List<Byte> _multiNestedTypes = [
(Byte)Asn1Type.SEQUENCE,
(Byte)Asn1Type.SEQUENCE | (Byte)Asn1Class.CONSTRUCTED,
(Byte)Asn1Type.SET,
(Byte)Asn1Type.SET | (Byte)Asn1Class.CONSTRUCTED
];
readonly List<Byte> _rawData = [];
readonly Dictionary<Int64, AsnInternalMap> _offsetMap = [];
readonly List<Byte> _multiNestedTypes = [
(Byte)Asn1Type.SEQUENCE,
(Byte)Asn1Type.SEQUENCE | (Byte)Asn1Class.CONSTRUCTED,
(Byte)Asn1Type.SET,
(Byte)Asn1Type.SET | (Byte)Asn1Class.CONSTRUCTED
];
AsnInternalMap currentPosition;
Int32 childCount;

Expand Down
53 changes: 53 additions & 0 deletions tests/Asn1Parser.Tests/CollectionPerfTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Asn1Parser.Tests;
[TestClass]
public class CollectionPerfTests {
static readonly List<Byte> _list = [0, 1, 2, 5, 6, 9, 10, 12, 13, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30];
static readonly HashSet<Byte> _hashSet = [0, 1, 2, 5, 6, 9, 10, 12, 13, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30];
const Int32 ITERATIONS = 1000000;

[TestMethod]
public void TestFirstMatch() {
assertGlobal(0);
}
[TestMethod]
public void TestMiddleMatch() {
assertGlobal(20);
}
[TestMethod]
public void TestLastMatch() {
assertGlobal(30);
}
[TestMethod]
public void TestNoMatch() {
assertGlobal(255);
}

static void assertGlobal(Byte searchByte) {
TimeSpan list = executeAction(_list.Contains, searchByte, ITERATIONS);
TimeSpan hashSet = executeAction(_hashSet.Contains, searchByte, ITERATIONS);
assertListIsFaster(list, hashSet);
}
static void assertListIsFaster(TimeSpan list, TimeSpan hashSet) {
Assert.IsTrue(list < hashSet);
}
//static void assertListIsFaster3xTimes(TimeSpan list, TimeSpan hashSet) {
// Double ratio = hashSet / list;
// Console.WriteLine(ratio);
// Assert.IsTrue(ratio >= 3);
//}
static TimeSpan executeAction(Func<Byte, Boolean> action, Byte searchValue, Int32 iterations) {
var sw = new Stopwatch();
sw.Start();
for (Int32 i = 0; i < iterations; i++) {
action.Invoke(searchValue);
}
sw.Stop();

return sw.Elapsed;
}
}