]> Cypherpunks repositories - gostls13.git/commitdiff
go/token: slight performance improvement for IsIdentifier
authorchanxuehong <chanxuehong@gmail.com>
Mon, 21 Feb 2022 08:51:05 +0000 (08:51 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Wed, 2 Mar 2022 11:28:40 +0000 (11:28 +0000)
If name is empty or a keyword, we can skip the loop entirely.
Otherwise, we do the same amount of work as before.

Here is the benchmark result for go/parser:

name          old time/op    new time/op    delta
Parse-12        2.53ms ± 2%    2.47ms ± 1%  -2.38%  (p=0.000 n=9+10)
ParseOnly-12    1.97ms ± 1%    1.93ms ± 2%  -1.80%  (p=0.000 n=10+10)
Resolve-12       560µs ± 1%     558µs ± 1%    ~     (p=0.200 n=9+8)

name          old speed      new speed      delta
Parse-12      26.1MB/s ± 2%  26.8MB/s ± 1%  +2.44%  (p=0.000 n=9+10)
ParseOnly-12  33.6MB/s ± 1%  34.3MB/s ± 2%  +1.82%  (p=0.000 n=10+10)
Resolve-12     118MB/s ± 2%   119MB/s ± 1%    ~     (p=0.116 n=10+8)

Change-Id: I87ac9c2637a6c0e697382b74245ac88ef523bba7
GitHub-Last-Rev: 036bc38d837c095dd5a8d97ece83e1596d875d3e
GitHub-Pull-Request: golang/go#48534
Reviewed-on: https://go-review.googlesource.com/c/go/+/351389
Trust: David Chase <drchase@google.com>
Trust: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/token/token.go

index d22e575661a2df95726266b65c512fbdda7c5838..dd0f4f82345ef8ee4eac0b74d637ff1c01726fd4 100644 (file)
@@ -340,10 +340,13 @@ func IsKeyword(name string) bool {
 // is not a digit. Keywords are not identifiers.
 //
 func IsIdentifier(name string) bool {
+       if name == "" || IsKeyword(name) {
+               return false
+       }
        for i, c := range name {
                if !unicode.IsLetter(c) && c != '_' && (i == 0 || !unicode.IsDigit(c)) {
                        return false
                }
        }
-       return name != "" && !IsKeyword(name)
+       return true
 }