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

Look for places to collapse AsSpan().Slice(start, length) into .AsSpan(start,length) #25131

Closed
ghost opened this issue Feb 21, 2018 · 9 comments · Fixed by dotnet/corefxlab#2206
Assignees
Labels
area-Meta enhancement Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@ghost
Copy link

ghost commented Feb 21, 2018

No description provided.

@ghost ghost self-assigned this Feb 21, 2018
@tarekgh
Copy link
Member

tarekgh commented Mar 7, 2018

@atsushikan do we still have work to do here? or this is done?

@ghost
Copy link

ghost commented Mar 7, 2018

Work hasn't started on this. (BTW, this work is cleanup and microoptimization rather than "Enhancement.")

@stephentoub
Copy link
Member

A quick search in corefx shows this:

  D:\repos\corefx\src\Common\src\CoreLib\System\String.Manipulation.cs(1139):                    this.AsSpan().Slice(thisIdx, count).CopyTo(dstSpan.Slice(dstIdx));
  D:\repos\corefx\src\Common\src\CoreLib\System\String.Manipulation.cs(1151):            this.AsSpan().Slice(thisIdx).CopyTo(dstSpan.Slice(dstIdx));
  D:\repos\corefx\src\Common\src\CoreLib\System\TimeZoneInfo.Unix.cs(1201):                        if (int.TryParse(dateRule.AsSpan().Slice(1, firstDotIndex - 1), out month) &&
  D:\repos\corefx\src\Common\src\CoreLib\System\TimeZoneInfo.Unix.cs(1202):                            int.TryParse(dateRule.AsSpan().Slice(firstDotIndex + 1, secondDotIndex - firstDotIndex - 1), out week) &&
  D:\repos\corefx\src\Common\src\CoreLib\System\TimeZoneInfo.Unix.cs(1203):                            int.TryParse(dateRule.AsSpan().Slice(secondDotIndex + 1), out int day))
  D:\repos\corefx\src\Common\src\CoreLib\System\Globalization\CompareInfo.cs(509):                string1.AsSpan().Slice(offset1, length1),
  D:\repos\corefx\src\Common\src\CoreLib\System\Globalization\CompareInfo.cs(510):                string2.AsSpan().Slice(offset2, length2),
  D:\repos\corefx\src\Common\src\CoreLib\System\Globalization\CompareInfo.cs(534):            return CompareOrdinalIgnoreCase(strA.AsSpan().Slice(indexA, lengthA), strB.AsSpan().Slice(indexB, lengthB));
  D:\repos\corefx\src\Common\src\CoreLib\System\Globalization\DateTimeParse.cs(5048):                        if (m_info.CompareOptionIgnoreCase(Value.Slice(thisPosition, segmentLength), target.AsSpan().Slice(targetPosition, segmentLength)) != 0)
  D:\repos\corefx\src\Common\src\CoreLib\System\Globalization\DateTimeParse.cs(5074):                    if (m_info.CompareOptionIgnoreCase(Value.Slice(thisPosition, segmentLength), target.AsSpan().Slice(targetPosition, segmentLength)) != 0)
  D:\repos\corefx\src\Common\src\CoreLib\System\Globalization\HijriCalendar.Win32.cs(72):                                int advance = Int32.Parse(str.AsSpan().Slice(HijriAdvanceRegKeyEntry.Length), provider:CultureInfo.InvariantCulture);
  D:\repos\corefx\src\Common\src\CoreLib\System\IO\Path.Windows.cs(86):                combinedPath = Join(GetPathRoot(basePath.AsSpan()), path.AsSpan().Slice(1)); // Cut the separator to ensure we don't end up with two separators when joining with the root.
  D:\repos\corefx\src\Common\src\CoreLib\System\IO\Path.Windows.cs(98):                    combinedPath = Join(basePath, path.AsSpan().Slice(2));
  D:\repos\corefx\src\Common\src\CoreLib\System\IO\Path.Windows.cs(108):                            ? JoinInternal(basePath.AsSpan().Slice(0, 4), path, @"\")
  D:\repos\corefx\src\Common\src\CoreLib\System\IO\Path.Windows.cs(109):                            : JoinInternal(basePath.AsSpan().Slice(0, 4), path.AsSpan().Slice(0, 2), @"\", path.AsSpan().Slice(2));
  D:\repos\corefx\src\Common\src\CoreLib\System\IO\PathHelper.Windows.cs(87):                buffer.Append(content.AsSpan().Slice(PathInternal.UncPrefixLength));
  D:\repos\corefx\src\Common\src\CoreLib\System\IO\PathHelper.Windows.cs(201):                        outputBuilder.Append(inputBuilder.AsSpan().Slice(foundIndex, inputBuilder.Length - foundIndex));
  D:\repos\corefx\src\Common\src\CoreLib\System\IO\PathHelper.Windows.cs(221):            ReadOnlySpan<char> output = builderToUse.AsSpan().Slice(rootDifference);
  D:\repos\corefx\src\Common\src\CoreLib\System\Text\StringBuilder.cs(1555):                            itemFormatSpan = format.AsSpan().Slice(startPos, pos - startPos);
  D:\repos\corefx\src\Common\src\Interop\Unix\System.Security.Cryptography.Native\Interop.OpenSslVersion.cs(26):                    if (!Version.TryParse(OpenSslVersionDescription().AsSpan().Slice(OpenSSL.Length, 5).ToString(), out s_opensslVersion))
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\Asn1V2.Serializer.cs(638):                        valAsMemory.Span.CopyTo(tooBig.AsSpan().Slice(1));
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\Asn1V2.Serializer.cs(884):                                return new ReadOnlyMemory<byte>(rented.AsSpan().Slice(0, bytesWritten).ToArray());
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\Asn1V2.Serializer.cs(915):                                return new ReadOnlyMemory<byte>(rented.AsSpan().Slice(0, bytesWritten).ToArray());
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(102):            if (!tag.TryWrite(_buffer.AsSpan().Slice(_offset, spaceRequired), out int written) ||
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(193):            preEncodedValue.Span.CopyTo(_buffer.AsSpan().Slice(_offset));
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(452):            bitString.CopyTo(_buffer.AsSpan().Slice(_offset));
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(511):                dest = _buffer.AsSpan().Slice(_offset);
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(524):            dest = _buffer.AsSpan().Slice(_offset);
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(658):            octetString.CopyTo(_buffer.AsSpan().Slice(_offset));
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(710):                dest = _buffer.AsSpan().Slice(_offset);
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(719):            dest = _buffer.AsSpan().Slice(_offset);
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(846):                int localLen = EncodeSubIdentifier(tmp.AsSpan().Slice(tmpOffset), ref subIdentifier);
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(852):                    localLen = EncodeSubIdentifier(tmp.AsSpan().Slice(tmpOffset), ref subIdentifier);
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(1124):            Span<byte> baseSpan = _buffer.AsSpan().Slice(_offset);
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(1237):            Span<byte> baseSpan = _buffer.AsSpan().Slice(_offset);
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(1290):            _buffer.AsSpan().Slice(0, _offset).CopyTo(dest);
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(1308):            return _buffer.AsSpan().Slice(0, _offset).ToArray();
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(1475):                    Span<byte> dest = _buffer.AsSpan().Slice(_offset, size);
  D:\repos\corefx\src\Common\src\System\Security\Cryptography\AsnWriter.cs(1520):            WriteConstructedCerOctetString(tag, tmp.AsSpan().Slice(0, size));
  D:\repos\corefx\src\System.Diagnostics.Process\src\System\Diagnostics\Process.Linux.cs(91):                    Double.TryParse(text.AsSpan().Slice(0, length), NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo, out uptimeSeconds);
  D:\repos\corefx\src\System.Private.Xml\src\System\Xml\NameTable.cs(243):            ReadOnlySpan<byte> bytes = key.AsSpan().Slice(start, len).AsBytes();
  D:\repos\corefx\src\System.Runtime.Extensions\src\System\IO\StringReader.cs(141):                _s.AsSpan().Slice(_pos, n).CopyTo(buffer);

@tarekgh
Copy link
Member

tarekgh commented Mar 7, 2018

Work hasn't started on this. (BTW, this work is cleanup and microoptimization rather than "Enhancement.")

that is what meant by "Enhancement".

@stephentoub you marked it as up-for-grabs so we should un-assign @atsushikan if you think someone else will pick it up.

@stephentoub
Copy link
Member

so we should un-assign

Not necessary unless he wants that. This is just the kind of thing that anyone can do; doesn't require Ati's skill set.

@tarekgh
Copy link
Member

tarekgh commented Mar 7, 2018

what I meant, people usually don't pick the assigned issues. anyway, I'll see if I can do this one myself.

@khellang
Copy link
Member

khellang commented Mar 8, 2018

What about shipping an analyzer and code fix for this?

@tarekgh tarekgh assigned tarekgh and unassigned ghost Mar 8, 2018
@ahsonkhan
Copy link
Member

@tarekgh, can you please fix such uses in coreclr as well?

For instance (the only one I could find): https://github.com/dotnet/coreclr/blob/76be5e3928cbfa01e96086e532a6476f1acac293/src/mscorlib/shared/System/IO/PathInternal.cs#L137

@ahsonkhan
Copy link
Member

What about shipping an analyzer and code fix for this?

See dotnet/corefxlab#2206 (experimental, trying it out)

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Meta enhancement Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants