Copilot commented on code in PR #2409: URL: https://github.com/apache/pekko/pull/2409#discussion_r2473986166
########## stream/src/main/scala/org/apache/pekko/stream/impl/io/compression/ZstdCompressor.scala: ########## @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.stream.impl.io.compression + +import java.nio.ByteBuffer + +import com.github.luben.zstd.{ Zstd, ZstdDictCompress, ZstdDirectBufferCompressingStreamNoFinalizer } + +import org.apache.pekko +import pekko.annotation.InternalApi +import pekko.io.ByteBufferCleaner +import pekko.util.ByteString + +/** INTERNAL API */ +@InternalApi private[pekko] class ZstdCompressor( + compressionLevel: Int = + Zstd.defaultCompressionLevel(), dictionary: Option[ZstdDictCompress] = None) extends Compressor { + + private val targetBuffer = ByteBuffer.allocateDirect(65536) + private val compressingStream = new ZstdDirectBufferCompressingStreamNoFinalizer(targetBuffer, compressionLevel) + + dictionary.foreach(compressingStream.setDict) + + override def compress(input: ByteString): ByteString = { + val inputBB = ByteBuffer.allocateDirect(input.size) + inputBB.put(input.toArrayUnsafe()) + inputBB.flip() + compressingStream.compress(inputBB) + val result = ByteString.fromByteBuffer(targetBuffer) + targetBuffer.flip() + if (ByteBufferCleaner.isSupported) + ByteBufferCleaner.clean(inputBB) + result + } + + override def flush(): ByteString = { + targetBuffer.flip() + val result = ByteString.fromByteBuffer(targetBuffer) + targetBuffer.clear() + compressingStream.flush() + result + } + + override def finish(): ByteString = { + compressingStream.close() + targetBuffer.flip() + val arr = Array.ofDim[Byte](targetBuffer.limit()) + targetBuffer.get(arr) + val result = ByteString.fromArrayUnsafe(arr) + if (ByteBufferCleaner.isSupported) + ByteBufferCleaner.clean(targetBuffer) + result + } + + override def compressAndFlush(input: ByteString): ByteString = { + val inputBB = ByteBuffer.allocateDirect(input.size) + inputBB.put(input.toArray) Review Comment: Inconsistent use of `toArray` instead of `toArrayUnsafe()`. Line 41 uses `toArrayUnsafe()` in the `compress` method, but this method uses `toArray` which creates a defensive copy. For consistency and performance, use `toArrayUnsafe()` here as well, matching the pattern in line 41. ########## stream/src/main/scala/org/apache/pekko/stream/impl/io/compression/ZstdDecompressor.scala: ########## @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.stream.impl.io.compression + +import java.nio.ByteBuffer + +import org.apache.pekko +import pekko.annotation.InternalApi +import pekko.io.ByteBufferCleaner +import pekko.stream.Attributes +import pekko.stream.impl.fusing.GraphStages.SimpleLinearGraphStage +import pekko.stream.stage.{ GraphStageLogic, InHandler, OutHandler } +import pekko.util.ByteString + +import com.github.luben.zstd.ZstdDirectBufferDecompressingStreamNoFinalizer + +/** INTERNAL API */ +@InternalApi private[pekko] class ZstdDecompressor(maxBytesPerChunk: Int) extends SimpleLinearGraphStage[ByteString] { + + override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = { + new GraphStageLogic(shape) with InHandler with OutHandler { + + private val sourceBuffer = ByteBuffer.allocateDirect(maxBytesPerChunk) + + // This is initialized here to avoid an allocation per onPush, remember to clear before using + private val outputBuffer = ByteBuffer.allocateDirect(maxBytesPerChunk) + private val decompressingStream = new ZstdDirectBufferDecompressingStreamNoFinalizer(sourceBuffer) + + override def onPush(): Unit = { + sourceBuffer.clear() + val inputArray = grab(in).toArrayUnsafe() + sourceBuffer.put(inputArray) + sourceBuffer.flip() + if (sourceBuffer.hasRemaining) { + var result = ByteString.empty + while (sourceBuffer.hasRemaining) { + outputBuffer.clear() + decompressingStream.read(outputBuffer) + outputBuffer.flip() + val outputArray = new Array[Byte](outputBuffer.limit()) + outputBuffer.get(outputArray) + result = result.concat(ByteString.fromArrayUnsafe(outputArray)) + } + + // Fencepost case, it's possible to still have sourceBuffer.hasRemaining and yet decompression result + // not output anything + if (result.nonEmpty) + push(out, result) + else + pull(in) + } else pull(in) + sourceBuffer.flip() Review Comment: The `sourceBuffer.flip()` call after the if-else block appears to be incorrect. The buffer was already flipped on line 48, and all processing paths (lines 49-66) consume it. This extra flip will reset the buffer to an inconsistent state. This line should be removed. ```suggestion ``` ########## stream/src/main/scala/org/apache/pekko/stream/impl/io/compression/ZstdCompressor.scala: ########## @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.stream.impl.io.compression + +import java.nio.ByteBuffer + +import com.github.luben.zstd.{ Zstd, ZstdDictCompress, ZstdDirectBufferCompressingStreamNoFinalizer } + +import org.apache.pekko +import pekko.annotation.InternalApi +import pekko.io.ByteBufferCleaner +import pekko.util.ByteString + +/** INTERNAL API */ +@InternalApi private[pekko] class ZstdCompressor( + compressionLevel: Int = + Zstd.defaultCompressionLevel(), dictionary: Option[ZstdDictCompress] = None) extends Compressor { + + private val targetBuffer = ByteBuffer.allocateDirect(65536) + private val compressingStream = new ZstdDirectBufferCompressingStreamNoFinalizer(targetBuffer, compressionLevel) + + dictionary.foreach(compressingStream.setDict) + + override def compress(input: ByteString): ByteString = { + val inputBB = ByteBuffer.allocateDirect(input.size) + inputBB.put(input.toArrayUnsafe()) + inputBB.flip() + compressingStream.compress(inputBB) + val result = ByteString.fromByteBuffer(targetBuffer) + targetBuffer.flip() + if (ByteBufferCleaner.isSupported) + ByteBufferCleaner.clean(inputBB) + result + } + + override def flush(): ByteString = { + targetBuffer.flip() + val result = ByteString.fromByteBuffer(targetBuffer) + targetBuffer.clear() + compressingStream.flush() Review Comment: The `compressingStream.flush()` is called after extracting the result and clearing the buffer. This means any data written by flush() to `targetBuffer` will be lost. The flush should be called before flipping the buffer, and the buffer should be cleared after creating the result ByteString, similar to the pattern in `compressAndFlush`. ```suggestion compressingStream.flush() targetBuffer.flip() val result = ByteString.fromByteBuffer(targetBuffer) targetBuffer.clear() ``` ########## stream/src/main/scala/org/apache/pekko/stream/impl/io/compression/ZstdCompressor.scala: ########## @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.stream.impl.io.compression + +import java.nio.ByteBuffer + +import com.github.luben.zstd.{ Zstd, ZstdDictCompress, ZstdDirectBufferCompressingStreamNoFinalizer } + +import org.apache.pekko +import pekko.annotation.InternalApi +import pekko.io.ByteBufferCleaner +import pekko.util.ByteString + +/** INTERNAL API */ +@InternalApi private[pekko] class ZstdCompressor( + compressionLevel: Int = + Zstd.defaultCompressionLevel(), dictionary: Option[ZstdDictCompress] = None) extends Compressor { + + private val targetBuffer = ByteBuffer.allocateDirect(65536) + private val compressingStream = new ZstdDirectBufferCompressingStreamNoFinalizer(targetBuffer, compressionLevel) + + dictionary.foreach(compressingStream.setDict) + + override def compress(input: ByteString): ByteString = { + val inputBB = ByteBuffer.allocateDirect(input.size) + inputBB.put(input.toArrayUnsafe()) + inputBB.flip() + compressingStream.compress(inputBB) + val result = ByteString.fromByteBuffer(targetBuffer) + targetBuffer.flip() + if (ByteBufferCleaner.isSupported) + ByteBufferCleaner.clean(inputBB) + result + } + + override def flush(): ByteString = { + targetBuffer.flip() + val result = ByteString.fromByteBuffer(targetBuffer) + targetBuffer.clear() + compressingStream.flush() + result + } + + override def finish(): ByteString = { + compressingStream.close() + targetBuffer.flip() + val arr = Array.ofDim[Byte](targetBuffer.limit()) + targetBuffer.get(arr) + val result = ByteString.fromArrayUnsafe(arr) + if (ByteBufferCleaner.isSupported) + ByteBufferCleaner.clean(targetBuffer) + result + } + + override def compressAndFlush(input: ByteString): ByteString = { + val inputBB = ByteBuffer.allocateDirect(input.size) + inputBB.put(input.toArray) + inputBB.flip() + compressingStream.compress(inputBB) + compressingStream.flush() + targetBuffer.flip() + + val arr = new Array[Byte](targetBuffer.limit()) + targetBuffer.get(arr) + targetBuffer.clear() + if (ByteBufferCleaner.isSupported) + ByteBufferCleaner.clean(inputBB) + ByteString.fromArrayUnsafe(arr) + } + + override def compressAndFinish(input: ByteString): ByteString = { + val inputBB = ByteBuffer.allocateDirect(input.size) + inputBB.put(input.toArray) Review Comment: Inconsistent use of `toArray` instead of `toArrayUnsafe()`. Line 41 uses `toArrayUnsafe()` in the `compress` method, but this method uses `toArray` which creates a defensive copy. For consistency and performance, use `toArrayUnsafe()` here as well, matching the pattern in line 41. ########## stream/src/main/scala/org/apache/pekko/stream/impl/io/compression/ZstdCompressor.scala: ########## @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.pekko.stream.impl.io.compression + +import java.nio.ByteBuffer + +import com.github.luben.zstd.{ Zstd, ZstdDictCompress, ZstdDirectBufferCompressingStreamNoFinalizer } + +import org.apache.pekko +import pekko.annotation.InternalApi +import pekko.io.ByteBufferCleaner +import pekko.util.ByteString + +/** INTERNAL API */ +@InternalApi private[pekko] class ZstdCompressor( + compressionLevel: Int = + Zstd.defaultCompressionLevel(), dictionary: Option[ZstdDictCompress] = None) extends Compressor { + + private val targetBuffer = ByteBuffer.allocateDirect(65536) + private val compressingStream = new ZstdDirectBufferCompressingStreamNoFinalizer(targetBuffer, compressionLevel) + + dictionary.foreach(compressingStream.setDict) + + override def compress(input: ByteString): ByteString = { + val inputBB = ByteBuffer.allocateDirect(input.size) + inputBB.put(input.toArrayUnsafe()) + inputBB.flip() + compressingStream.compress(inputBB) + val result = ByteString.fromByteBuffer(targetBuffer) + targetBuffer.flip() Review Comment: The order of operations is incorrect. `ByteString.fromByteBuffer` reads from the current position to the limit, but `targetBuffer` hasn't been flipped yet. The flip should occur before calling `fromByteBuffer`. This should be: `targetBuffer.flip()` followed by `val result = ByteString.fromByteBuffer(targetBuffer)`. ```suggestion targetBuffer.flip() val result = ByteString.fromByteBuffer(targetBuffer) ``` -- 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]
