A possible improvement at Tokens and Names
Guoxiong Li
lgxbslgx at gmail.com
Wed Jan 20 17:11:22 UTC 2021
Hi Jon,
Thank you for your help.
I opened a PR at Github just now. Please see
https://github.com/openjdk/jdk/pull/2169.
Best Regards.
On Wed, Jan 20, 2021 at 10:26 PM Jonathan Gibbons <
jonathan.gibbons at oracle.com> wrote:
> Hi,
>
> I'll create a JBS issue to track this. In itself, that does not imply a
> review or an endorsement of the idea, but it does provide a place to record
> the discussion.
> https://bugs.openjdk.java.net/browse/JDK-8260053
>
> Can you post the patch as a PR?
>
> -- Jon
> On 1/20/21 4:10 AM, Guoxiong Li wrote:
>
> Hi all,
>
> When I read the code at `jdk.compiler/com.sun.tools.javac.parser.Tokens`
> and ``jdk.compiler/com.sun.tools.javac.util.Names`, I find a point which
> can be possibly improved.
>
> The constructor of the class `Tokens` invokes `Names.instance(context)` to
> initialize `Names` which would initialize many unrelated names. Then, this
> constructor uses the method `enterKeyword` to initialize the names about
> `TokenKind` and to set `maxKey`. Because `Names.instance(context)`
> initializes many unrelated names, the `Name.index` becomes very large so
> that the `maxKey` also becomes very large. Then, the constructor creates an
> array `key` by using the code `key = new TokenKind[maxKey+1]`. We can
> identify that the variable `key` is unnecessary to be so large.
>
> I would like to initialize the related names, which are about `TokenKind`,
> at the beginning of the constructor of the class `Names` to improve the
> code. The complete patch is shown at the end of this email.
>
> When using the code of the master branch, the length of the array
> `Tokens.key` is 4280. When using my patch, the length of the array
> `Tokens.key` is 383. The improvement is obvious.
>
> Could I get your help to identify whether this improvement is feasible?
> If so, could I get your help to file an issue at the bug tracker? Thanks a
> lot.
>
>
>
> The patch:
>
> diff --git
> a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/Tokens.java
> b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/Tokens.java
> index bd61dcf8ad2..3b4eb5bf4be 100644
> --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/Tokens.java
> +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/Tokens.java
> @@ -60,7 +60,7 @@ public class Tokens {
>
> /** The names of all tokens.
> */
> - private Name[] tokenName = new Name[TokenKind.values().length];
> + private Name[] tokenName;
>
> public static final Context.Key<Tokens> tokensKey = new
> Context.Key<>();
>
> @@ -74,11 +74,10 @@ public class Tokens {
> protected Tokens(Context context) {
> context.put(tokensKey, this);
> names = Names.instance(context);
> - for (TokenKind t : TokenKind.values()) {
> - if (t.name != null)
> - enterKeyword(t.name, t);
> - else
> - tokenName[t.ordinal()] = null;
> + tokenName = names.tokenName;
> + for (Name n : tokenName) {
> + if (n != null && n.getIndex() > maxKey)
> + maxKey = n.getIndex();
> }
>
> key = new TokenKind[maxKey+1];
> @@ -89,12 +88,6 @@ public class Tokens {
> }
> }
>
> - private void enterKeyword(String s, TokenKind token) {
> - Name n = names.fromString(s);
> - tokenName[token.ordinal()] = n;
> - if (n.getIndex() > maxKey) maxKey = n.getIndex();
> - }
> -
> /**
> * Create a new token given a name; if the name corresponds to a
> token name,
> * a new token of the corresponding kind is returned; otherwise, an
> diff --git
> a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java
> b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java
> index 26e0b7577aa..341817e8ff9 100644
> --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java
> +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java
> @@ -25,6 +25,8 @@
>
> package com.sun.tools.javac.util;
>
> +import com.sun.tools.javac.parser.Tokens.TokenKind;
> +
> /**
> * Access to the compiler's name table. Standard names are defined,
> * as well as methods to create new names.
> @@ -47,6 +49,9 @@ public class Names {
> return instance;
> }
>
> + // the names about TokenKind
> + public final Name[] tokenName = new Name[TokenKind.values().length];
> +
> // operators and punctuation
> public final Name asterisk;
> public final Name comma;
> @@ -220,6 +225,15 @@ public class Names {
> Options options = Options.instance(context);
> table = createTable(options);
>
> + // set the names about TokenKind
> + for (TokenKind tokenKind : TokenKind.values()) {
> + Name tempName = null;
> + if (tokenKind.name != null) {
> + tempName = fromString(tokenKind.name);
> + }
> + tokenName[tokenKind.ordinal()] = tempName;
> + }
> +
> // operators and punctuation
> asterisk = fromString("*");
> comma = fromString(",");
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210121/af211766/attachment-0001.htm>
More information about the compiler-dev
mailing list