8025515: Performance issues with Source.getLine()

Fri, 27 Sep 2013 16:59:01 +0200

author
hannesw
date
Fri, 27 Sep 2013 16:59:01 +0200
changeset 582
2016a6b9e1f3
parent 576
23958764f866
child 583
1809c9e97c71

8025515: Performance issues with Source.getLine()
Reviewed-by: sundar, lagergren

src/jdk/nashorn/internal/codegen/CodeGenerator.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/ir/LexicalContext.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/parser/Parser.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/runtime/Source.java file | annotate | diff | comparison | revisions
test/script/basic/JDK-8025515.js file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Thu Sep 26 11:47:24 2013 +0200
     1.2 +++ b/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Fri Sep 27 16:59:01 2013 +0200
     1.3 @@ -1956,7 +1956,7 @@
     1.4  
     1.5          final Expression expression = throwNode.getExpression();
     1.6          final int        position   = throwNode.position();
     1.7 -        final int        line       = source.getLine(position);
     1.8 +        final int        line       = throwNode.getLineNumber();
     1.9          final int        column     = source.getColumn(position);
    1.10  
    1.11          load(expression);
     2.1 --- a/src/jdk/nashorn/internal/ir/LexicalContext.java	Thu Sep 26 11:47:24 2013 +0200
     2.2 +++ b/src/jdk/nashorn/internal/ir/LexicalContext.java	Fri Sep 27 16:59:01 2013 +0200
     2.3 @@ -587,11 +587,11 @@
     2.4                  final FunctionNode fn = (FunctionNode)node;
     2.5                  final Source source = fn.getSource();
     2.6                  String src = source.toString();
     2.7 -                if (src.indexOf(File.pathSeparator) != -1) {
     2.8 +                if (src.contains(File.pathSeparator)) {
     2.9                      src = src.substring(src.lastIndexOf(File.pathSeparator));
    2.10                  }
    2.11                  src += ' ';
    2.12 -                src += source.getLine(fn.getStart());
    2.13 +                src += fn.getLineNumber();
    2.14                  sb.append(src);
    2.15              }
    2.16              sb.append(' ');
     3.1 --- a/src/jdk/nashorn/internal/parser/Parser.java	Thu Sep 26 11:47:24 2013 +0200
     3.2 +++ b/src/jdk/nashorn/internal/parser/Parser.java	Fri Sep 27 16:59:01 2013 +0200
     3.3 @@ -2436,7 +2436,7 @@
     3.4          // name is null, generate anonymous name
     3.5          boolean isAnonymous = false;
     3.6          if (name == null) {
     3.7 -            final String tmpName = "_L" + source.getLine(Token.descPosition(token));
     3.8 +            final String tmpName = "_L" + functionLine;
     3.9              name = new IdentNode(functionToken, Token.descPosition(functionToken), tmpName);
    3.10              isAnonymous = true;
    3.11          }
     4.1 --- a/src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java	Thu Sep 26 11:47:24 2013 +0200
     4.2 +++ b/src/jdk/nashorn/internal/runtime/RecompilableScriptFunctionData.java	Fri Sep 27 16:59:01 2013 +0200
     4.3 @@ -132,7 +132,7 @@
     4.4          if (source != null) {
     4.5              sb.append(source.getName())
     4.6                  .append(':')
     4.7 -                .append(source.getLine(Token.descPosition(token)))
     4.8 +                .append(functionNode.getLineNumber())
     4.9                  .append(' ');
    4.10          }
    4.11  
     5.1 --- a/src/jdk/nashorn/internal/runtime/Source.java	Thu Sep 26 11:47:24 2013 +0200
     5.2 +++ b/src/jdk/nashorn/internal/runtime/Source.java	Fri Sep 27 16:59:01 2013 +0200
     5.3 @@ -272,6 +272,10 @@
     5.4  
     5.5      /**
     5.6       * Return line number of character position.
     5.7 +     *
     5.8 +     * <p>This method can be expensive for large sources as it iterates through
     5.9 +     * all characters up to {@code position}.</p>
    5.10 +     *
    5.11       * @param position Position of character in source content.
    5.12       * @return Line number.
    5.13       */
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/test/script/basic/JDK-8025515.js	Fri Sep 27 16:59:01 2013 +0200
     6.3 @@ -0,0 +1,58 @@
     6.4 +/*
     6.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     6.7 + * 
     6.8 + * This code is free software; you can redistribute it and/or modify it
     6.9 + * under the terms of the GNU General Public License version 2 only, as
    6.10 + * published by the Free Software Foundation.
    6.11 + * 
    6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    6.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    6.15 + * version 2 for more details (a copy is included in the LICENSE file that
    6.16 + * accompanied this code).
    6.17 + * 
    6.18 + * You should have received a copy of the GNU General Public License version
    6.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    6.21 + * 
    6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    6.23 + * or visit www.oracle.com if you need additional information or have any
    6.24 + * questions.
    6.25 + */
    6.26 +
    6.27 +/**
    6.28 + * JDK-8025515: Performance issues with Source.getLine()
    6.29 + *
    6.30 + * @test
    6.31 + * @run
    6.32 + */
    6.33 +
    6.34 +// Make sure synthetic names of anonymous functions have correct line numbers
    6.35 +
    6.36 +function testMethodName(f, expected) {
    6.37 +    try {
    6.38 +        f();
    6.39 +        fail("expected error");
    6.40 +    } catch (e) {
    6.41 +        var stack = e.getStackTrace();
    6.42 +        if (stack[0].methodName !== expected) {
    6.43 +            fail("got " + stack[0].methodName + ", expected " + expected);
    6.44 +        }
    6.45 +    }
    6.46 +}
    6.47 +
    6.48 +testMethodName(function() {
    6.49 +    return a.b.c;
    6.50 +}, "_L45");
    6.51 +
    6.52 +testMethodName(function() { throw new Error() }, "_L49");
    6.53 +
    6.54 +var f = (function() {
    6.55 +    return function() { a.b.c; };
    6.56 +})();
    6.57 +testMethodName(f, "_L51$_L52");
    6.58 +
    6.59 +testMethodName((function() {
    6.60 +    return function() { return a.b.c; };
    6.61 +})(), "_L56$_L57");

mercurial