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

Side by Side Diff: src/preparser-api.cc

Issue 5545006: Optimized scanner to avoid virtual calls for every character read. (Closed)
Patch Set: Created 10 years 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 unified diff | Download patch
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 21 matching lines...) Expand all
32 #include "utils.h" 32 #include "utils.h"
33 #include "list.h" 33 #include "list.h"
34 #include "scanner-base.h" 34 #include "scanner-base.h"
35 #include "preparse-data.h" 35 #include "preparse-data.h"
36 #include "preparser.h" 36 #include "preparser.h"
37 37
38 namespace v8 { 38 namespace v8 {
39 namespace internal { 39 namespace internal {
40 40
41 // UTF16Buffer based on a v8::UnicodeInputStream. 41 // UTF16Buffer based on a v8::UnicodeInputStream.
42 class InputStreamUTF16Buffer : public UTF16Buffer { 42 class InputStreamUTF16Buffer : public UC16CharacterStream {
43 public: 43 public:
44 explicit InputStreamUTF16Buffer(UnicodeInputStream* stream) 44 explicit InputStreamUTF16Buffer(UnicodeInputStream* stream)
45 : UTF16Buffer(), 45 : UC16CharacterStream(),
46 stream_(stream) { } 46 stream_(stream),
47 pushback_active_(false) {
48 buffer_cursor_ = buffer_end_ = buffer_;
49 ReadBlock();
50 }
47 51
48 virtual ~InputStreamUTF16Buffer() { } 52 virtual ~InputStreamUTF16Buffer() { }
49 53
50 virtual void PushBack(uc32 ch) { 54 virtual void PushBack(uc16 ch) {
55 if (buffer_cursor_ > buffer_) {
56 // While we can stay within the buffer, just do so.
57 buffer_cursor_--;
58 ASSERT_EQ(*buffer_cursor_, ch);
59 return;
60 }
Erik Corry 2010/12/07 12:27:30 Can the contents of this first if be put in a non-
Lasse Reichstein 2010/12/07 14:05:54 Pushback is a rare operation. I doubt there is any
61 if (!pushback_active_) {
62 // Push back the entire buffer to the stream and let the
63 // stream handle pushbacks from now.
64 // We leave buffer_cursor_ == buffer_end_, so the next read
65 // will fill the buffer from the current position.
66 while (buffer_end_ > buffer_) {
67 stream_->PushBack(*--buffer_end_);
68 }
Erik Corry 2010/12/07 12:27:30 Do we need a PushBackBlock method? This looks slo
Lasse Reichstein 2010/12/07 14:05:54 The interface should be reconsidered. It might not
69 pushback_active_ = true;
70 }
51 stream_->PushBack(ch); 71 stream_->PushBack(ch);
52 pos_--; 72 pos_--;
53 } 73 }
54 74
55 virtual uc32 Advance() { 75 protected:
56 uc32 result = stream_->Next(); 76 virtual bool ReadBlock() {
57 if (result >= 0) pos_++; 77 pushback_active_ = false;
58 return result; 78 int32_t value;
79 buffer_cursor_ = buffer_end_ = buffer_;
80 while ((value = stream_->Next()) >= 0) {
81 if (value > static_cast<int32_t>(unibrow::Utf8::kMaxThreeByteChar)) {
82 value = unibrow::Utf8::kBadChar;
83 }
84 // buffer_end_ is a const pointer, but buffer_ is writable.
85 buffer_[buffer_end_++ - buffer_] = static_cast<uc16>(value);
86 if (buffer_end_ == buffer_ + kBufferSize) break;
87 }
88 return buffer_end_ > buffer_;
59 } 89 }
60 90
61 virtual void SeekForward(int pos) { 91 virtual unsigned SlowSeekForward(unsigned pos) {
62 // Seeking in the input is not used by preparsing. 92 // Seeking in the input is not used by preparsing.
63 // It's only used by the real parser based on preparser data. 93 // It's only used by the real parser based on preparser data.
64 UNIMPLEMENTED(); 94 UNIMPLEMENTED();
95 return 0;
65 } 96 }
66 97
67 private: 98 private:
99 static const unsigned kBufferSize = 512;
68 v8::UnicodeInputStream* const stream_; 100 v8::UnicodeInputStream* const stream_;
101 uc16 buffer_[kBufferSize];
102 bool pushback_active_;
69 }; 103 };
70 104
71 105
72 class StandAloneJavaScriptScanner : public JavaScriptScanner { 106 class StandAloneJavaScriptScanner : public JavaScriptScanner {
73 public: 107 public:
74 void Initialize(UTF16Buffer* source) { 108 void Initialize(UC16CharacterStream* source) {
75 source_ = source; 109 source_ = source;
76 literal_flags_ = kLiteralString | kLiteralIdentifier; 110 literal_flags_ = kLiteralString | kLiteralIdentifier;
77 Init(); 111 Init();
78 // Skip initial whitespace allowing HTML comment ends just like 112 // Skip initial whitespace allowing HTML comment ends just like
79 // after a newline and scan first token. 113 // after a newline and scan first token.
80 has_line_terminator_before_next_ = true; 114 has_line_terminator_before_next_ = true;
81 SkipWhiteSpace(); 115 SkipWhiteSpace();
82 Scan(); 116 Scan();
83 } 117 }
84 }; 118 };
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 return PreParserData(size, data); 153 return PreParserData(size, data);
120 } 154 }
121 155
122 } // namespace v8. 156 } // namespace v8.
123 157
124 158
125 // Used by ASSERT macros and other immediate exits. 159 // Used by ASSERT macros and other immediate exits.
126 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) { 160 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) {
127 exit(EXIT_FAILURE); 161 exit(EXIT_FAILURE);
128 } 162 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698