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

8270317: Large Allocation in CipherSuite #4783

Closed
wants to merge 7 commits into from

Conversation

cliveverghese
Copy link
Contributor

@cliveverghese cliveverghese commented Jul 14, 2021

Benchmark results

I have benchmarked 3 cases.

  1. The current situation.
Benchmark                                                        (cipherSuite)  Mode  Cnt    Score   Error  Units
CipherSuiteBench.benchmarkCipherSuite                   TLS_AES_256_GCM_SHA384  avgt   25  124.783 ? 2.050  ns/op
CipherSuiteBench.benchmarkCipherSuite  TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384  avgt   25  125.403 ? 0.554  ns/op
CipherSuiteBench.benchmarkCipherSuite      TLS_DHE_DSS_WITH_AES_128_CBC_SHA256  avgt   25  127.117 ? 0.789  ns/op
CipherSuiteBench.benchmarkCipherSuite         TLS_DHE_RSA_WITH_AES_256_CBC_SHA  avgt   25  127.869 ? 1.112  ns/op
  1. Use static final array instead of calling CipherSuite.values each time.
Benchmark                                                        (cipherSuite)  Mode  Cnt   Score   Error  Units
CipherSuiteBench.benchmarkCipherSuite                   TLS_AES_256_GCM_SHA384  avgt   25  10.146 ? 0.252  ns/op
CipherSuiteBench.benchmarkCipherSuite  TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384  avgt   25  30.501 ? 0.207  ns/op
CipherSuiteBench.benchmarkCipherSuite      TLS_DHE_DSS_WITH_AES_128_CBC_SHA256  avgt   25  47.375 ? 0.150  ns/op
CipherSuiteBench.benchmarkCipherSuite         TLS_DHE_RSA_WITH_AES_256_CBC_SHA  avgt   25  55.887 ? 3.786  ns/op
  1. Using Hashmap for lookup instead of iterating through the array each time. (Method in this PR)
Benchmark                                                        (cipherSuite)  Mode  Cnt   Score   Error  Units
CipherSuiteBench.benchmarkCipherSuite                   TLS_AES_256_GCM_SHA384  avgt   25  13.533 ? 0.148  ns/op
CipherSuiteBench.benchmarkCipherSuite  TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384  avgt   25  11.269 ? 0.147  ns/op
CipherSuiteBench.benchmarkCipherSuite      TLS_DHE_DSS_WITH_AES_128_CBC_SHA256  avgt   25  11.507 ? 0.107  ns/op
CipherSuiteBench.benchmarkCipherSuite         TLS_DHE_RSA_WITH_AES_256_CBC_SHA  avgt   25  10.932 ? 0.146  ns/op

I have picked 4 cipher suite from the start of the list and are roughly 10 positions apart. I have opted to go with HashMap for name and id lookup as they provide a more consistent times and benchmarks are similar for the first few cipher suits in the enum as well.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/4783/head:pull/4783
$ git checkout pull/4783

Update a local copy of the PR:
$ git checkout pull/4783
$ git pull https://git.openjdk.java.net/jdk pull/4783/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 4783

View PR using the GUI difftool:
$ git pr show -t 4783

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/4783.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Jul 14, 2021

👋 Welcome back cverghese! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jul 14, 2021
@openjdk
Copy link

openjdk bot commented Jul 14, 2021

@cliveverghese The following label will be automatically applied to this pull request:

  • security

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the security security-dev@openjdk.org label Jul 14, 2021
@mlbridge
Copy link

mlbridge bot commented Jul 14, 2021

@XueleiFan
Copy link
Member

Hi Clive Verghese,

The performance improve is impressive to me. Would you mind have an additional benchmark for the throughput (@BenchmarkMode(Mode.Throughput))? I guess the throughput should be good as well.

Thanks!

Copy link
Member

@djelinski djelinski left a comment

Choose a reason for hiding this comment

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

The benchmark you provided looks a bit odd... In variant 1 best and worst cases differ by 3 ns, and in variant 2 they differ by 45 ns. The algorithm is supposed to be the same, so... Where does the difference come from?

