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

Change initial capacity of options Hashmap #44

Merged
merged 1 commit into from
Sep 16, 2018

Conversation

katsadim
Copy link
Contributor

@katsadim katsadim commented Dec 9, 2017

This is a minor change that changes the initial capacity of a hashmap so there won't be a need for a rehashing operation.

Although three entries are added to the options hashmap, the initial capacity is set to two.

@codecov-io
Copy link

Codecov Report

Merging #44 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##             master      #44    +/-   ##
==========================================
  Coverage     87.73%   87.73%            
- Complexity        0      149   +149     
==========================================
  Files            23       23            
  Lines           432      432            
  Branches         39       28    -11     
==========================================
  Hits            379      379            
  Misses           49       49            
  Partials          4        4
Impacted Files Coverage Δ Complexity Δ
...ca/coglinc/gradle/plugins/javacc/JavaccPlugin.java 93.93% <100%> (ø) 9 <1> (+9) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f001e5f...35ba439. Read the comment docs.

@johnmartel johnmartel merged commit 0b80215 into javacc:master Sep 16, 2018
@@ -60,7 +60,7 @@ private void addCompileJjdocTaskToProject(Project project, Configuration configu
}

private void addTaskToProject(Project project, Class<? extends AbstractJavaccTask> type, String name, String description, String group, Configuration configuration) {
Map<String, Object> options = new HashMap<String, Object>(2);
Map<String, Object> options = new HashMap<String, Object>(3);
Copy link

Choose a reason for hiding this comment

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

Below, three objects are put into options. Constructor of HashMap which takes an int is a bit confusing, and this change won't guarantee prevention of rehashing, as the description of the PR claims. The misleading constructor of HashMap is described in detail in https://bugs.openjdk.org/browse/JDK-8284377.

JDK-8284377 was released in Java 19, so it is not yet accessible to JavaCC due to max version limited to Java 8 (see README). A more correct way of calculating capacity for a pre-sized HashMap in Java versions before Java 19 is numberOfElements / 0.75, where 0.75 is the default load factor. See, for example, change for JDK-8284377 in HostLocaleProviderAdapterImpl.java.

In this particular case, 3 / 0.75 == 4, although other places in code of JDK also add 1 on top of that:

  • Character.java:
            private static Map<String, UnicodeBlock> map =
                 new HashMap<>((int)(NUM_ENTITIES / 0.75f + 1.0f));
            [...]
                             aliases = new HashMap<>((int)(162 / 0.75f + 1.0f));
  • Class.java:
                directory = new HashMap<>((int)(universe.length / 0.75f) + 1);

Copy link
Collaborator

Choose a reason for hiding this comment

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

@rybak how did you find this issue? (It's probably not worth changing again since the impact is negligible, though it's good to know about the discrepancy.)

Copy link

Choose a reason for hiding this comment

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

I've found out about JDK-8284377 online – it was used as an example of a fairly fast contribution to JDK from a new contributor. It took less than a month between PR open and merge, and it included a CSR, which is a big deal, apparently. I stumbled upon it either on Twitter or the Inside Java podcast.

As for JavaccPlugin.java – I was reading the source of the plugin for $dayjob purposes. The exact reason is offtopic for this PR, but I might come back with a question in form of a new GitHub issue, or even a PR to improve docs, if it will be applicable.

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.

None yet

5 participants