Faster IndexOf for substrings (#63285)
authorEgor Bogatov <egorbo@gmail.com>
Tue, 25 Jan 2022 18:54:35 +0000 (21:54 +0300)
committerGitHub <noreply@github.com>
Tue, 25 Jan 2022 18:54:35 +0000 (13:54 -0500)
commit442a42147ef23c3b9742abcd8b997e8f472af68a
treeb132ca32cef577257e734956809eecd61ccb9867
parentaa1b4f8e4a029cea54e2a87c1ff027d5a20e323c
Faster IndexOf for substrings (#63285)

* Improve "lastChar == firstChar" case, also, use IndexOf directly if value.Length == 1

* Try plain IndexOf first, to optimize cases where even first char of value is never met

* add 1-byte implementation

* copyrights

* fix copy-paste mistake

* Initial LastIndexOf impl

* More efficient LastIndexOf

* fix bug in Char version (we need two clear two lowest bits in the mask) & temporarily remove AdvSimd impl

* use ResetLowestSetBit

* Fix bug

* Add two-byte LastIndexOf

* Fix build

* Minor optimizations

* optimize cases with two-byte/two-char values

* Remove gotos, fix build

* fix bug in LastIndexOf

* Make sure String.LastIndexOf is optimized

* Use xplat simd helpers - implicit ARM support

* fix arm

* Delete \

* Use Vector128.IsHardwareAccelerated

* Fix build

* Use IsAllZero

* Address feedback

* Address feedback

* micro-optimization, do-while is better here since mask is guaranteed to be non-zero

* Address feedabc

* Use clever trick I borrowed from IndexOfAny for trailing elements

* give up on +1 bump for SequenceCompare

* Clean up

* Clean up

* fix build

* Add debug asserts

* Clean up: give up on the unrolled trick - too little value from code bloat

* Add a test

* Fix build

* Add byte-specific test

* Fix build

* Update IndexOfSequence.byte.cs
THIRD-PARTY-NOTICES.TXT
src/libraries/System.Memory/tests/Span/IndexOfSequence.byte.cs
src/libraries/System.Memory/tests/Span/IndexOfSequence.char.cs
src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs
src/libraries/System.Private.CoreLib/src/System/Numerics/BitOperations.cs
src/libraries/System.Private.CoreLib/src/System/SpanHelpers.Byte.cs
src/libraries/System.Private.CoreLib/src/System/SpanHelpers.Char.cs
src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs