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

Tiny optimisations #54

Merged
merged 6 commits into from
Dec 8, 2017

Conversation

fpavageau
Copy link
Contributor

I've made a series of commits, each containing a small and simple optimization:

  • Compare enums with == instead of equals() to avoid method calls
  • Precompile regexps into Patterns (and remove duplicate classes)
  • Don't fall through a case into the default case (even if it's currently empty)
  • Create ArrayList with the correct size when known to reduce garbage
  • Use ArrayList consistently
  • Simplify the creation of an EnumSet with 2 elements

Since the exact size needed is known, it's much more efficient to size the
ArrayList correctly, as it avoids over- or under-allocating and generates
less garbage.
The list serves as the initial value of the path and is only copied into
other ArrayLists (in getPath()), never modified. It's more consistent to
also use an ArrayList here, since seeing LinkedList raises questions as to
why it was chosen. Sizing it at 0 avoids garbage, and depending on the JDK
can also kick off optimizations such as using a constant, empty array
(Oracle JDK 8, for example).

The JIT compiler might also generate slightly more efficient native code for
getPath(), as a single implementation of List will be observed, instead of
2: all method calls are monomorphic instead of bimorphic, so the code is
simpler.
@vishwakarma vishwakarma merged commit 9f36740 into flipkart-incubator:master Dec 8, 2017
@vishwakarma
Copy link
Member

Thank you @fpavageau for the PR, really appreciate it

@fpavageau fpavageau deleted the tiny_optimisations branch December 8, 2017 20:25
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