Skip to content

Commit

Permalink
Replace StringBuilderPool with ValueStringBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Dec 7, 2023
1 parent 1c8a9b9 commit 1a347fd
Show file tree
Hide file tree
Showing 15 changed files with 482 additions and 227 deletions.
10 changes: 5 additions & 5 deletions Jint/Native/Array/ArrayPrototype.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma warning disable CA1859 // Use concrete types when possible for improved performance -- most of prototype methods return JsValue

using System.Linq;
using System.Text;
using Jint.Collections;
using Jint.Native.Iterator;
using Jint.Native.Number;
using Jint.Native.Object;
using Jint.Native.Symbol;
using Jint.Pooling;
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Descriptors.Specialized;
Expand Down Expand Up @@ -1265,15 +1265,15 @@ static string StringFromJsValue(JsValue value)
return s;
}

using var sb = StringBuilderPool.Rent();
sb.Builder.Append(s);
var sb = new ValueStringBuilder(stackalloc char[128]);
sb.Append(s);
for (uint k = 1; k < len; k++)
{
if (sep != "")
{
sb.Builder.Append(sep);
sb.Append(sep);
}
sb.Builder.Append(StringFromJsValue(o.Get(k)));
sb.Append(StringFromJsValue(o.Get(k)));
}

return sb.ToString();
Expand Down
20 changes: 13 additions & 7 deletions Jint/Native/BigInt/BigIntPrototype.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Globalization;
using System.Numerics;
using System.Text;
using Jint.Collections;
using Jint.Native.Object;
using Jint.Native.Symbol;
using Jint.Pooling;
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Interop;
Expand Down Expand Up @@ -114,22 +114,28 @@ private JsValue ToBigIntString(JsValue thisObject, JsValue[] arguments)
value = -value;
}

const string digits = "0123456789abcdefghijklmnopqrstuvwxyz";
const string Digits = "0123456789abcdefghijklmnopqrstuvwxyz";

using var builder = StringBuilderPool.Rent();
var sb = builder.Builder;
var sb = new ValueStringBuilder(stackalloc char[64]);

for (; value > 0; value /= radixMV)
{
var d = (int) (value % radixMV);
sb.Append(digits[d]);
sb.Append(Digits[d]);
}

#if NET6_0_OR_GREATER
var charArray = sb.Length < 512 ? stackalloc char[sb.Length] : new char[sb.Length];
sb.AsSpan().CopyTo(charArray);
charArray.Reverse();
#else
var charArray = new char[sb.Length];
sb.CopyTo(0, charArray, 0, charArray.Length);
sb.AsSpan().CopyTo(charArray);
System.Array.Reverse(charArray);
#endif

return (negative ? "-" : "") + new string(charArray);
var s = new string(charArray);
return negative ? '-' + s : s;
}

/// <summary>
Expand Down
26 changes: 4 additions & 22 deletions Jint/Native/Json/JsonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System.Runtime.InteropServices;
using System.Text;
using Esprima;
using Jint.Native.Object;
using Jint.Pooling;
using Jint.Runtime;

namespace Jint.Native.Json
Expand Down Expand Up @@ -174,7 +172,7 @@ private string ScanPunctuatorValue(int start, char code)

private Token ScanNumericLiteral(ref State state)
{
var sb = state.TokenBuffer;
var sb = new ValueStringBuilder(stackalloc char[128]);
var start = _index;
var ch = _source.CharCodeAt(_index);
var canBeInteger = true;
Expand Down Expand Up @@ -249,7 +247,6 @@ private Token ScanNumericLiteral(ref State state)
}

var number = sb.ToString();
sb.Clear();

JsNumber value;
if (canBeInteger && long.TryParse(number, NumberStyles.Integer, CultureInfo.InvariantCulture, out var longResult) && longResult != -0)
Expand Down Expand Up @@ -312,7 +309,7 @@ private Token ScanStringLiteral(ref State state)
int start = _index;
++_index;

var sb = state.TokenBuffer;
var sb = new ValueStringBuilder(stackalloc char[128]);
while (_index < _length)
{
char ch = _source[_index++];
Expand Down Expand Up @@ -383,7 +380,6 @@ private Token ScanStringLiteral(ref State state)
}

string value = sb.ToString();
sb.Clear();
return CreateToken(Tokens.String, value, '\"', new JsString(value), new TextRange(start, _index));
}

Expand Down Expand Up @@ -704,8 +700,7 @@ public JsValue Parse(string code, ParserOptions? options)
_length = _source.Length;
_lookahead = null!;

using var wrapper = StringBuilderPool.Rent();
State state = new State(wrapper.Builder);
State state = new State();

Peek(ref state);
JsValue jsv = ParseJsonValue(ref state);
Expand All @@ -719,22 +714,9 @@ public JsValue Parse(string code, ParserOptions? options)
return jsv;
}

[StructLayout(LayoutKind.Auto)]
private ref struct State
{
public State(StringBuilder tokenBuffer)
{
TokenBuffer = tokenBuffer;
CurrentDepth = 0;
}

/// <summary>
/// StringBuilder instance which can be used to collect
/// characters into a single string. Must only be used
/// when no child-parser gets called. Must be cleared
/// after usage.
/// </summary>
public StringBuilder TokenBuffer { get; }

/// <summary>
/// The current recursion depth
/// </summary>
Expand Down
10 changes: 6 additions & 4 deletions Jint/Native/Json/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Jint.Native.Object;
using Jint.Native.Proxy;
using Jint.Native.String;
using Jint.Pooling;
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Interop;
Expand Down Expand Up @@ -54,9 +53,9 @@ public JsValue Serialize(JsValue value, JsValue replacer, JsValue space)
var wrapper = _engine.Realm.Intrinsics.Object.Construct(Arguments.Empty);
wrapper.DefineOwnProperty(JsString.Empty, new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable));

using var jsonBuilder = StringBuilderPool.Rent();
var jsonBuilder = new StringBuilder();

var target = new SerializerState(jsonBuilder.Builder);
var target = new SerializerState(jsonBuilder);
if (SerializeJSONProperty(JsString.Empty, wrapper, ref target) == SerializeResult.Undefined)
{
return JsValue.Undefined;
Expand Down Expand Up @@ -196,7 +195,10 @@ private SerializeResult SerializeJSONProperty(JsValue key, JsValue holder, ref S
}

target.DtoaBuilder.Reset();
NumberPrototype.NumberToString(doubleValue, target.DtoaBuilder, target.Json);
var sb = new ValueStringBuilder(stackalloc char[128]);
NumberPrototype.NumberToString(doubleValue, target.DtoaBuilder, ref sb);
target.Json.Append(sb.ToString());

return SerializeResult.NotUndefined;
}

Expand Down
Loading

0 comments on commit 1a347fd

Please sign in to comment.