Revision: 20201
Author: [email protected]
Date: Mon Mar 24 14:43:41 2014 UTC
Log: Add parser-shell.
parser-shell will help to see how much PreParsing gets slower when PreParser
starts doing more checks (especially tracking variables and scopes). It will
also show how much having preparse data (or cached data) speeds up the
parsing.
[email protected]
BUG=
Review URL: https://codereview.chromium.org/209353008
http://code.google.com/p/v8/source/detail?r=20201
Added:
/branches/bleeding_edge/tools/parser-shell.cc
/branches/bleeding_edge/tools/shell-utils.h
Modified:
/branches/bleeding_edge/build/all.gyp
/branches/bleeding_edge/tools/lexer-shell.cc
/branches/bleeding_edge/tools/lexer-shell.gyp
=======================================
--- /dev/null
+++ /branches/bleeding_edge/tools/parser-shell.cc Mon Mar 24 14:43:41 2014
UTC
@@ -0,0 +1,171 @@
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided
+// with the distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived
+// from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string>
+#include <vector>
+#include "v8.h"
+
+#include "api.h"
+#include "compiler.h"
+#include "scanner-character-streams.h"
+#include "shell-utils.h"
+#include "parser.h"
+#include "preparse-data-format.h"
+#include "preparse-data.h"
+#include "preparser.h"
+
+using namespace v8::internal;
+
+enum TestMode {
+ PreParseAndParse,
+ PreParse,
+ Parse
+};
+
+std::pair<TimeDelta, TimeDelta> RunBaselineParser(
+ const char* fname, Encoding encoding, int repeat, v8::Isolate* isolate,
+ v8::Handle<v8::Context> context, TestMode test_mode) {
+ int length = 0;
+ const byte* source = ReadFileAndRepeat(fname, &length, repeat);
+ v8::Handle<v8::String> source_handle;
+ switch (encoding) {
+ case UTF8: {
+ source_handle = v8::String::NewFromUtf8(
+ isolate, reinterpret_cast<const char*>(source));
+ break;
+ }
+ case UTF16: {
+ source_handle = v8::String::NewFromTwoByte(
+ isolate, reinterpret_cast<const uint16_t*>(source),
+ v8::String::kNormalString, length / 2);
+ break;
+ }
+ case LATIN1: {
+ source_handle = v8::String::NewFromOneByte(isolate, source);
+ break;
+ }
+ }
+ v8::ScriptData* cached_data = NULL;
+ TimeDelta preparse_time, parse_time;
+ if (test_mode == PreParseAndParse || test_mode == PreParse) {
+ ElapsedTimer timer;
+ timer.Start();
+ cached_data = v8::ScriptData::PreCompile(source_handle);
+ preparse_time = timer.Elapsed();
+ if (cached_data == NULL || cached_data->HasError()) {
+ fprintf(stderr, "Preparsing failed\n");
+ return std::make_pair(TimeDelta(), TimeDelta());
+ }
+ }
+ if (test_mode == PreParseAndParse || test_mode == Parse) {
+ Handle<String> str = v8::Utils::OpenHandle(*source_handle);
+ i::Isolate* internal_isolate = str->GetIsolate();
+ Handle<Script> script = internal_isolate->factory()->NewScript(str);
+ CompilationInfoWithZone info(script);
+ info.MarkAsGlobal();
+ i::ScriptDataImpl* cached_data_impl =
+ static_cast<i::ScriptDataImpl*>(cached_data);
+ if (test_mode == PreParseAndParse) {
+ info.SetCachedData(&cached_data_impl,
+ i::CONSUME_CACHED_DATA);
+ }
+ info.SetContext(v8::Utils::OpenHandle(*context));
+ ElapsedTimer timer;
+ timer.Start();
+ // Allow lazy parsing; otherwise the preparse data won't help.
+ bool success = Parser::Parse(&info, true);
+ parse_time = timer.Elapsed();
+ if (!success) {
+ fprintf(stderr, "Parsing failed\n");
+ return std::make_pair(TimeDelta(), TimeDelta());
+ }
+ }
+ return std::make_pair(preparse_time, parse_time);
+}
+
+
+int main(int argc, char* argv[]) {
+ v8::V8::InitializeICU();
+ v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
+ Encoding encoding = LATIN1;
+ TestMode test_mode = PreParseAndParse;
+ std::vector<std::string> fnames;
+ std::string benchmark;
+ int repeat = 1;
+ for (int i = 0; i < argc; ++i) {
+ if (strcmp(argv[i], "--latin1") == 0) {
+ encoding = LATIN1;
+ } else if (strcmp(argv[i], "--utf8") == 0) {
+ encoding = UTF8;
+ } else if (strcmp(argv[i], "--utf16") == 0) {
+ encoding = UTF16;
+ } else if (strcmp(argv[i], "--preparse-and-parse") == 0) {
+ test_mode = PreParseAndParse;
+ } else if (strcmp(argv[i], "--preparse") == 0) {
+ test_mode = PreParse;
+ } else if (strcmp(argv[i], "--parse") == 0) {
+ test_mode = Parse;
+ } else if (strncmp(argv[i], "--benchmark=", 12) == 0) {
+ benchmark = std::string(argv[i]).substr(12);
+ } else if (strncmp(argv[i], "--repeat=", 9) == 0) {
+ std::string repeat_str = std::string(argv[i]).substr(9);
+ repeat = atoi(repeat_str.c_str());
+ } else if (i > 0 && argv[i][0] != '-') {
+ fnames.push_back(std::string(argv[i]));
+ }
+ }
+ v8::Isolate* isolate = v8::Isolate::GetCurrent();
+ {
+ v8::HandleScope handle_scope(isolate);
+ v8::Handle<v8::ObjectTemplate> global =
v8::ObjectTemplate::New(isolate);
+ v8::Local<v8::Context> context = v8::Context::New(isolate, NULL,
global);
+ ASSERT(!context.IsEmpty());
+ {
+ v8::Context::Scope scope(context);
+ double preparse_total = 0;
+ double parse_total = 0;
+ for (size_t i = 0; i < fnames.size(); i++) {
+ std::pair<TimeDelta, TimeDelta> time = RunBaselineParser(
+ fnames[i].c_str(), encoding, repeat, isolate, context,
test_mode);
+ preparse_total += time.first.InMillisecondsF();
+ parse_total += time.second.InMillisecondsF();
+ }
+ if (benchmark.empty()) benchmark = "Baseline";
+ printf("%s(PreParseRunTime): %.f ms\n", benchmark.c_str(),
+ preparse_total);
+ printf("%s(ParseRunTime): %.f ms\n", benchmark.c_str(), parse_total);
+ printf("%s(RunTime): %.f ms\n", benchmark.c_str(),
+ preparse_total + parse_total);
+ }
+ }
+ v8::V8::Dispose();
+ return 0;
+}
=======================================
--- /dev/null
+++ /branches/bleeding_edge/tools/shell-utils.h Mon Mar 24 14:43:41 2014 UTC
@@ -0,0 +1,67 @@
+// Copyright 2014 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided
+// with the distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived
+// from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Utility functions used by parser-shell and lexer-shell.
+
+#include <stdio.h>
+
+namespace v8 {
+namespace internal {
+
+enum Encoding {
+ LATIN1,
+ UTF8,
+ UTF16
+};
+
+const byte* ReadFileAndRepeat(const char* name, int* size, int repeat) {
+ FILE* file = fopen(name, "rb");
+ *size = 0;
+ if (file == NULL) return NULL;
+
+ fseek(file, 0, SEEK_END);
+ int file_size = ftell(file);
+ rewind(file);
+
+ *size = file_size * repeat;
+
+ byte* chars = new byte[*size + 1];
+ for (int i = 0; i < file_size;) {
+ int read = static_cast<int>(fread(&chars[i], 1, file_size - i, file));
+ i += read;
+ }
+ fclose(file);
+
+ for (int i = file_size; i < *size; i++) {
+ chars[i] = chars[i - file_size];
+ }
+ chars[*size] = 0;
+
+ return chars;
+}
+
+} } // namespace v8::internal
=======================================
--- /branches/bleeding_edge/build/all.gyp Wed Jan 15 14:24:32 2014 UTC
+++ /branches/bleeding_edge/build/all.gyp Mon Mar 24 14:43:41 2014 UTC
@@ -16,6 +16,7 @@
['component!="shared_library"', {
'dependencies': [
'../tools/lexer-shell.gyp:lexer-shell',
+ '../tools/lexer-shell.gyp:parser-shell',
],
}],
]
=======================================
--- /branches/bleeding_edge/tools/lexer-shell.cc Wed Jan 8 06:53:31 2014
UTC
+++ /branches/bleeding_edge/tools/lexer-shell.cc Mon Mar 24 14:43:41 2014
UTC
@@ -26,7 +26,6 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <assert.h>
-#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
@@ -35,53 +34,18 @@
#include "v8.h"
#include "api.h"
-#include "ast.h"
-#include "char-predicates-inl.h"
#include "messages.h"
#include "platform.h"
#include "runtime.h"
#include "scanner-character-streams.h"
#include "scopeinfo.h"
+#include "shell-utils.h"
#include "string-stream.h"
#include "scanner.h"
using namespace v8::internal;
-enum Encoding {
- LATIN1,
- UTF8,
- UTF16
-};
-
-
-const byte* ReadFile(const char* name, Isolate* isolate,
- int* size, int repeat) {
- FILE* file = fopen(name, "rb");
- *size = 0;
- if (file == NULL) return NULL;
-
- fseek(file, 0, SEEK_END);
- int file_size = ftell(file);
- rewind(file);
-
- *size = file_size * repeat;
-
- byte* chars = new byte[*size + 1];
- for (int i = 0; i < file_size;) {
- int read = static_cast<int>(fread(&chars[i], 1, file_size - i, file));
- i += read;
- }
- fclose(file);
-
- for (int i = file_size; i < *size; i++) {
- chars[i] = chars[i - file_size];
- }
- chars[*size] = 0;
-
- return chars;
-}
-
class BaselineScanner {
public:
@@ -92,7 +56,7 @@
int repeat)
: stream_(NULL) {
int length = 0;
- source_ = ReadFile(fname, isolate, &length, repeat);
+ source_ = ReadFileAndRepeat(fname, &length, repeat);
unicode_cache_ = new UnicodeCache();
scanner_ = new Scanner(unicode_cache_);
switch (encoding) {
=======================================
--- /branches/bleeding_edge/tools/lexer-shell.gyp Wed Nov 13 12:26:09 2013
UTC
+++ /branches/bleeding_edge/tools/lexer-shell.gyp Mon Mar 24 14:43:41 2014
UTC
@@ -51,6 +51,29 @@
],
'sources': [
'lexer-shell.cc',
+ 'shell-utils.h',
+ ],
+ },
+ {
+ 'target_name': 'parser-shell',
+ 'type': 'executable',
+ 'dependencies': [
+ '../tools/gyp/v8.gyp:v8',
+ ],
+ 'conditions': [
+ ['v8_enable_i18n_support==1', {
+ 'dependencies': [
+ '<(icu_gyp_path):icui18n',
+ '<(icu_gyp_path):icuuc',
+ ],
+ }],
+ ],
+ 'include_dirs+': [
+ '../src',
+ ],
+ 'sources': [
+ 'parser-shell.cc',
+ 'shell-utils.h',
],
},
],
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.