v8/test/mjsunit/compiler/string-slice.js
Benedikt Meurer 3200cc600c [turbofan] Optimize String#slice(-1) calls.
In TurboFan we can easily recognize calls to String.prototype.slice
where the start parameter is -1 and the end parameter is either
undefined or not present. These calls either return an empty string if
the input string is empty, or the last character of the input string
as a single character string. So we can just make use of the existing
StringCharAt operator.

This reduces the overhead of the String.prototype.slice calls from
optimized code in the chai test of the web-tooling-benchmark
significantly. We observe a 2-3% improvement on the test.

Bug: v8:6936, v8:7137
Change-Id: Iebe02667446880f5760e3e8c80f8b7cc712df663
Reviewed-on: https://chromium-review.googlesource.com/795726
Commit-Queue: Benedikt Meurer <bmeurer@chromium.org>
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49704}
2017-11-29 10:55:26 +00:00

34 lines
912 B
JavaScript

// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --allow-natives-syntax
(function() {
function foo(s) { return s.slice(-1); }
assertEquals('', foo(''));
assertEquals('a', foo('a'));
assertEquals('b', foo('ab'));
assertEquals('c', foo('abc'));
%OptimizeFunctionOnNextCall(foo);
assertEquals('', foo(''));
assertEquals('a', foo('a'));
assertEquals('b', foo('ab'));
assertEquals('c', foo('abc'));
})();
(function() {
function foo(s) { return s.slice(-1, undefined); }
assertEquals('', foo(''));
assertEquals('a', foo('a'));
assertEquals('b', foo('ab'));
assertEquals('c', foo('abc'));
%OptimizeFunctionOnNextCall(foo);
assertEquals('', foo(''));
assertEquals('a', foo('a'));
assertEquals('b', foo('ab'));
assertEquals('c', foo('abc'));
})();