Skip to content

Add ByteString.endsWith and expand startsWith test coverage#2862

Open
pjfanning wants to merge 7 commits intoapache:mainfrom
pjfanning:copilot/improve-tests-for-bytestring
Open

Add ByteString.endsWith and expand startsWith test coverage#2862
pjfanning wants to merge 7 commits intoapache:mainfrom
pjfanning:copilot/improve-tests-for-bytestring

Conversation

@pjfanning
Copy link
Copy Markdown
Member

ByteString had startsWith(Array[Byte]) but no endsWith equivalent, and the existing startsWith tests lacked edge-case coverage.

New API

endsWith(bytes: Array[Byte]): Boolean added to the ByteString base class, mirroring startsWith semantics:

ByteString("abcdefghijklmnopqrstuvwxyz").endsWith("uvwxyz".getBytes) // true
ByteString("abcdefghijklmnopqrstuvwxyz").endsWith("12345".getBytes)  // false
ByteString.empty.endsWith(Array.emptyByteArray)                      // true

Test improvements

  • startsWith: added coverage for empty byte array (always true), exact-length match, bytes longer than the string (false), ByteString1C, and empty ByteString
  • endsWith: full suite covering ByteString1, ByteString1C, ByteStrings (multi-segment, including cross-boundary suffix), ByteString1 with internal offset, empty byte array, exact match, oversized input, and empty ByteString

@pjfanning pjfanning marked this pull request as draft April 15, 2026 09:03
@pjfanning
Copy link
Copy Markdown
Member Author

I intend to look at adding a set of JMH benchmarks.
The existing impl inherited from Seq class involves creating iterators and doing drops and I would expect that this comes with a performance penalty for ByteString.

@pjfanning pjfanning force-pushed the copilot/improve-tests-for-bytestring branch from 50d10f7 to 4c331dc Compare April 17, 2026 09:12
@pjfanning
Copy link
Copy Markdown
Member Author

With Changes
[info] ByteString_startEnd_Benchmark.bs_endsWith         thrpt    3  276333055.676 ±  61641905.180  ops/s
[info] ByteString_startEnd_Benchmark.bs_endsWithBytes    thrpt    3  114538811.656 ± 167288641.049  ops/s
[info] ByteString_startEnd_Benchmark.bs_startsWith       thrpt    3  124530515.986 ±  36356815.658  ops/s
[info] ByteString_startEnd_Benchmark.bs_startsWithBytes  thrpt    3   71968560.978 ± 179926032.403  ops/s
[info] ByteString_startEnd_Benchmark.bss_endsWith        thrpt    3   12830794.623 ±   6215170.619  ops/s
[info] ByteString_startEnd_Benchmark.bss_startsWith      thrpt    3   86003737.814 ± 397051764.052  ops/s

Without Changes
[info] ByteString_startEnd_Benchmark.bs_endsWith         thrpt    3   52301743.336 ± 249800803.546  ops/s
[info] ByteString_startEnd_Benchmark.bs_endsWithBytes    thrpt    3   57212290.541 ±  36873077.965  ops/s
[info] ByteString_startEnd_Benchmark.bs_startsWith       thrpt    3    1865819.744 ±   8946244.499  ops/s
[info] ByteString_startEnd_Benchmark.bs_startsWithBytes  thrpt    3   47994920.794 ± 195723663.984  ops/s
[info] ByteString_startEnd_Benchmark.bss_endsWith        thrpt    3     295568.162 ±   1047083.734  ops/s
[info] ByteString_startEnd_Benchmark.bss_startsWith      thrpt    3    2736217.171 ±  10193061.120  ops/s

@pjfanning pjfanning marked this pull request as ready for review April 17, 2026 09:37
@pjfanning pjfanning added this to the 2.0.0-M2 milestone Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants