eclipse-vertx / vert.x

Vert.x is a tool-kit for building reactive applications on the JVM
http://vertx.io
Other
14.32k stars 2.08k forks source link

ConcurrentHashSet is redundant with ConcurrentHashMap#newKeySet() #4851

Closed magicprinc closed 1 year ago

magicprinc commented 1 year ago

It seems that io.vertx.core.impl.ConcurrentHashSet is a copy of JDK's Collections.newSetFromMap. The Best Code is No Code At All. It is probably a good idea to review and replace this class with two utility methods:

public static <E> Set<E> concurrentHashSet (){
  return Collections.newSetFromMap(new ConcurrentHashMap<>());
}

public static <E> Set<E> concurrentHashSet (int initialSize){
  return Collections.newSetFromMap(new ConcurrentHashMap<>(initialSize));
}

PR: https://github.com/eclipse-vertx/vert.x/pull/4852

He-Pin commented 1 year ago

Better to use ConcurrentHashMap.newKeySet

tsegismont commented 1 year ago

You're welcome to contribute a PR for this, as well as in the impacted projects:

magicprinc commented 1 year ago

I would like to, but I still have failed tests when I build vert.x-core locally on my Windows10/JDK17. So I am not ready yet... :-(

BTW, I would enjoy if you find my other PR useful: https://github.com/eclipse-vertx/vert.x/pull/4844 https://github.com/eclipse-vertx/vert.x/pull/4847 https://github.com/eclipse-vertx/vert.x/pull/4850

They have more benefits than ConcurrentHashSet removial.

He-Pin commented 1 year ago

If you don't mind, i can do this too:), @magicprinc ,a nice find!

magicprinc commented 1 year ago

@tsegismont Anyway. Without tests :-) https://github.com/eclipse-vertx/vert.x/pull/4852

@He-Pin Could you test it? And add test for concurrentHashSet implementations too?

magicprinc commented 1 year ago

1) I have replaced method with its body. 2) Fix in vertx-service-discovery: https://github.com/vert-x3/vertx-service-discovery/pull/172 3) Fix in vertx-mqtt: https://github.com/vert-x3/vertx-mqtt/pull/243

@tsegismont All your tasks are done!

tsegismont commented 1 year ago

@magicprinc there's one more usage in vertx-mqtt, can you please take care of it before we merge this PR?

magicprinc commented 1 year ago

@tsegismont I am sorry, but I can't find it. Could you name a class with it? / no_CHS

magicprinc commented 1 year ago

GitHub search shows 2 files, and both are fixed in my PR https://github.com/vert-x3/vertx-mqtt/pull/243

tsegismont commented 1 year ago

Here are the references in MQTT:

https://github.com/vert-x3/vertx-mqtt/blob/master/src/test/java/io/vertx/mqtt/test/server/MqttServerTest.java#L84 https://github.com/vert-x3/vertx-mqtt/blob/master/src/test/java/io/vertx/mqtt/test/server/MqttServerWebSocketTest.java#L78

magicprinc commented 1 year ago

@tsegismont They are both fixed in PR https://github.com/vert-x3/vertx-mqtt/pull/243 Have you seen this PR?

tsegismont commented 1 year ago

For some reason, I wasn't subscribed to all notifications on vertx-mqtt

This is why I missed the PR, sorry.

tsegismont commented 1 year ago

Closed by #4852