Skip to content

Commit 92de45e

Browse files
committed
Added more inline hints
1 parent 389d244 commit 92de45e

File tree

7 files changed

+52
-1
lines changed

7 files changed

+52
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
88

99
### Added
1010
- `ValueStringBuilder` constructor can take initial buffer instead of creating it itself.
11+
- More compiler hints for inlining.
1112

1213
## [1.1.0] - 2022-04-16
1314

src/LinkDotNet.StringBuilder/TypedSpanList.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Buffers;
2+
using System.Runtime.CompilerServices;
23

34
namespace LinkDotNet.StringBuilder;
45

@@ -15,6 +16,7 @@ internal ref struct TypedSpanList<T>
1516
/// <summary>
1617
/// Initializes a new instance of the <see cref="TypedSpanList{T}"/> struct.
1718
/// </summary>
19+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1820
public TypedSpanList()
1921
{
2022
buffer = new T[8];
@@ -23,6 +25,7 @@ public TypedSpanList()
2325

2426
public ReadOnlySpan<T> AsSpan => buffer[..count];
2527

28+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2629
public void Add(T value)
2730
{
2831
if (count >= buffer.Length)
@@ -34,6 +37,7 @@ public void Add(T value)
3437
count++;
3538
}
3639

40+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3741
private void Grow(int capacity = 0)
3842
{
3943
var currentSize = buffer.Length;

src/LinkDotNet.StringBuilder/ValueStringBuilder.Append.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,77 @@ public ref partial struct ValueStringBuilder
99
/// Appends the string representation of the boolean to the builder.
1010
/// </summary>
1111
/// <param name="value">Bool value to add.</param>
12+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1213
public void Append(bool value) => Append(value.ToString());
1314

1415
/// <summary>
1516
/// Appends the string representation of the character to the builder.
1617
/// </summary>
1718
/// <param name="value">Integer to add.</param>
19+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1820
public void Append(char value) => AppendSpanFormattable(value);
1921

2022
/// <summary>
2123
/// Appends the string representation of the signed byte to the builder.
2224
/// </summary>
2325
/// <param name="value">Signed byte to add.</param>
26+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2427
public void Append(sbyte value) => AppendSpanFormattable(value);
2528

2629
/// <summary>
2730
/// Appends the string representation of the byte to the builder.
2831
/// </summary>
2932
/// <param name="value">Byte to add.</param>
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3034
public void Append(byte value) => AppendSpanFormattable(value);
3135

3236
/// <summary>
3337
/// Appends the string representation of the short to the builder.
3438
/// </summary>
3539
/// <param name="value">Short to add.</param>
40+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3641
public void Append(short value) => AppendSpanFormattable(value);
3742

3843
/// <summary>
3944
/// Appends the string representation of the integer to the builder.
4045
/// </summary>
4146
/// <param name="value">Integer to add.</param>
47+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4248
public void Append(int value) => AppendSpanFormattable(value);
4349

4450
/// <summary>
4551
/// Appends the string representation of the long integer to the builder.
4652
/// </summary>
4753
/// <param name="value">Long integer to add.</param>
54+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4855
public void Append(long value) => AppendSpanFormattable(value);
4956

5057
/// <summary>
5158
/// Appends the string representation of the float to the builder.
5259
/// </summary>
5360
/// <param name="value">Float to add.</param>
61+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5462
public void Append(float value) => AppendSpanFormattable(value);
5563

5664
/// <summary>
5765
/// Appends the string representation of the double to the builder.
5866
/// </summary>
5967
/// <param name="value">Double to add.</param>
68+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6069
public void Append(double value) => AppendSpanFormattable(value);
6170

6271
/// <summary>
6372
/// Appends the string representation of the decimal to the builder.
6473
/// </summary>
6574
/// <param name="value">Decimal to add.</param>
75+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6676
public void Append(decimal value) => AppendSpanFormattable(value);
6777

6878
/// <summary>
6979
/// Appends a string to the string builder.
7080
/// </summary>
7181
/// <param name="str">String, which will be added to this builder.</param>
82+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7283
public void Append(ReadOnlySpan<char> str)
7384
{
7485
var newSize = str.Length + bufferPosition;
@@ -101,6 +112,7 @@ public void AppendLine(ReadOnlySpan<char> str)
101112
Append(Environment.NewLine);
102113
}
103114

115+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
104116
private void AppendSpanFormattable<T>(T value)
105117
where T : ISpanFormattable
106118
{

src/LinkDotNet.StringBuilder/ValueStringBuilder.AppendJoin.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public void AppendJoin<T>(ReadOnlySpan<char> separator, IEnumerable<T> values)
4242
public void AppendJoin<T>(char separator, IEnumerable<T> values)
4343
=> AppendJoinInternalChar(separator, values);
4444

45+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4546
private void AppendJoinInternalString<T2>(ReadOnlySpan<char> separator, IEnumerable<T2> values)
4647
{
4748
ArgumentNullException.ThrowIfNull(values);
@@ -70,6 +71,7 @@ private void AppendJoinInternalString<T2>(ReadOnlySpan<char> separator, IEnumera
7071
}
7172
}
7273

74+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7375
private void AppendJoinInternalChar<T2>(char separator, IEnumerable<T2> values)
7476
{
7577
ArgumentNullException.ThrowIfNull(values);
@@ -98,6 +100,7 @@ private void AppendJoinInternalChar<T2>(char separator, IEnumerable<T2> values)
98100
}
99101
}
100102

103+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
101104
private void AppendInternal<T>(T value)
102105
{
103106
if (value is ISpanFormattable spanFormattable)

src/LinkDotNet.StringBuilder/ValueStringBuilder.Insert.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace LinkDotNet.StringBuilder;
1+
using System.Runtime.CompilerServices;
2+
3+
namespace LinkDotNet.StringBuilder;
24

35
public ref partial struct ValueStringBuilder
46
{
@@ -7,76 +9,87 @@ public ref partial struct ValueStringBuilder
79
/// </summary>
810
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
911
/// <param name="value">String to insert into this builder.</param>
12+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1013
public void Insert(int index, bool value) => Insert(index, value.ToString());
1114

1215
/// <summary>
1316
/// Insert the string representation of the char to the builder at the given index.
1417
/// </summary>
1518
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
1619
/// <param name="value">String to insert into this builder.</param>
20+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1721
public void Insert(int index, char value) => InsertSpanFormattable(index, value);
1822

1923
/// <summary>
2024
/// Insert the string representation of the signed byte to the builder at the given index.
2125
/// </summary>
2226
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
2327
/// <param name="value">String to insert into this builder.</param>
28+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2429
public void Insert(int index, sbyte value) => InsertSpanFormattable(index, value);
2530

2631
/// <summary>
2732
/// Insert the string representation of the byte to the builder at the given index.
2833
/// </summary>
2934
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
3035
/// <param name="value">String to insert into this builder.</param>
36+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3137
public void Insert(int index, byte value) => InsertSpanFormattable(index, value);
3238

3339
/// <summary>
3440
/// Insert the string representation of the short to the builder at the given index.
3541
/// </summary>
3642
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
3743
/// <param name="value">String to insert into this builder.</param>
44+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3845
public void Insert(int index, short value) => InsertSpanFormattable(index, value);
3946

4047
/// <summary>
4148
/// Insert the string representation of the integer to the builder at the given index.
4249
/// </summary>
4350
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
4451
/// <param name="value">String to insert into this builder.</param>
52+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4553
public void Insert(int index, int value) => InsertSpanFormattable(index, value);
4654

4755
/// <summary>
4856
/// Insert the string representation of the long to the builder at the given index.
4957
/// </summary>
5058
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
5159
/// <param name="value">String to insert into this builder.</param>
60+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5261
public void Insert(int index, long value) => InsertSpanFormattable(index, value);
5362

5463
/// <summary>
5564
/// Insert the string representation of the float to the builder at the given index.
5665
/// </summary>
5766
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
5867
/// <param name="value">String to insert into this builder.</param>
68+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5969
public void Insert(int index, float value) => InsertSpanFormattable(index, value);
6070

6171
/// <summary>
6272
/// Insert the string representation of the double to the builder at the given index.
6373
/// </summary>
6474
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
6575
/// <param name="value">String to insert into this builder.</param>
76+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6677
public void Insert(int index, double value) => InsertSpanFormattable(index, value);
6778

6879
/// <summary>
6980
/// Insert the string representation of the decimal to the builder at the given index.
7081
/// </summary>
7182
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
7283
/// <param name="value">String to insert into this builder.</param>
84+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7385
public void Insert(int index, decimal value) => InsertSpanFormattable(index, value);
7486

7587
/// <summary>
7688
/// Appends the string representation of the boolean to the builder.
7789
/// </summary>
7890
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
7991
/// <param name="value">String to insert into this builder.</param>
92+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8093
public void Insert(int index, ReadOnlySpan<char> value)
8194
{
8295
if (index < 0)
@@ -104,6 +117,7 @@ public void Insert(int index, ReadOnlySpan<char> value)
104117
value.CopyTo(buffer[index..shift]);
105118
}
106119

120+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
107121
private void InsertSpanFormattable<T>(int index, T value)
108122
where T : ISpanFormattable
109123
{

src/LinkDotNet.StringBuilder/ValueStringBuilder.Replace.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Buffers;
2+
using System.Runtime.CompilerServices;
23

34
namespace LinkDotNet.StringBuilder;
45

@@ -9,6 +10,7 @@ public ref partial struct ValueStringBuilder
910
/// </summary>
1011
/// <param name="oldValue">The character to replace.</param>
1112
/// <param name="newValue">The character to replace <paramref name="oldValue"/> with.</param>
13+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1214
public void Replace(char oldValue, char newValue) => Replace(oldValue, newValue, 0, Length);
1315

1416
/// <summary>
@@ -18,6 +20,7 @@ public ref partial struct ValueStringBuilder
1820
/// <param name="newValue">The character to replace <paramref name="oldValue"/> with.</param>
1921
/// <param name="startIndex">The index to start in this builder.</param>
2022
/// <param name="count">The number of characters to read in this builder.</param>
23+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2124
public void Replace(char oldValue, char newValue, int startIndex, int count)
2225
{
2326
if (startIndex < 0)
@@ -48,6 +51,7 @@ public void Replace(char oldValue, char newValue, int startIndex, int count)
4851
/// If <paramref name="newValue"/> is <c>empty</c>, instances of <paramref name="oldValue"/>
4952
/// are removed from this builder.
5053
/// </remarks>
54+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5155
public void Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue)
5256
=> Replace(oldValue, newValue, 0, Length);
5357

@@ -61,6 +65,7 @@ public void Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue)
6165
/// Otherwise the ToString method is called.
6266
/// </remarks>
6367
/// /// <typeparam name="T">Any type.</typeparam>
68+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6469
public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue)
6570
=> ReplaceGeneric(oldValue, newValue, 0, Length);
6671

@@ -76,6 +81,7 @@ public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue)
7681
/// Otherwise the ToString method is called.
7782
/// </remarks>
7883
/// /// <typeparam name="T">Any type.</typeparam>
84+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7985
public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue, int startIndex, int count)
8086
{
8187
if (newValue is ISpanFormattable spanFormattable)
@@ -106,6 +112,7 @@ public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue, int start
106112
/// If <paramref name="newValue"/> is <c>empty</c>, instances of <paramref name="oldValue"/>
107113
/// are removed from this builder.
108114
/// </remarks>
115+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
109116
public void Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue, int startIndex, int count)
110117
{
111118
var length = startIndex + count;

src/LinkDotNet.StringBuilder/ValueStringBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public ref partial struct ValueStringBuilder
1919
/// <summary>
2020
/// Initializes a new instance of the <see cref="ValueStringBuilder"/> struct.
2121
/// </summary>
22+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2223
public ValueStringBuilder()
2324
{
2425
bufferPosition = 0;
@@ -29,6 +30,7 @@ public ValueStringBuilder()
2930
/// Initializes a new instance of the <see cref="ValueStringBuilder"/> struct.
3031
/// </summary>
3132
/// <param name="initialBuffer">Initial buffer for the string builder to begin with.</param>
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3234
public ValueStringBuilder(Span<char> initialBuffer)
3335
{
3436
bufferPosition = 0;
@@ -82,6 +84,7 @@ public ValueStringBuilder(Span<char> initialBuffer)
8284
/// fixed (var* buffer = stringBuilder) { ... }
8385
/// </code>
8486
/// </remarks>
87+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8588
public ref char GetPinnableReference()
8689
{
8790
return ref MemoryMarshal.GetReference(buffer);
@@ -101,6 +104,7 @@ public ref char GetPinnableReference()
101104
/// <remarks>
102105
/// This will not enforce some re-allocation or shrinking of the internal buffer. The size stays the same.
103106
/// </remarks>
107+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
104108
public void Clear()
105109
{
106110
bufferPosition = 0;
@@ -113,6 +117,7 @@ public void Clear()
113117
/// <remarks>
114118
/// If <paramref name="newCapacity"/> is smaller or equal to <see cref="Length"/> nothing will be done.
115119
/// </remarks>
120+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
116121
public void EnsureCapacity(int newCapacity)
117122
{
118123
if (newCapacity < 0)
@@ -134,6 +139,7 @@ public void EnsureCapacity(int newCapacity)
134139
/// <remarks>
135140
/// This method will not affect the internal size of the string.
136141
/// </remarks>
142+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
137143
public void Remove(int startIndex, int length)
138144
{
139145
if (length == 0)
@@ -175,6 +181,7 @@ public void Remove(int startIndex, int length)
175181
/// <param name="word">Word to look for in this string.</param>
176182
/// <param name="startIndex">Index to begin with.</param>
177183
/// <returns>The index of the found <paramref name="word"/> in this string or -1 if not found.</returns>
184+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
178185
public int IndexOf(ReadOnlySpan<char> word, int startIndex)
179186
{
180187
if (startIndex < 0)
@@ -204,6 +211,7 @@ public int IndexOf(ReadOnlySpan<char> word, int startIndex)
204211
/// <param name="word">Word to look for in this string.</param>
205212
/// <param name="startIndex">Index to begin with.</param>
206213
/// <returns>The index of the found <paramref name="word"/> in this string or -1 if not found.</returns>
214+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
207215
public int LastIndexOf(ReadOnlySpan<char> word, int startIndex)
208216
{
209217
if (startIndex < 0)
@@ -227,8 +235,10 @@ public int LastIndexOf(ReadOnlySpan<char> word, int startIndex)
227235
/// <remarks>
228236
/// This method performs an ordinal (case-sensitive and culture-insensitive) comparison.
229237
/// </remarks>
238+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
230239
public bool Contains(ReadOnlySpan<char> word) => IndexOf(word) != -1;
231240

241+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
232242
private void Grow(int capacity = 0)
233243
{
234244
var currentSize = buffer.Length;

0 commit comments

Comments
 (0)