You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "pjfanning (via GitHub)" <gi...@apache.org> on 2025/06/07 20:12:17 UTC
[PR] Remove use of sun.misc.Unsafe in Mailbox [pekko]
pjfanning opened a new pull request, #1894:
URL: https://github.com/apache/pekko/pull/1894
Unsafe is deprecated and in newer Java versions, it is getting harder to use it.
In my benchmarking, the Atomic classes in Java deliver performance that is as good.
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "mdedetrich (via GitHub)" <gi...@apache.org>.
mdedetrich commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-3140914092
@pjfanning I have fixed the issue with `VarHandle` being slow, see https://github.com/pjfanning/pekko-bench/pull/3
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "mdedetrich (via GitHub)" <gi...@apache.org>.
mdedetrich commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-3139498117
> I haven't double checked the results recently but the varhandle approach is slower than the alternatives - but maybe I made a mistake in its impl.
I think there is an issue with the impl, at https://github.com/pjfanning/pekko-bench/blob/main/src/main/scala/example/ActorCell.scala#L22 you are casting the result using `.asInstanceOf[Long]` which explains why its so slow, I believe there are typed variants of `VarHandle` to avoid this cast, which is the same problem described at https://github.com/OpenHFT/Zero-Allocation-Hashing/issues/67#issuecomment-2810148595
Ill look into it and open a PR if I manage to improve the 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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on code in PR #1894:
URL: https://github.com/apache/pekko/pull/1894#discussion_r2246323796
##########
actor/src/main/java/org/apache/pekko/dispatch/AbstractMailbox.java:
##########
@@ -1,28 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * license agreements; and to You under the Apache License, version 2.0:
+ * 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
*
- * https://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * This file is part of the Apache Pekko project, which was derived from Akka.
- */
-
-/*
- * Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
+ * 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.
Review Comment:
all the old Akka code was removed - so I changed to the standard ASF code
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "mdedetrich (via GitHub)" <gi...@apache.org>.
mdedetrich commented on code in PR #1894:
URL: https://github.com/apache/pekko/pull/1894#discussion_r2246480576
##########
actor/src/main/scala/org/apache/pekko/dispatch/Mailbox.scala:
##########
@@ -118,8 +118,7 @@ private[pekko] abstract class Mailbox(val messageQueue: MessageQueue)
@volatile
protected var _systemQueueDoNotCallMeDirectly: SystemMessage = _ // null by default
- final def currentStatus: Mailbox.Status =
- Unsafe.instance.getIntVolatile(this, AbstractMailbox.mailboxStatusOffset): @nowarn("cat=deprecation")
+ final def currentStatus: Mailbox.Status = AbstractMailbox.mailboxStatusHandle.get(this).asInstanceOf[Mailbox.Status]
Review Comment:
Can you try removing the `.asInstanceOf[Mailbox.Status]`?
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "raboof (via GitHub)" <gi...@apache.org>.
raboof commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-2991224636
seems reasonable to me, any reason this is still in 'draft'?
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-3139438440
> Actually JDK 23 may not even be needed, I think that `VarHandle` is enough and that was introduced in JDK 9 so we can use it without needing any multi-jar release, see https://muratcanyeldan.com/whats-new-in-java-23-a-comprehensive-overview-cf0c2d23e61e.
>
> [OpenHFT/Zero-Allocation-Hashing#67](https://github.com/OpenHFT/Zero-Allocation-Hashing/issues/67) is also relevant, they had to do the same thing and they also confirmed that `VarHandle` is just as fast as unsafe
I'm not looking to merge this at the moment.
VarHandle is probably the right change but I was disappointed by its performance compared to Unsafe. It may not matter that much as perf diff may not be much compared to everything else that happens.
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-2991268474
This not a behaviour change - it is just an internal refactor of code that makes makes it easier to use Java 24 and 25.
@raboof I still want to do more benchmark testing on this. I'm more happy with #1892 - the benchmarks show that change will have minimal perf impact
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "He-Pin (via GitHub)" <gi...@apache.org>.
He-Pin commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-2991254561
I think the reason is that we should ship this in the next 2.0 release.
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "mdedetrich (via GitHub)" <gi...@apache.org>.
mdedetrich commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-3139448722
> VarHandle is probably the right change but I was disappointed by its performance compared to Unsafe. It may not matter that much as perf diff may not be much compared to everything else that happens.
So according to https://github.com/OpenHFT/Zero-Allocation-Hashing/issues/67, `VarHandle` performance should be identical (according to that issue the initial benchmarks for varhandle was wrong, see https://github.com/OpenHFT/Zero-Allocation-Hashing/issues/67#issuecomment-2810148595)
Did you benchmark with `Varhandle` or was this second hand info?
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-3139474935
> > VarHandle is probably the right change but I was disappointed by its performance compared to Unsafe. It may not matter that much as perf diff may not be much compared to everything else that happens.
>
> So according to [OpenHFT/Zero-Allocation-Hashing#67](https://github.com/OpenHFT/Zero-Allocation-Hashing/issues/67), `VarHandle` performance should be identical (according to that issue the initial benchmarks for varhandle was wrong, see [OpenHFT/Zero-Allocation-Hashing#67 (comment)](https://github.com/OpenHFT/Zero-Allocation-Hashing/issues/67#issuecomment-2810148595))
>
> Did you benchmark with `Varhandle` or was this second hand info?
I did basic microbenchmarks here:
https://github.com/pjfanning/pekko-bench
I haven't double checked the results recently but the varhandle approach is slower than the alternatives - but maybe I made a mistake in its impl.
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "mdedetrich (via GitHub)" <gi...@apache.org>.
mdedetrich commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-3140327234
Hmm, so the implementation of `VarHandle` is indeed correct but something else is going on, if I change the type of [`_nextName` ](https://github.com/pjfanning/pekko-bench/blob/31d3bd4a7bd85a9b3b3575ca43e4455b3a30e8f8/src/main/scala/example/ActorCell.scala#L10) to `Int` and do all of the necessary changes then `VarHandles` are as fast as unsafe, there must be some implicit int/long conversion going on somewhere which is causing it to be slower
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "raboof (via GitHub)" <gi...@apache.org>.
raboof commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-2991265787
Why?
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #1894:
URL: https://github.com/apache/pekko/pull/1894#discussion_r2246318381
##########
actor/src/main/java/org/apache/pekko/dispatch/AbstractMailbox.java:
##########
@@ -1,28 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * license agreements; and to You under the Apache License, version 2.0:
+ * 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
*
- * https://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * This file is part of the Apache Pekko project, which was derived from Akka.
- */
-
-/*
- * Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
+ * 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.
Review Comment:
The license header change appears to be unintentional. The original license header was correctly formatted and should be preserved unless there's a specific reason to change it.
##########
actor/src/main/java/org/apache/pekko/dispatch/AbstractMailbox.java:
##########
@@ -1,28 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
- * license agreements; and to You under the Apache License, version 2.0:
+ * 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
*
- * https://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
Review Comment:
The URL format has been changed from HTTPS to HTTP, which is inconsistent with security best practices. The original HTTPS URL should be maintained.
```suggestion
* https://www.apache.org/licenses/LICENSE-2.0
```
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "mdedetrich (via GitHub)" <gi...@apache.org>.
mdedetrich commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-3139429459
Actually JDK 23 may not even be needed, I think that `VarHandle` is enough and that was introduced in JDK 9 so we can use it without needing any multi-jar release, see https://muratcanyeldan.com/whats-new-in-java-23-a-comprehensive-overview-cf0c2d23e61e.
https://github.com/OpenHFT/Zero-Allocation-Hashing/issues/67 is also relevant, they had to do the same thing and they also confirmed that `VarHandle` is just as fast as unsafe
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "mdedetrich (via GitHub)" <gi...@apache.org>.
mdedetrich commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-3139400254
Hmm we have to be careful about this, this is core code and it was written using unsafe because its in hot path (not trying to discount @pjfanning claim that "its as fast", just that we need to be 100% sure about this).
From what I have read, JDK 22 and newer have an officially supported replacement with `Arena` and `MemorySegment` API's (see https://www.infoq.com/news/2024/06/jep-456-removing-unsafe-methods/) so to me the safest but also hardest solution to this is to leave the `unsafe` calls as is but use multi-release jar for JDK 23 or later for the proper replacements.
This will mean we will have to get proper support for multi release jar in sbt though
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning merged PR #1894:
URL: https://github.com/apache/pekko/pull/1894
--
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] Remove use of sun.misc.Unsafe in Mailbox [pekko]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on PR #1894:
URL: https://github.com/apache/pekko/pull/1894#issuecomment-3140942355
> @pjfanning I have fixed the issue with `VarHandle` being slow, see [pjfanning/pekko-bench#3](https://github.com/pjfanning/pekko-bench/pull/3).
>
> Do you want to update the PR to use `VarHandle` or should I make a new one?
Good catch. VarHandles are the best way to get rid of the Unsafe field offset code.
--
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