8260053: Optimize Tokens' use of Names

Reviewed-by: mcimadamore
This commit is contained in:
Guoxiong Li 2021-01-22 15:47:05 +00:00 committed by Maurizio Cimadamore
parent 18eb6d9e34
commit 0ea5862670

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -25,17 +25,14 @@
package com.sun.tools.javac.parser; package com.sun.tools.javac.parser;
import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import com.sun.tools.javac.api.Formattable; import com.sun.tools.javac.api.Formattable;
import com.sun.tools.javac.api.Messages; import com.sun.tools.javac.api.Messages;
import com.sun.tools.javac.parser.Tokens.Token.Tag; import com.sun.tools.javac.parser.Tokens.Token.Tag;
import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.*;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Filter;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Names;
/** A class that defines codes/utilities for Java source tokens /** A class that defines codes/utilities for Java source tokens
* returned from lexical analysis. * returned from lexical analysis.
@ -52,15 +49,7 @@ public class Tokens {
/** /**
* Keyword array. Maps name indices to Token. * Keyword array. Maps name indices to Token.
*/ */
private final TokenKind[] key; private Map<String, TokenKind> keywords = new HashMap<>();
/** The number of the last entered keyword.
*/
private int maxKey = 0;
/** The names of all tokens.
*/
private Name[] tokenName = new Name[TokenKind.values().length];
public static final Context.Key<Tokens> tokensKey = new Context.Key<>(); public static final Context.Key<Tokens> tokensKey = new Context.Key<>();
@ -75,24 +64,11 @@ public class Tokens {
context.put(tokensKey, this); context.put(tokensKey, this);
names = Names.instance(context); names = Names.instance(context);
for (TokenKind t : TokenKind.values()) { for (TokenKind t : TokenKind.values()) {
if (t.name != null) if (t.name != null) {
enterKeyword(t.name, t); names.fromString(t.name);
else keywords.put(t.name, t);
tokenName[t.ordinal()] = null;
}
key = new TokenKind[maxKey+1];
for (int i = 0; i <= maxKey; i++) key[i] = TokenKind.IDENTIFIER;
for (TokenKind t : TokenKind.values()) {
if (t.name != null)
key[tokenName[t.ordinal()].getIndex()] = t;
} }
} }
private void enterKeyword(String s, TokenKind token) {
Name n = names.fromString(s);
tokenName[token.ordinal()] = n;
if (n.getIndex() > maxKey) maxKey = n.getIndex();
} }
/** /**
@ -101,11 +77,13 @@ public class Tokens {
* identifier token is returned. * identifier token is returned.
*/ */
TokenKind lookupKind(Name name) { TokenKind lookupKind(Name name) {
return (name.getIndex() > maxKey) ? TokenKind.IDENTIFIER : key[name.getIndex()]; TokenKind t = keywords.get(name.toString());
return (t != null) ? t : TokenKind.IDENTIFIER;
} }
TokenKind lookupKind(String name) { TokenKind lookupKind(String name) {
return lookupKind(names.fromString(name)); TokenKind t = keywords.get(name);
return (t != null) ? t : TokenKind.IDENTIFIER;
} }
/** /**