OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 <fcntl.h> |
| 30 #include <string.h> |
| 31 #include <stdio.h> |
| 32 #include <stdlib.h> |
| 33 #include <string> |
| 34 #include <vector> |
| 35 #include "v8.h" |
| 36 |
| 37 #include "api.h" |
| 38 #include "ast.h" |
| 39 #include "char-predicates-inl.h" |
| 40 #include "messages.h" |
| 41 #include "platform.h" |
| 42 #include "runtime.h" |
| 43 #include "scanner-character-streams.h" |
| 44 #include "scopeinfo.h" |
| 45 #include "string-stream.h" |
| 46 #include "scanner.h" |
| 47 |
| 48 |
| 49 using namespace v8::internal; |
| 50 |
| 51 enum Encoding { |
| 52 LATIN1, |
| 53 UTF8, |
| 54 UTF16 |
| 55 }; |
| 56 |
| 57 |
| 58 const byte* ReadFile(const char* name, Isolate* isolate, int* size) { |
| 59 FILE* file = fopen(name, "rb"); |
| 60 *size = 0; |
| 61 if (file == NULL) return NULL; |
| 62 |
| 63 fseek(file, 0, SEEK_END); |
| 64 *size = ftell(file); |
| 65 rewind(file); |
| 66 |
| 67 byte* chars = new byte[*size + 1]; |
| 68 chars[*size] = 0; |
| 69 for (int i = 0; i < *size;) { |
| 70 int read = static_cast<int>(fread(&chars[i], 1, *size - i, file)); |
| 71 i += read; |
| 72 } |
| 73 fclose(file); |
| 74 return chars; |
| 75 } |
| 76 |
| 77 |
| 78 class BaselineScanner { |
| 79 public: |
| 80 BaselineScanner(const char* fname, |
| 81 Isolate* isolate, |
| 82 Encoding encoding, |
| 83 ElapsedTimer* timer) |
| 84 : stream_(NULL) { |
| 85 int length = 0; |
| 86 source_ = ReadFile(fname, isolate, &length); |
| 87 unicode_cache_ = new UnicodeCache(); |
| 88 scanner_ = new Scanner(unicode_cache_); |
| 89 switch (encoding) { |
| 90 case UTF8: |
| 91 stream_ = new Utf8ToUtf16CharacterStream(source_, length); |
| 92 break; |
| 93 case UTF16: { |
| 94 Handle<String> result = isolate->factory()->NewStringFromTwoByte( |
| 95 Vector<const uint16_t>( |
| 96 reinterpret_cast<const uint16_t*>(source_), |
| 97 length / 2)); |
| 98 stream_ = |
| 99 new GenericStringUtf16CharacterStream(result, 0, result->length()); |
| 100 break; |
| 101 } |
| 102 case LATIN1: { |
| 103 Handle<String> result = isolate->factory()->NewStringFromOneByte( |
| 104 Vector<const uint8_t>(source_, length)); |
| 105 stream_ = |
| 106 new GenericStringUtf16CharacterStream(result, 0, result->length()); |
| 107 break; |
| 108 } |
| 109 } |
| 110 timer->Start(); |
| 111 scanner_->Initialize(stream_); |
| 112 } |
| 113 |
| 114 ~BaselineScanner() { |
| 115 delete scanner_; |
| 116 delete stream_; |
| 117 delete unicode_cache_; |
| 118 delete[] source_; |
| 119 } |
| 120 |
| 121 Token::Value Next(int* beg_pos, int* end_pos) { |
| 122 Token::Value res = scanner_->Next(); |
| 123 *beg_pos = scanner_->location().beg_pos; |
| 124 *end_pos = scanner_->location().end_pos; |
| 125 return res; |
| 126 } |
| 127 |
| 128 private: |
| 129 UnicodeCache* unicode_cache_; |
| 130 Scanner* scanner_; |
| 131 const byte* source_; |
| 132 BufferedUtf16CharacterStream* stream_; |
| 133 }; |
| 134 |
| 135 |
| 136 struct TokenWithLocation { |
| 137 Token::Value value; |
| 138 size_t beg; |
| 139 size_t end; |
| 140 TokenWithLocation() : value(Token::ILLEGAL), beg(0), end(0) { } |
| 141 TokenWithLocation(Token::Value value, size_t beg, size_t end) : |
| 142 value(value), beg(beg), end(end) { } |
| 143 bool operator==(const TokenWithLocation& other) { |
| 144 return value == other.value && beg == other.beg && end == other.end; |
| 145 } |
| 146 bool operator!=(const TokenWithLocation& other) { |
| 147 return !(*this == other); |
| 148 } |
| 149 void Print(const char* prefix) const { |
| 150 printf("%s %11s at (%d, %d)\n", |
| 151 prefix, Token::Name(value), |
| 152 static_cast<int>(beg), static_cast<int>(end)); |
| 153 } |
| 154 }; |
| 155 |
| 156 |
| 157 TimeDelta RunBaselineScanner(const char* fname, |
| 158 Isolate* isolate, |
| 159 Encoding encoding, |
| 160 bool dump_tokens, |
| 161 std::vector<TokenWithLocation>* tokens) { |
| 162 ElapsedTimer timer; |
| 163 BaselineScanner scanner(fname, isolate, encoding, &timer); |
| 164 Token::Value token; |
| 165 int beg, end; |
| 166 do { |
| 167 token = scanner.Next(&beg, &end); |
| 168 if (dump_tokens) { |
| 169 tokens->push_back(TokenWithLocation(token, beg, end)); |
| 170 } |
| 171 } while (token != Token::EOS); |
| 172 return timer.Elapsed(); |
| 173 } |
| 174 |
| 175 |
| 176 void PrintTokens(const char* name, |
| 177 const std::vector<TokenWithLocation>& tokens) { |
| 178 printf("No of tokens: %d\n", |
| 179 static_cast<int>(tokens.size())); |
| 180 printf("%s:\n", name); |
| 181 for (size_t i = 0; i < tokens.size(); ++i) { |
| 182 tokens[i].Print("=>"); |
| 183 } |
| 184 } |
| 185 |
| 186 |
| 187 TimeDelta ProcessFile( |
| 188 const char* fname, |
| 189 Encoding encoding, |
| 190 Isolate* isolate, |
| 191 bool print_tokens) { |
| 192 if (print_tokens) { |
| 193 printf("Processing file %s\n", fname); |
| 194 } |
| 195 HandleScope handle_scope(isolate); |
| 196 std::vector<TokenWithLocation> baseline_tokens; |
| 197 TimeDelta baseline_time; |
| 198 baseline_time = RunBaselineScanner( |
| 199 fname, isolate, encoding, print_tokens, |
| 200 &baseline_tokens); |
| 201 if (print_tokens) { |
| 202 PrintTokens("Baseline", baseline_tokens); |
| 203 } |
| 204 return baseline_time; |
| 205 } |
| 206 |
| 207 |
| 208 int main(int argc, char* argv[]) { |
| 209 v8::V8::InitializeICU(); |
| 210 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 211 Encoding encoding = LATIN1; |
| 212 bool print_tokens = false; |
| 213 std::vector<std::string> fnames; |
| 214 std::string benchmark; |
| 215 for (int i = 0; i < argc; ++i) { |
| 216 if (strcmp(argv[i], "--latin1") == 0) { |
| 217 encoding = LATIN1; |
| 218 } else if (strcmp(argv[i], "--utf8") == 0) { |
| 219 encoding = UTF8; |
| 220 } else if (strcmp(argv[i], "--utf16") == 0) { |
| 221 encoding = UTF16; |
| 222 } else if (strcmp(argv[i], "--print-tokens") == 0) { |
| 223 print_tokens = true; |
| 224 } else if (strncmp(argv[i], "--benchmark=", 12) == 0) { |
| 225 benchmark = std::string(argv[i]).substr(12); |
| 226 } else if (i > 0 && argv[i][0] != '-') { |
| 227 fnames.push_back(std::string(argv[i])); |
| 228 } |
| 229 } |
| 230 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 231 { |
| 232 v8::HandleScope handle_scope(isolate); |
| 233 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 234 v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global); |
| 235 ASSERT(!context.IsEmpty()); |
| 236 { |
| 237 v8::Context::Scope scope(context); |
| 238 Isolate* isolate = Isolate::Current(); |
| 239 double baseline_total = 0; |
| 240 for (size_t i = 0; i < fnames.size(); i++) { |
| 241 TimeDelta time; |
| 242 time = ProcessFile(fnames[i].c_str(), encoding, isolate, print_tokens); |
| 243 baseline_total += time.InMillisecondsF(); |
| 244 } |
| 245 if (benchmark.empty()) benchmark = "Baseline"; |
| 246 printf("%s(RunTime): %.f ms\n", benchmark.c_str(), baseline_total); |
| 247 } |
| 248 } |
| 249 v8::V8::Dispose(); |
| 250 return 0; |
| 251 } |
OLD | NEW |