Skip to content

Commit 6b3ec9d

Browse files
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 git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@20201 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
1 parent fd4bbdf commit 6b3ec9d

File tree

5 files changed

+264
-38
lines changed

5 files changed

+264
-38
lines changed

build/all.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
['component!="shared_library"', {
1717
'dependencies': [
1818
'../tools/lexer-shell.gyp:lexer-shell',
19+
'../tools/lexer-shell.gyp:parser-shell',
1920
],
2021
}],
2122
]

tools/lexer-shell.cc

+2-38
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727

2828
#include <assert.h>
29-
#include <fcntl.h>
3029
#include <string.h>
3130
#include <stdio.h>
3231
#include <stdlib.h>
@@ -35,53 +34,18 @@
3534
#include "v8.h"
3635

3736
#include "api.h"
38-
#include "ast.h"
39-
#include "char-predicates-inl.h"
4037
#include "messages.h"
4138
#include "platform.h"
4239
#include "runtime.h"
4340
#include "scanner-character-streams.h"
4441
#include "scopeinfo.h"
42+
#include "shell-utils.h"
4543
#include "string-stream.h"
4644
#include "scanner.h"
4745

4846

4947
using namespace v8::internal;
5048

51-
enum Encoding {
52-
LATIN1,
53-
UTF8,
54-
UTF16
55-
};
56-
57-
58-
const byte* ReadFile(const char* name, Isolate* isolate,
59-
int* size, int repeat) {
60-
FILE* file = fopen(name, "rb");
61-
*size = 0;
62-
if (file == NULL) return NULL;
63-
64-
fseek(file, 0, SEEK_END);
65-
int file_size = ftell(file);
66-
rewind(file);
67-
68-
*size = file_size * repeat;
69-
70-
byte* chars = new byte[*size + 1];
71-
for (int i = 0; i < file_size;) {
72-
int read = static_cast<int>(fread(&chars[i], 1, file_size - i, file));
73-
i += read;
74-
}
75-
fclose(file);
76-
77-
for (int i = file_size; i < *size; i++) {
78-
chars[i] = chars[i - file_size];
79-
}
80-
chars[*size] = 0;
81-
82-
return chars;
83-
}
84-
8549

8650
class BaselineScanner {
8751
public:
@@ -92,7 +56,7 @@ class BaselineScanner {
9256
int repeat)
9357
: stream_(NULL) {
9458
int length = 0;
95-
source_ = ReadFile(fname, isolate, &length, repeat);
59+
source_ = ReadFileAndRepeat(fname, &length, repeat);
9660
unicode_cache_ = new UnicodeCache();
9761
scanner_ = new Scanner(unicode_cache_);
9862
switch (encoding) {

tools/lexer-shell.gyp

+23
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,29 @@
5151
],
5252
'sources': [
5353
'lexer-shell.cc',
54+
'shell-utils.h',
55+
],
56+
},
57+
{
58+
'target_name': 'parser-shell',
59+
'type': 'executable',
60+
'dependencies': [
61+
'../tools/gyp/v8.gyp:v8',
62+
],
63+
'conditions': [
64+
['v8_enable_i18n_support==1', {
65+
'dependencies': [
66+
'<(icu_gyp_path):icui18n',
67+
'<(icu_gyp_path):icuuc',
68+
],
69+
}],
70+
],
71+
'include_dirs+': [
72+
'../src',
73+
],
74+
'sources': [
75+
'parser-shell.cc',
76+
'shell-utils.h',
5477
],
5578
},
5679
],

tools/parser-shell.cc

