pjfanning opened a new pull request, #3428:
URL: https://github.com/apache/pekko/pull/3428
Assisted by Claude AI
2. ByteStrings.slice creates two intermediate ByteStrings
File: ByteString.scala:1579-1582
```
override def slice(from: Int, until: Int): ByteString =
if (from <= 0 && until >= length) this
else if (from > length || until <= from) ByteString.empty
else drop(from).dropRight(length - until)
```
This allocates an intermediate ByteStrings from drop(from), then allocates
another from dropRight(...). A direct implementation that locates the start/end
fragments and constructs the result in one pass would halve allocations:
```
override def slice(from: Int, until: Int): ByteString = {
val lo = math.max(from, 0)
val hi = math.min(until, length)
if (lo >= hi) ByteString.empty
else if (lo == 0 && hi == length) this
else {
// find start fragment and offset
var fragIdx = 0
var fragOff = lo
while (fragOff >= bytestrings(fragIdx).length) {
fragOff -= bytestrings(fragIdx).length
fragIdx += 1
}
// collect fragments in range
val builder = new VectorBuilder[ByteString1]
var remaining = hi - lo
var firstOff = fragOff
while (remaining > 0) {
val frag = bytestrings(fragIdx)
val take = math.min(remaining, frag.length - firstOff)
builder += (if (firstOff == 0 && take == frag.length) frag
else ByteString1(frag.bytes, frag.startIndex + firstOff,
take))
remaining -= take
fragIdx += 1
firstOff = 0
}
ByteStrings(builder.result(), hi - lo)
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]