You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "He-Pin (via GitHub)" <gi...@apache.org> on 2024/01/20 22:12:44 UTC
[PR] chore: Make use of System.arraycopy directly. [incubator-pekko]
He-Pin opened a new pull request, #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001
Motivation:
As ByteString is only copy `Array[Byte]`, better to just use `System.arraycopy`
Result:
Better performance.
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#discussion_r1460842193
##########
bench-jmh/src/main/scala/org/apache/pekko/util/ArrayCopyOf_Benchmark.scala:
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.util
+
+import org.openjdk.jmh.annotations._
+
+import java.util
+import java.util.concurrent.TimeUnit
+
+@State(Scope.Benchmark)
+@Measurement(timeUnit = TimeUnit.MILLISECONDS)
+class ArrayCopyOf_Benchmark {
+
+ var bs: Array[Byte] = _
+
+ @Param(Array("1", "10", "100", "1000"))
+ var kb = 0
+
+ /*
+ bench-jmh/jmh:run -f 1 -wi 10 -i 10 .*ArrayCopyOf_Benchmark.*
+ */
+
+ @Setup
+ def setup(): Unit = {
+ bs = Array.fill[Byte](1024 * kb)(1)
+ }
+
+ @Benchmark
+ def handleCopyAndGrow(): Unit = {
+ val len = bs.length
+ val buffer2 = new Array[Byte](bs.length << 1)
+ System.arraycopy(bs, 0, buffer2, 0, len)
+ }
+
+ @Benchmark
+ def ArraysCopyOfAndGrow(): Unit = {
+ val len = bs.length
+ util.Arrays.copyOf(bs, len << 1)
Review Comment:
why not compare Array.copy with System.arraycopy ?
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "jrudolph (via GitHub)" <gi...@apache.org>.
jrudolph commented on PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#issuecomment-1911846646
Would you mind to explain how `System.arraycopy` and `Arrays.copyOf` relate (which the cats-effect change uses)?
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#issuecomment-1902572803
@mdedetrich Would you like to take a look at this too, thanks.
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on code in PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#discussion_r1460860144
##########
bench-jmh/src/main/scala/org/apache/pekko/util/ArrayCopyOf_Benchmark.scala:
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.util
+
+import org.openjdk.jmh.annotations._
+
+import java.util
+import java.util.concurrent.TimeUnit
+
+@State(Scope.Benchmark)
+@Measurement(timeUnit = TimeUnit.MILLISECONDS)
+class ArrayCopyOf_Benchmark {
+
+ var bs: Array[Byte] = _
+
+ @Param(Array("1", "10", "100", "1000"))
+ var kb = 0
+
+ /*
+ bench-jmh/jmh:run -f 1 -wi 10 -i 10 .*ArrayCopyOf_Benchmark.*
+ */
+
+ @Setup
+ def setup(): Unit = {
+ bs = Array.fill[Byte](1024 * kb)(1)
+ }
+
+ @Benchmark
+ def handleCopyAndGrow(): Unit = {
+ val len = bs.length
+ val buffer2 = new Array[Byte](bs.length << 1)
+ System.arraycopy(bs, 0, buffer2, 0, len)
+ }
+
+ @Benchmark
+ def ArraysCopyOfAndGrow(): Unit = {
+ val len = bs.length
+ util.Arrays.copyOf(bs, len << 1)
Review Comment:
I was forget to delete this.
##########
bench-jmh/src/main/scala/org/apache/pekko/util/ArrayCopyOf_Benchmark.scala:
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.util
+
+import org.openjdk.jmh.annotations._
+
+import java.util
+import java.util.concurrent.TimeUnit
+
+@State(Scope.Benchmark)
+@Measurement(timeUnit = TimeUnit.MILLISECONDS)
+class ArrayCopyOf_Benchmark {
+
+ var bs: Array[Byte] = _
+
+ @Param(Array("1", "10", "100", "1000"))
+ var kb = 0
+
+ /*
+ bench-jmh/jmh:run -f 1 -wi 10 -i 10 .*ArrayCopyOf_Benchmark.*
+ */
+
+ @Setup
+ def setup(): Unit = {
+ bs = Array.fill[Byte](1024 * kb)(1)
+ }
+
+ @Benchmark
+ def handleCopyAndGrow(): Unit = {
+ val len = bs.length
+ val buffer2 = new Array[Byte](bs.length << 1)
+ System.arraycopy(bs, 0, buffer2, 0, len)
+ }
+
+ @Benchmark
+ def ArraysCopyOfAndGrow(): Unit = {
+ val len = bs.length
+ util.Arrays.copyOf(bs, len << 1)
Review Comment:
I forget to delete this.
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#issuecomment-1911947781
@jrudolph Yes, I was shocked that in CE, they are not using the `Array.copy` method too, and that's why I come up with this .
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#issuecomment-1911956613
Thanks for the detailed review.
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#issuecomment-1911908162
@jrudolph @pjfanning Let me explian:
1. I was looking at the change of CE, where they just call `system.arrayCopy` instead of Scala's `Array.copy`, and the Scala's `Array.copy` did some extra checking , but in this context, we knew it's always the `Array[Byte]`, so we can just call Java's `System.arrayCopy` directly.
```scala
if (srcClass.isArray && dest.getClass.isAssignableFrom(srcClass))
java.lang.System.arraycopy(src, srcPos, dest, destPos, length)
else
slowcopy(src, srcPos, dest, destPos, length)
```
2. pekko/akka's ByteString extends the `IndexedSeq` and in the test, when we concating it with a `Vector[Byte]` which will cause problem, because the `Vector[Byte]` is actually backing by a `Array[AnyRef]`, if I change the code to `System.arrayCopy` then an `Array store exception` will throw. I asked on scala/bugs and that's not a design goal of Scala's stdlib. refs: https://github.com/scala/bug/issues/12931
```scala
override def copyToArray[B >: Byte](dest: Array[B], start: Int, len: Int): Int = {
// min of the bytes available to copy, bytes there is room for in dest and the requested number of bytes
val toCopy = math.min(math.min(len, length), dest.length - start)
if (toCopy > 0) {
Array.copy(bytes, startIndex, dest, start, toCopy)
}
toCopy
}
```
So I think we can just call the `System.arraycopy` where it fits. I asked on JVM group of china too, I was told the the CE's pr may not be faster on little bytes, and the results shows too, which is for SIMD,anyway, that's another story.
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#issuecomment-1903380618

@pjfanning @mdedetrich I just ran the benchmark.
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "jrudolph (via GitHub)" <gi...@apache.org>.
jrudolph commented on PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#issuecomment-1911945578
Thanks for the explanation, @He-Pin. Moving away from `Array.copy` seems like a good solution when you know it's an `Array[Byte]`.
Regarding the integration of `ByteString` with scala collection, I think, this is one of its weakest parts, so I think it's fine not to be optimal here.
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin merged PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#issuecomment-1911840698
@mdedetrich what do you think? I'm neutral to leaning slightly against. The perf gains seem small. In the end of the day, maybe we can delay till 1.1.0-M2.
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "jrudolph (via GitHub)" <gi...@apache.org>.
jrudolph commented on PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#issuecomment-1911847576
> Have no idea why a `Vector[Byte]` is backed by a `Array[AnyRef]`
Just out of interest, what does this relate to?
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "jrudolph (via GitHub)" <gi...@apache.org>.
jrudolph commented on PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#issuecomment-1911959986
> @jrudolph Yes, I was shocked that in CE, they are not using the `Array.copy` method too, and that's why I come up with this .
Not sure what you mean?
It's pretty likely that `java.util.Arrays.copyOf` and `System.arraycopy` are both intrinsics, so the difference is likely small. There's the a chance that `Arrays.copyOf` is a slight bit faster, since it is a higher-level definition, so the JVM might be avoiding some bounds checks etc between creating the destination array and copying over the data (or at least with less analysis necessary in the JIT compiler).
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#issuecomment-1911945965
@pjfanning @mdedetrich it's 4% free lunch , why not?

--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [PR] perf: Make use of System.arraycopy directly. [incubator-pekko]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on code in PR #1001:
URL: https://github.com/apache/incubator-pekko/pull/1001#discussion_r1460860653
##########
bench-jmh/src/main/scala/org/apache/pekko/util/ArrayCopyOf_Benchmark.scala:
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.util
+
+import org.openjdk.jmh.annotations._
+
+import java.util
+import java.util.concurrent.TimeUnit
+
+@State(Scope.Benchmark)
+@Measurement(timeUnit = TimeUnit.MILLISECONDS)
+class ArrayCopyOf_Benchmark {
+
+ var bs: Array[Byte] = _
+
+ @Param(Array("1", "10", "100", "1000"))
+ var kb = 0
+
+ /*
+ bench-jmh/jmh:run -f 1 -wi 10 -i 10 .*ArrayCopyOf_Benchmark.*
+ */
+
+ @Setup
+ def setup(): Unit = {
+ bs = Array.fill[Byte](1024 * kb)(1)
+ }
+
+ @Benchmark
+ def handleCopyAndGrow(): Unit = {
+ val len = bs.length
+ val buffer2 = new Array[Byte](bs.length << 1)
+ System.arraycopy(bs, 0, buffer2, 0, len)
+ }
+
+ @Benchmark
+ def ArraysCopyOfAndGrow(): Unit = {
+ val len = bs.length
+ util.Arrays.copyOf(bs, len << 1)
Review Comment:
And this is a test for https://github.com/typelevel/cats-effect/pull/3963/, I will delete the benchmark.
--
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: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org