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

Improve JsonEncoder.EscapeString and fix test warnings for ToString() #2527

Merged
merged 22 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
54 changes: 32 additions & 22 deletions Stack/Opc.Ua.Core/Types/Encoders/JsonEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Xml;

namespace Opc.Ua
Expand All @@ -35,6 +36,7 @@ public class JsonEncoder : IJsonEncoder
private static readonly char s_rightCurlyBrace = '}';
private static readonly char s_leftSquareBracket = '[';
private static readonly char s_rightSquareBracket = ']';
private StringBuilder m_stringBuilder = new StringBuilder();
private Stream m_stream;
private MemoryStream m_memoryStream;
private StreamWriter m_writer;
Expand Down Expand Up @@ -498,38 +500,46 @@ public void PopNamespace()
m_namespaces.Pop();
}

private static readonly char[] m_specialChars = new char[] { s_quotation, s_backslash, '\n', '\r', '\t', '\b', '\f', };
private static readonly char[] m_substitution = new char[] { s_quotation, s_backslash, 'n', 'r', 't', 'b', 'f' };
private static readonly Dictionary<char, string> m_substitution = new Dictionary<char, string>
{
{ s_quotation, "\\\"" },
{ s_backslash, "\\\\" },
{ '\n', "\\n" },
{ '\r', "\\r" },
{ '\t', "\\t" },
{ '\b', "\\b" },
{ '\f', "\\f" }
};

/// <summary>
/// Escapes a string and writes it to the stream.
/// </summary>
/// <param name="value"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void EscapeString(string value)
bhnaphade marked this conversation as resolved.
Show resolved Hide resolved
{
m_stringBuilder.Clear();
m_stringBuilder.EnsureCapacity(value.Length * 2);

foreach (char ch in value)
{
bool found = false;

for (int ii = 0; ii < m_specialChars.Length; ii++)
// Check if ch is present in the dictionary
if (m_substitution.TryGetValue(ch, out string escapeSequence))
{
if (m_specialChars[ii] == ch)
{
m_writer.Write(s_backslash);
m_writer.Write(m_substitution[ii]);
found = true;
break;
}
m_stringBuilder.Append(escapeSequence);
}

if (!found)
else if (ch < 32)
{
if (ch < 32)
{
m_writer.Write("\\u");
m_writer.Write("{0:X4}", (int)ch);
continue;
}

m_writer.Write(ch);
m_stringBuilder.Append("\\u");
m_stringBuilder.Append(((int)ch).ToString("X4", CultureInfo.InvariantCulture));
}
else
{
m_stringBuilder.Append(ch);
}
}

m_writer.Write(m_stringBuilder);
}

private void WriteSimpleField(string fieldName, string value, bool quotes)
Expand Down
178 changes: 178 additions & 0 deletions Tests/Opc.Ua.Core.Tests/Types/Encoders/JsonEncoderBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@
* ======================================================================*/

using System;
using System.Buffers;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Xml;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using FastSerialization;
using Microsoft.Extensions.Logging;
using Microsoft.IO;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using Opc.Ua.Bindings;

namespace Opc.Ua.Core.Tests.Types.Encoders
Expand Down Expand Up @@ -331,4 +340,173 @@ public void GlobalCleanup()
#endregion
}