maps_id.put(cs.id, cs);
maps_name.put(cs.name, cs);
for (String alias : cs.aliases) {
maps_name.put(alias, cs);
Copy link
Member

Choose a reason for hiding this comment

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

A minor concern here. HashMap can't have duplicate keys. what if there are duplicated names/aliases?

Even it's not the case now, CipherSuite is subject to change in the future and I think it is a good opportunity to detect key duplication here. Currently, it's silently overwritten. This may introduce inconsistent behavior for nameOf.

Copy link
Contributor

Choose a reason for hiding this comment

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

this could be potentially stored in immutable collections which might be slightly more compact + they throw when they encounter duplicate keys

  1. change base type to Map
  2. copy ciphers array into Map.Entry array
  3. maps_id = Map.ofEntries(entries) // + handle IAE

similar for the name map but with a list in between.

there might be a JDK internal shortcut to get to new ImmutableCollections.MapN<>(flatArray) right away without the Map.Entry step - I am sure a reviewer will chime in if there is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the feedback. I have updated to use ImmutableMap. However, I had to use copyOf(Map) method instead.

It would not check for duplicate values. Would it make more sense to add a test case to check for duplicates instead?. If so I can add that, else i'll check for duplicate while creating the map.

@cliveverghese
Copy link
Contributor Author

Hi Clive Verghese,

The performance improve is impressive to me. Would you mind have an additional benchmark for the throughput (@BenchmarkMode(Mode.Throughput))? I guess the throughput should be good as well.

Thanks!

Sure, I'll add the Throughput mode and run the benchmarks.

The benchmark you provided looks a bit odd... In variant 1 best and worst cases differ by 3 ns, and in variant 2 they differ by 45 ns. The algorithm is supposed to be the same, so... Where does the difference come from?

I'll rerun the benchmarks with larger iterations and investigate a bit further to understand this difference. Thank you for point it out.

@cliveverghese
Copy link
Contributor Author

cliveverghese commented Jul 21, 2021

Updated Benchmarks in Throughput mode

Current

Benchmark                                                        (cipherSuite)   Mode  Cnt     Score     Error   Units
CipherSuiteBench.benchmarkCipherSuite                   TLS_AES_256_GCM_SHA384  thrpt   25  8050.787 ? 232.335  ops/ms
CipherSuiteBench.benchmarkCipherSuite  TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384  thrpt   25  8165.124 ? 283.718  ops/ms
CipherSuiteBench.benchmarkCipherSuite      TLS_DHE_DSS_WITH_AES_128_CBC_SHA256  thrpt   25  7827.758 ? 293.311  ops/ms
CipherSuiteBench.benchmarkCipherSuite         TLS_DHE_RSA_WITH_AES_256_CBC_SHA  thrpt   25  7768.286 ? 181.399  ops/ms

ArrayList

Benchmark                                                        (cipherSuite)   Mode  Cnt    Score   Error   Units
CipherSuiteBench.benchmarkCipherSuite                   TLS_AES_256_GCM_SHA384  thrpt   25  100.626 ? 0.294  ops/us
CipherSuiteBench.benchmarkCipherSuite  TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384  thrpt   25   32.793 ? 0.804  ops/us
CipherSuiteBench.benchmarkCipherSuite      TLS_DHE_DSS_WITH_AES_128_CBC_SHA256  thrpt   25   21.162 ? 0.217  ops/us
CipherSuiteBench.benchmarkCipherSuite         TLS_DHE_RSA_WITH_AES_256_CBC_SHA  thrpt   25   18.220 ? 0.903  ops/us

Hashmap

Benchmark                                                        (cipherSuite)   Mode  Cnt   Score   Error   Units
CipherSuiteBench.benchmarkCipherSuite                   TLS_AES_256_GCM_SHA384  thrpt   25  63.836 ? 4.517  ops/us
CipherSuiteBench.benchmarkCipherSuite  TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384  thrpt   25  69.983 ? 8.965  ops/us
CipherSuiteBench.benchmarkCipherSuite      TLS_DHE_DSS_WITH_AES_128_CBC_SHA256  thrpt   25  68.091 ? 7.404  ops/us
CipherSuiteBench.benchmarkCipherSuite         TLS_DHE_RSA_WITH_AES_256_CBC_SHA  thrpt   25  52.831 ? 4.317  ops/us

I am currently looking into the JFR profiles to identify why there is a difference in benchmarks with regards the different cipher suits in the current version vs the arraylist.

@XueleiFan
Copy link
Member

Updated Benchmarks in Throughput mode

Thank you very much for the update.

I am currently looking into the JFR profiles to identify why there is a difference in benchmarks with regards the different cipher suits in the current version vs the arraylist.

I guess the garbage collection plays a role here.

@cliveverghese
Copy link
Contributor Author

I am currently looking into the JFR profiles to identify why there is a difference in benchmarks with regards the different cipher suits in the current version vs the arraylist.

I guess the garbage collection plays a role here.

Yes, from the JFR profiles it looks like this could be from the GC.

Comment on lines 856 to 858
private static final Map<Integer, CipherSuite> maps_id;
private static final Map<String, CipherSuite> maps_name;
private static final CipherSuite[] ciphers = CipherSuite.values();
Copy link
Member

Choose a reason for hiding this comment

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

Generally, Java follows camel-case syntax for names. It would be good to keep the coding style consistent and use instinctive names. I may use names like "cipherSuiteIds", "cipherSuiteNames" and "cipherSuites" for the above three fields.

if (cs.id == id) {
return cs.name;
}
String name = maps_id.get(id).name;
Copy link
Member

Choose a reason for hiding this comment

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

I guess get() may return null. It may be safe to check the return value of get():

        CipherSuite cipherSuite = cipherSuiteIds.get(id);
        if (cipherSuite != null) {
            return cipherSuite.name;
        }

}

