DataObjects-NET / dataobjects-net

https://dataobjects.net
MIT License
61 stars 25 forks source link

`internal struct ValueStringBuilder`; Optimizations of string concatenations #322

Closed SergeiPavlov closed 1 year ago

SergeiPavlov commented 1 year ago

The class is copy/pasted from .NET source code

It is recommended by:

image https://www.cnblogs.com/InCerry/p/Dotnet-Perf-Opt-Use-ValueStringBuilder.html

alex-kulakov commented 1 year ago

It doesn't build properly.

I offer to make following changes of two methods in ValueStringBuilder to at least fix build

    private void AppendSlow(string s)
    {
      int pos = _pos;
      if (pos > _chars.Length - s.Length) {
        Grow(s.Length);
      }
#if NET6_0_OR_GREATER
      s.CopyTo(_chars.Slice(pos));
#else
      s.AsSpan().CopyTo(_chars.Slice(pos));
#endif
      _pos += s.Length;
    }

and

    public void Insert(int index, string? s)
    {
      if (s == null) {
        return;
      }

      int count = s.Length;

      if (_pos > (_chars.Length - count)) {
        Grow(count);
      }

#if NET6_0_OR_GREATER
      int remaining = _pos - index;
      _chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count));
      s.CopyTo(_chars.Slice(index));
      _pos += count;
#else
      int remaining = _pos - index;
      _chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count));
      s.AsSpan().CopyTo(_chars.Slice(index));
      _pos += count;
#endif
    }

I postpone merging my PR with migration to NET6 and NET7 to not resolve conflicts in your PRs, after them I will adapt solution to not have #if NET6_0_OR_GREATER.

SergeiPavlov commented 1 year ago

Fixed .NET5 build