[TestFixture, Category("JsonEncoder")]
[SetCulture("en-us"), SetUICulture("en-us")]
[NonParallelizable]
[MemoryDiagnoser]
[DisassemblyDiagnoser(printSource: true)]
public class JsonEncoderEscapeStringBenchmark
{

[Params(1,2,3)]
public int StringVariantIndex { get; set; } = 3;

[Benchmark]
public void EscapeStringBenchmark1()
{
m_memoryStream.SetLength(0);
m_memoryStream.Position = 0;
EscapedStringToStream(m_testString);
}
mregen marked this conversation as resolved.
Show resolved Hide resolved

[Test]
[Benchmark]
public void EscapeStringBenchmark2()
{
m_memoryStream.SetLength(0);
m_memoryStream.Position = 0;
EscapeString(m_testString);
}

#region Test Setup
[OneTimeSetUp]
public void OneTimeSetUp()
{
m_memoryManager = new Microsoft.IO.RecyclableMemoryStreamManager();
m_memoryStream = new Microsoft.IO.RecyclableMemoryStream(m_memoryManager);
m_streamWriter = new StreamWriter(m_memoryStream, Encoding.UTF8, m_streamSize, false);
bhnaphade marked this conversation as resolved.
Show resolved Hide resolved
m_testString = "Test string ascii, special characters \n \b and control characters \0 \x04 ␀ ␁ ␂ ␃ ␄";
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
m_streamWriter?.Flush();
var result = Encoding.UTF8.GetString(m_memoryStream.ToArray());
Assert.NotNull(result);

m_streamWriter?.Dispose();
m_streamWriter = null;
m_memoryStream?.Dispose();
m_memoryStream = null;
m_recyclableMemoryStream?.Dispose();
m_recyclableMemoryStream = null;
m_memoryManager = null;
}
#endregion

#region Benchmark Setup
/// <summary>
/// Set up some variables for benchmarks.
/// </summary>
[GlobalSetup]
public void GlobalSetup()
{
m_memoryStream = new MemoryStream();
m_streamWriter = new StreamWriter(m_memoryStream, Encoding.UTF8, m_streamSize, false);

// for validating benchmark tests
switch (StringVariantIndex )
{
case 1: m_testString = "\" \n \r \t \b \f \\"; break;
case 2: m_testString = "\0 \x01 \x02 \x03 \x04"; break;
default: m_testString = "Ascii characters , special characters \n \b & control characters \0 \x04 ␀ ␁ ␂ ␃ ␄"; break;
}
}

[GlobalCleanup]
public void GlobalCleanup()
{
m_streamWriter?.Flush();
m_streamWriter?.Dispose();
m_streamWriter = null;
m_memoryStream?.Dispose();
m_memoryStream = null;
m_recyclableMemoryStream?.Dispose();
m_recyclableMemoryStream = null;
m_memoryManager = null;
}
#endregion

#region Private Methods
private void EscapedStringToStream(string value)
{
foreach (char ch in value)
{
bool found = false;

for (int ii = 0; ii < m_specialChars.Length; ii++)
{
if (m_specialChars[ii] == ch)
{
m_streamWriter.Write('\\');
m_streamWriter.Write(m_substitution[ii]);
found = true;
break;
}
}

if (!found)
{
if (ch < 32)
{
m_streamWriter.Write("\\u");
m_streamWriter.Write("{0:X4}", (int)ch);
continue;
}

m_streamWriter.Write(ch);
}
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void EscapeString(string value)
{
m_stringBuilder.Clear();
m_stringBuilder.EnsureCapacity(value.Length * 2);

foreach (char ch in value)
{
// Check if ch is present in the dictionary
if (m_replace.TryGetValue(ch, out string escapeSequence))
{
m_stringBuilder.Append(escapeSequence);
}
else if (ch < 32)
{
m_stringBuilder.Append("\\u");
m_stringBuilder.Append(((int)ch).ToString("X4", CultureInfo.InvariantCulture));
}
else
{
m_stringBuilder.Append(ch);
}
}
m_streamWriter.Write(m_stringBuilder);
}
#endregion

#region Private Fields
private StringBuilder m_stringBuilder = new StringBuilder();
private static string m_testString;
private Microsoft.IO.RecyclableMemoryStreamManager m_memoryManager;
private Microsoft.IO.RecyclableMemoryStream m_recyclableMemoryStream;
private MemoryStream m_memoryStream;
private StreamWriter m_streamWriter;
private int m_streamSize = 1024;
private static readonly char[] m_specialChars = new char[] { '\"', '\\', '\n', '\r', '\t', '\b', '\f', };
private static readonly char[] m_substitution = new char[] { '\"', '\\', 'n', 'r', 't', 'b', 'f' };
private static readonly Dictionary<char, string> m_replace = new Dictionary<char, string>
{
{ '\"', "\\\"" },
{ '\\', "\\\\" },
{ '\n', "\\n" },
{ '\r', "\\r" },
{ '\t', "\\t" },
{ '\b', "\\b" },
{ '\f', "\\f" }
};
#endregion
}
}
Loading