return "UNKNOWN-CIPHER-SUITE(" + Utilities.byte16HexString(id) + ")";
}

static Collection<CipherSuite> allowedCipherSuites() {
Collection<CipherSuite> cipherSuites = new LinkedList<>();
for (CipherSuite cs : CipherSuite.values()) {
for (CipherSuite cs : ciphers) {
Copy link
Member

@XueleiFan XueleiFan Jul 22, 2021

Choose a reason for hiding this comment

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

It may worthy of a static class field for the allowed cipher suites.

@@ -940,7 +942,7 @@ static String nameOf(int id) {

static Collection<CipherSuite> defaultCipherSuites() {
Collection<CipherSuite> cipherSuites = new LinkedList<>();
for (CipherSuite cs : CipherSuite.values()) {
for (CipherSuite cs : ciphers) {
Copy link
Member

@XueleiFan XueleiFan Jul 22, 2021

Choose a reason for hiding this comment

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

It may worthy of a static class field for the default cipher suites.

Comment on lines 975 to 977
for (CipherSuite cs : CipherSuite.values()) {
for (CipherSuite cs : ciphers) {
Copy link
Member

@XueleiFan XueleiFan Jul 22, 2021

Choose a reason for hiding this comment

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

If there is a static allowed cipher suite list, the logic here could be simplified to use the allowed cipher suite list. Then, the "ciphers" class filed may be not needed any longer.

@cliveverghese
Copy link
Contributor Author

Hi @XueleiFan, Thank you for the feedback. I have update the pull requests addressing your comments.

Copy link
Member

@XueleiFan XueleiFan left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thank you!

Map<String, CipherSuite> names = new HashMap<>();
List<CipherSuite> allowedCS = new ArrayList<>();
List<CipherSuite> defaultCS = new ArrayList<>();
for(CipherSuite cs : CipherSuite.values()) {
Copy link
Member

Choose a reason for hiding this comment

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

I may add blank lines before and after the for-loop block.

Copy link
Contributor Author

@cliveverghese cliveverghese Jul 22, 2021

Choose a reason for hiding this comment

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

Than you @XueleiFan for approving the PR.

I have added the blank lines before and after the PR.

@openjdk
Copy link

openjdk bot commented Jul 22, 2021

@cliveverghese This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8270317: Large Allocation in CipherSuite

Reviewed-by: xuelei, simonis

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 145 new commits pushed to the master branch:

  • 7468bbc: 8266988: compiler/jvmci/compilerToVM/IsMatureTest.java fails with Unexpected isMature state for multiple times invoked method: expected false to equal true
  • 286106d: 8271219: [REDO] JDK-8271063 Print injected fields for InstanceKlass
  • a1c0a6a: 8199594: Add doc describing how (?x) ignores spaces in character classes
  • 4ed548b: 8268261: C2: assert(n != __null) failed: Bad immediate dominator info.
  • ec71e2d: 8271221: [BACKOUT] JDK-8271063 Print injected fields for InstanceKlass
  • f4b3ee5: 8270280: security/infra/java/security/cert/CertPathValidator/certification/LetsEncryptCA.java OCSP response error
  • 45abbee: 8243543: jtreg test security/infra/java/security/cert/CertPathValidator/certification/BuypassCA.java fails
  • c9251db: 8271209: Fix doc comment typos in JavadocTokenizer
  • 96247ae: 8270187: G1: Remove ConcGCThreads constraint
  • 9b27df6: 8271063: Print injected fields for InstanceKlass
  • ... and 135 more: https://git.openjdk.java.net/jdk/compare/9cac94d581f240c10fe8fff2f803109a1ae30637...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@XueleiFan, @simonis) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jul 22, 2021
} else {
// values() is ordered, remaining cipher suites are
// not supported.
for (CipherSuite cs : allowedCipherSuites) {
Copy link
Member

Choose a reason for hiding this comment

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

We can avoid this loop; look up the cipher by name first (cs = nameOf(name)), then check for supported protocols.
Other than that, LGTM.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the feedback. I have updated the loop accordingly.

break;
}
CipherSuite cs;
if ((cs = cipherSuiteNames.get(name)) != null && !cs.supportedProtocols.isEmpty()) {
Copy link
Member

Choose a reason for hiding this comment

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

Nice update! I appreciate if you could avoid lines longer than 80 characters, since they’re not handled well by many terminals and tools (See Java Code Conventions).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you @XueleiFan, Updated the length of the line.

Copy link
Member

@XueleiFan XueleiFan Jul 23, 2021

Choose a reason for hiding this comment

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

Thank you for the update. This PR looks good to me, and I have no further comment.

Copy link
Member

@simonis simonis left a comment

Choose a reason for hiding this comment

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

This change looks good to me except the vary minor fact that the test has no newline at the end of the file. I wonder why jcheck hasn't caught this issue. If I remember right it did it in the old Mercurial version.
I'd appreciate if you could fix that as well before integrating (and no need for a new review from my side). I'll sponsor the change one you integrate.

Thanks for this nice improvement!

@cliveverghese
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Jul 23, 2021
@openjdk
Copy link

openjdk bot commented Jul 23, 2021

@cliveverghese
Your change (at version b0375b2) is now ready to be sponsored by a Committer.

@simonis
Copy link
Member

simonis commented Jul 24, 2021

/sponsor

@openjdk
Copy link

openjdk bot commented Jul 24, 2021

Going to push as commit e627cae.
Since your change was applied there have been 149 commits pushed to the master branch:

  • 0dcfc42: Merge
  • 8adf008: 8269984: [macos] JTabbedPane title looks like disabled
  • e90ed6c: 8271173: serviceability/jvmti/GetObjectSizeClass.java doesn't check exit code
  • b4c6229: 8271189: runtime/handshake/HandshakeTimeoutTest.java can be run in driver mode
  • 7468bbc: 8266988: compiler/jvmci/compilerToVM/IsMatureTest.java fails with Unexpected isMature state for multiple times invoked method: expected false to equal true
  • 286106d: 8271219: [REDO] JDK-8271063 Print injected fields for InstanceKlass
  • a1c0a6a: 8199594: Add doc describing how (?x) ignores spaces in character classes
  • 4ed548b: 8268261: C2: assert(n != __null) failed: Bad immediate dominator info.
  • ec71e2d: 8271221: [BACKOUT] JDK-8271063 Print injected fields for InstanceKlass
  • f4b3ee5: 8270280: security/infra/java/security/cert/CertPathValidator/certification/LetsEncryptCA.java OCSP response error
  • ... and 139 more: https://git.openjdk.java.net/jdk/compare/9cac94d581f240c10fe8fff2f803109a1ae30637...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot closed this Jul 24, 2021
@openjdk openjdk bot added integrated Pull request has been integrated and removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Jul 24, 2021
@openjdk
Copy link

openjdk bot commented Jul 24, 2021

@simonis @cliveverghese Pushed as commit e627cae.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated security security-dev@openjdk.org
Development

Successfully merging this pull request may close these issues.

7 participants