+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// Copyright 2014 the V8 project authors. All rights reserved.
2+
// Redistribution and use in source and binary forms, with or without
3+
// modification, are permitted provided that the following conditions are
4+
// met:
5+
//
6+
// * Redistributions of source code must retain the above copyright
7+
// notice, this list of conditions and the following disclaimer.
8+
// * Redistributions in binary form must reproduce the above
9+
// copyright notice, this list of conditions and the following
10+
// disclaimer in the documentation and/or other materials provided
11+
// with the distribution.
12+
// * Neither the name of Google Inc. nor the names of its
13+
// contributors may be used to endorse or promote products derived
14+
// from this software without specific prior written permission.
15+
//
16+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
28+
#include <assert.h>
29+
#include <string.h>
30+
#include <stdio.h>
31+
#include <stdlib.h>
32+
#include <string>
33+
#include <vector>
34+
#include "v8.h"
35+
36+
#include "api.h"
37+
#include "compiler.h"
38+
#include "scanner-character-streams.h"
39+
#include "shell-utils.h"
40+
#include "parser.h"
41+
#include "preparse-data-format.h"
42+
#include "preparse-data.h"
43+
#include "preparser.h"
44+
45+
using namespace v8::internal;
46+
47+
enum TestMode {
48+
PreParseAndParse,
49+
PreParse,
50+
Parse
51+
};
52+
53+
std::pair<TimeDelta, TimeDelta> RunBaselineParser(
54+
const char* fname, Encoding encoding, int repeat, v8::Isolate* isolate,
55+
v8::Handle<v8::Context> context, TestMode test_mode) {
56+
int length = 0;
57+
const byte* source = ReadFileAndRepeat(fname, &length, repeat);
58+
v8::Handle<v8::String> source_handle;
59+
switch (encoding) {
60+
case UTF8: {
61+
source_handle = v8::String::NewFromUtf8(
62+
isolate, reinterpret_cast<const char*>(source));
63+
break;
64+
}
65+
case UTF16: {
66+
source_handle = v8::String::NewFromTwoByte(
67+
isolate, reinterpret_cast<const uint16_t*>(source),
68+
v8::String::kNormalString, length / 2);
69+
break;
70+
}
71+
case LATIN1: {
72+
source_handle = v8::String::NewFromOneByte(isolate, source);
73+
break;
74+
}
75+
}
76+
v8::ScriptData* cached_data = NULL;
77+
TimeDelta preparse_time, parse_time;
78+
if (test_mode == PreParseAndParse || test_mode == PreParse) {
79+
ElapsedTimer timer;
80+
timer.Start();
81+
cached_data = v8::ScriptData::PreCompile(source_handle);
82+
preparse_time = timer.Elapsed();
83+
if (cached_data == NULL || cached_data->HasError()) {
84+
fprintf(stderr, "Preparsing failed\n");
85+
return std::make_pair(TimeDelta(), TimeDelta());
86+
}
87+
}
88+
if (test_mode == PreParseAndParse || test_mode == Parse) {
89+
Handle<String> str = v8::Utils::OpenHandle(*source_handle);
90+
i::Isolate* internal_isolate = str->GetIsolate();
91+
Handle<Script> script = internal_isolate->factory()->NewScript(str);
92+
CompilationInfoWithZone info(script);
93+
info.MarkAsGlobal();
94+
i::ScriptDataImpl* cached_data_impl =
95+
static_cast<i::ScriptDataImpl*>(cached_data);
96+
if (test_mode == PreParseAndParse) {
97+
info.SetCachedData(&cached_data_impl,
98+
i::CONSUME_CACHED_DATA);
99+
}
100+
info.SetContext(v8::Utils::OpenHandle(*context));
101+
ElapsedTimer timer;
102+
timer.Start();
103+
// Allow lazy parsing; otherwise the preparse data won't help.
104+
bool success = Parser::Parse(&info, true);
105+
parse_time = timer.Elapsed();
106+
if (!success) {
107+
fprintf(stderr, "Parsing failed\n");
108+
return std::make_pair(TimeDelta(), TimeDelta());
109+
}
110+
}
111+
return std::make_pair(preparse_time, parse_time);
112+
}
113+
114+
115+
int main(int argc, char* argv[]) {
116+
v8::V8::InitializeICU();
117+
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
118+
Encoding encoding = LATIN1;
119+
TestMode test_mode = PreParseAndParse;
120+
std::vector<std::string> fnames;
121+
std::string benchmark;
122+
int repeat = 1;
123+
for (int i = 0; i < argc; ++i) {
124+
if (strcmp(argv[i], "--latin1") == 0) {
125+
encoding = LATIN1;
126+
} else if (strcmp(argv[i], "--utf8") == 0) {
127+
encoding = UTF8;
128+
} else if (strcmp(argv[i], "--utf16") == 0) {
129+
encoding = UTF16;
130+
} else if (strcmp(argv[i], "--preparse-and-parse") == 0) {
131+
test_mode = PreParseAndParse;
132+
} else if (strcmp(argv[i], "--preparse") == 0) {
133+
test_mode = PreParse;
134+
} else if (strcmp(argv[i], "--parse") == 0) {
135+
test_mode = Parse;
136+
} else if (strncmp(argv[i], "--benchmark=", 12) == 0) {
137+
benchmark = std::string(argv[i]).substr(12);
138+
} else if (strncmp(argv[i], "--repeat=", 9) == 0) {
139+
std::string repeat_str = std::string(argv[i]).substr(9);
140+
repeat = atoi(repeat_str.c_str());
141+
} else if (i > 0 && argv[i][0] != '-') {
142+
fnames.push_back(std::string(argv[i]));
143+
}
144+
}
145+
v8::Isolate* isolate = v8::Isolate::GetCurrent();
146+
{
147+
v8::HandleScope handle_scope(isolate);
148+
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
149+
v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global);
150+
ASSERT(!context.IsEmpty());
151+
{
152+
v8::Context::Scope scope(context);
153+
double preparse_total = 0;
154+
double parse_total = 0;
155+
for (size_t i = 0; i < fnames.size(); i++) {
156+
std::pair<TimeDelta, TimeDelta> time = RunBaselineParser(
157+
fnames[i].c_str(), encoding, repeat, isolate, context, test_mode);
158+
preparse_total += time.first.InMillisecondsF();
159+
parse_total += time.second.InMillisecondsF();
160+
}
161+
if (benchmark.empty()) benchmark = "Baseline";
162+
printf("%s(PreParseRunTime): %.f ms\n", benchmark.c_str(),
163+
preparse_total);
164+
printf("%s(ParseRunTime): %.f ms\n", benchmark.c_str(), parse_total);
165+
printf("%s(RunTime): %.f ms\n", benchmark.c_str(),
166+
preparse_total + parse_total);
167+
}
168+
}
169+
v8::V8::Dispose();
170+
return 0;
171+
}

tools/shell-utils.h

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2014 the V8 project authors. All rights reserved.
2+
// Redistribution and use in source and binary forms, with or without
3+
// modification, are permitted provided that the following conditions are
4+
// met:
5+
//
6+
// * Redistributions of source code must retain the above copyright
7+
// notice, this list of conditions and the following disclaimer.
8+
// * Redistributions in binary form must reproduce the above
9+
// copyright notice, this list of conditions and the following
10+
// disclaimer in the documentation and/or other materials provided
11+
// with the distribution.
12+
// * Neither the name of Google Inc. nor the names of its
13+
// contributors may be used to endorse or promote products derived
14+
// from this software without specific prior written permission.
15+
//
16+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
28+
// Utility functions used by parser-shell and lexer-shell.
29+
30+
#include <stdio.h>
31+
32+
namespace v8 {
33+
namespace internal {
34+
35+
enum Encoding {
36+
LATIN1,
37+
UTF8,
38+
UTF16
39+
};
40+
41+
const byte* ReadFileAndRepeat(const char* name, int* size, int repeat) {
42+
FILE* file = fopen(name, "rb");
43+
*size = 0;
44+
if (file == NULL) return NULL;
45+
46+
fseek(file, 0, SEEK_END);
47+
int file_size = ftell(file);
48+
rewind(file);
49+
50+
*size = file_size * repeat;
51+
52+
byte* chars = new byte[*size + 1];
53+
for (int i = 0; i < file_size;) {
54+
int read = static_cast<int>(fread(&chars[i], 1, file_size - i, file));
55+
i += read;
56+
}
57+
fclose(file);
58+
59+
for (int i = file_size; i < *size; i++) {
60+
chars[i] = chars[i - file_size];
61+
}
62+
chars[*size] = 0;
63+
64+
return chars;
65+
}
66+
67+
} } // namespace v8::internal

0 commit comments

Comments
 (0)