Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace old Hashtable with modern data structures. #624

Closed
wants to merge 1 commit into from
Closed

Replace old Hashtable with modern data structures. #624

wants to merge 1 commit into from

Conversation

dbeliakov-r7
Copy link

This PR replaces the old Hashtable with modern data structures. Hashtable is no longer maintained and is not optimised for better performance.

I replaced type declaration with the interface. Since Hashtable is-a Map, this is seamless.
I replaced Hashtable with ConcurrentHashMap where multiple threads is expected to access the map, but ConcurrentHashMap handles it better (this could be improved further, but one step at a time).
I replaced Hashtable with HashMap where synchronisation was unnecessary.

Copy link

sonarcloud bot commented Aug 19, 2024

@norrisjeremy
Copy link
Contributor

I'm not sure I approve of this: it's not fixing any sort of bug nor is it likely fixing any serious performance issue.
All it is doing is potentially introducing new subtle bugs or incompatibilities.

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly certain this doesn't meet Google Java style that we utilize.
You should run mvn formatter:format.

config.put(key, newconf.get(newkey));
}
newconf.forEach((key, value) -> config
.put(key.equals("PubkeyAcceptedKeyTypes") ? "PubkeyAcceptedAlgorithms" : key, value));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather just see a traditional for loop over an newconf.entrySet() instead of the .forEach() lambda.


public class JSch {
/** The version number. */
public static final String VERSION = Version.getVersion();

static Hashtable<String, String> config = new Hashtable<>();
static Map<String, String> config = new ConcurrentHashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is any value in using a ConcurrentHashMap instead of a regular HashMap since all accesses to the config are synchronized on config anyway.

private static Hashtable<Session, byte[]> faked_cookie_pool = new Hashtable<>();
private static Hashtable<Session, byte[]> faked_cookie_hex_pool = new Hashtable<>();
private static final Map<Session, byte[]> faked_cookie_pool = new ConcurrentHashMap<>();
private static final Map<Session, byte[]> faked_cookie_hex_pool = new ConcurrentHashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is any value in using a ConcurrentHashMap instead of a regular HashMap since all accesses to the faked_cookie_hex_pool are synchronized on faked_cookie_hex_pool anyway.

@@ -45,8 +46,8 @@ class ChannelX11 extends Channel {
static byte[] cookie = null;
private static byte[] cookie_hex = null;

private static Hashtable<Session, byte[]> faked_cookie_pool = new Hashtable<>();
private static Hashtable<Session, byte[]> faked_cookie_hex_pool = new Hashtable<>();
private static final Map<Session, byte[]> faked_cookie_pool = new ConcurrentHashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just keep faked_cookie_pool a HashMap and maintain the synchronized on faked_cookie_pool that was removed anyway.

@@ -78,7 +79,7 @@ public void setXForwarding(boolean enable) {
@Deprecated
public void setEnv(Hashtable<byte[], byte[]> env) {
synchronized (this) {
this.env = env;
this.env = new ConcurrentHashMap<>(env);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is any value in using a ConcurrentHashMap instead of a regular HashMap since all accesses to the env are synchronized on env anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants