Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(601)

Unified Diff: src/parser.cc

Issue 3153037: Use collector for preparse data. (Closed)
Patch Set: Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/parser.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index d305528015791287e6b2b825555a52076ab7e90c..f8292d0c450e7dabf82224694eed7e36f2d9d398 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -909,12 +909,16 @@ class ParserRecorder: public ParserLog {
virtual void LogMessage(Scanner::Location loc,
const char* message,
Vector<const char*> args);
- void WriteString(Vector<const char> str);
- static const char* ReadString(unsigned* start, int* chars);
- List<unsigned>* store() { return &store_; }
+ Vector<unsigned> ExtractData() {
+ return store_.ToVector();
+ }
private:
bool has_error_;
- List<unsigned> store_;
+ Collector<unsigned> store_;
+ Vector<unsigned> preamble_;
+
+ Collector<unsigned>* store() { return &store_; }
+ void WriteString(Vector<const char> str);
};
@@ -936,12 +940,11 @@ FunctionEntry ScriptDataImpl::GetFunctionEnd(int start) {
bool ScriptDataImpl::SanityCheck() {
- if (store_.length() < static_cast<int>(ScriptDataImpl::kHeaderSize))
- return false;
- if (magic() != ScriptDataImpl::kMagicNumber)
- return false;
- if (version() != ScriptDataImpl::kCurrentVersion)
+ if (store_.length() < static_cast<int>(ScriptDataImpl::kHeaderSize)) {
return false;
+ }
+ if (magic() != ScriptDataImpl::kMagicNumber) return false;
+ if (version() != ScriptDataImpl::kCurrentVersion) return false;
return true;
}
@@ -959,26 +962,28 @@ FunctionEntry ScriptDataImpl::nth(int n) {
ParserRecorder::ParserRecorder()
- : has_error_(false), store_(4) {
- Vector<unsigned> preamble = store()->AddBlock(0, ScriptDataImpl::kHeaderSize);
- preamble[ScriptDataImpl::kMagicOffset] = ScriptDataImpl::kMagicNumber;
- preamble[ScriptDataImpl::kVersionOffset] = ScriptDataImpl::kCurrentVersion;
- preamble[ScriptDataImpl::kHasErrorOffset] = false;
+ : has_error_(false), store_(ScriptDataImpl::kHeaderSize) {
Mads Ager (chromium) 2010/08/24 14:25:54 Four space indent.
+ preamble_ = store()->AddBlock(ScriptDataImpl::kHeaderSize, 0);
+ preamble_[ScriptDataImpl::kMagicOffset] = ScriptDataImpl::kMagicNumber;
+ preamble_[ScriptDataImpl::kVersionOffset] = ScriptDataImpl::kCurrentVersion;
+ preamble_[ScriptDataImpl::kHasErrorOffset] = false;
}
void ParserRecorder::WriteString(Vector<const char> str) {
store()->Add(str.length());
- for (int i = 0; i < str.length(); i++)
+ for (int i = 0; i < str.length(); i++) {
store()->Add(str[i]);
+ }
}
-const char* ParserRecorder::ReadString(unsigned* start, int* chars) {
+const char* ScriptDataImpl::ReadString(unsigned* start, int* chars) {
int length = start[0];
char* result = NewArray<char>(length + 1);
- for (int i = 0; i < length; i++)
+ for (int i = 0; i < length; i++) {
result[i] = start[i + 1];
+ }
result[length] = '\0';
if (chars != NULL) *chars = length;
return result;
@@ -988,14 +993,18 @@ const char* ParserRecorder::ReadString(unsigned* start, int* chars) {
void ParserRecorder::LogMessage(Scanner::Location loc, const char* message,
Vector<const char*> args) {
if (has_error_) return;
- store()->Rewind(ScriptDataImpl::kHeaderSize);
- store()->at(ScriptDataImpl::kHasErrorOffset) = true;
+ store()->Reset();
+ preamble_ = store()->AddBlock(ScriptDataImpl::kHeaderSize, 0);
+ preamble_[ScriptDataImpl::kMagicOffset] = ScriptDataImpl::kMagicNumber;
+ preamble_[ScriptDataImpl::kVersionOffset] = ScriptDataImpl::kCurrentVersion;
+ preamble_[ScriptDataImpl::kHasErrorOffset] = true;
store()->Add(loc.beg_pos);
store()->Add(loc.end_pos);
store()->Add(args.length());
WriteString(CStrVector(message));
- for (int i = 0; i < args.length(); i++)
+ for (int i = 0; i < args.length(); i++) {
WriteString(CStrVector(args[i]));
+ }
}
@@ -1008,7 +1017,7 @@ Scanner::Location ScriptDataImpl::MessageLocation() {
const char* ScriptDataImpl::BuildMessage() {
unsigned* start = ReadAddress(3);
- return ParserRecorder::ReadString(start, NULL);
+ return ReadString(start, NULL);
}
@@ -1018,7 +1027,7 @@ Vector<const char*> ScriptDataImpl::BuildArgs() {
int pos = ScriptDataImpl::kHeaderSize + Read(3);
for (int i = 0; i < arg_count; i++) {
int count = 0;
- array[i] = ParserRecorder::ReadString(ReadAddress(pos), &count);
+ array[i] = ReadString(ReadAddress(pos), &count);
pos += count + 1;
}
return Vector<const char*>(array, arg_count);
@@ -1037,7 +1046,7 @@ unsigned* ScriptDataImpl::ReadAddress(int position) {
FunctionEntry ParserRecorder::LogFunction(int start) {
if (has_error_) return FunctionEntry();
- FunctionEntry result(store()->AddBlock(0, FunctionEntry::kSize));
+ FunctionEntry result(store()->AddBlock(FunctionEntry::kSize, 0));
result.set_start_pos(start);
return result;
}
@@ -5233,10 +5242,9 @@ ScriptDataImpl* PreParse(Handle<String> source,
Bootstrapper::IsActive();
PreParser parser(no_script, allow_natives_syntax, extension);
if (!parser.PreParseProgram(source, stream)) return NULL;
- // The list owns the backing store so we need to clone the vector.
- // That way, the result will be exactly the right size rather than
- // the expected 50% too large.
- Vector<unsigned> store = parser.recorder()->store()->ToVector().Clone();
+ // Extract the accumulated data from the recorder as a single
+ // contiguous vector that we are responsible for disposing.
+ Vector<unsigned> store = parser.recorder()->ExtractData();
return new ScriptDataImpl(store);
}
« no previous file with comments | « src/parser.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698