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

Unified Diff: benchmarks/write-utf8-benchmark.cc

Issue 9689065: Benchmarkify pointer swapping string encoder
Patch Set: Created 8 years, 9 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 | « no previous file | benchmarks/write-utf8-benchmark.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: benchmarks/write-utf8-benchmark.cc
diff --git a/benchmarks/write-utf8-benchmark.cc b/benchmarks/write-utf8-benchmark.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c8fefcdb95c904520a24672fab67e4833f64f80d
--- /dev/null
+++ b/benchmarks/write-utf8-benchmark.cc
@@ -0,0 +1,250 @@
+// Copyright 2012 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.
+
+// This links against libv8. If you don't have that handy, but you have Python and Scons,
+// the following commands will link against a directory with the V8 source.
+
+// #!/bin/sh
+// rm -f obj/release/*
+// /path/to/v8/tools/test.py --mode=release --arch=x64 -j18 --build-only
+// rm obj/release/d8*.o
+// rm obj/release/preparser-api.o
+// g++ -I /home/erikcorry/OpenV82/v8/include -o writeutf8 write-utf8-benchmark.cc obj/release/*.o obj/release/x64/*.o obj/release/extensions/externalize-string-extension.o obj/release/extensions/gc-extension.o -lpthread
+
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "v8.h"
+
+#ifdef _WIN32
+#include <Windows.h>
+#else
+#include <sys/time.h>
+#endif
+
+using namespace v8;
+
+const char* ascii[] = {
+ "a", // 1
+ "aa", // 2
+ "aaaa", // 4
+ "aaaaaaaa", // 8
+ "aaaaaaaaaaaaaaaa", // 16
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", // 32
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", // 64
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", // 128
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" // 256
+};
+
+const char* utf[] = {
+ "\304\233", // 1
+ "\304\233\304\233", // 2
+ "\304\233\304\233\304\233\304\233", // 4
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233", // 8
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233", // 16
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233", // 32
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233", // 64
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233", // 128
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233"
+ "\304\233\304\233\304\233\304\233\304\233\304\233\304\233\304\233" // 256
+};
+
+
+#ifdef _WIN32
+typedef LARGE_INTEGER timestamp;
+static void GetTime(timestamp* time) {
+ QueryPerformanceCounter(time);
+}
+static void PrintTime(timestamp* start, const char* name, bool just_enough) {
+ timestamp end;
+ static LARGE_INTEGER freq = {0};
+ QueryPerformanceCounter(&end);
+ if (freq.QuadPart == 0) {
+ QueryPerformanceFrequency(&freq);
+ }
+ printf("%s (%s): %ldms\n", name, just_enough ? "just enough" : "plenty", (end.QuadPart - start->QuadPart) / (freq.QuadPart / 1000));
+}
+
+#else //UNIX
+typedef struct timeval timestamp;
+static void GetTime(timestamp* time) {
+ gettimeofday(time, NULL);
+}
+static void PrintTime(timestamp* start, const char* name, bool just_enough) {
+ timestamp end;
+ gettimeofday(&end, NULL);
+ printf("%s (%s): %ldms\n", name, just_enough ? "just enough" : "plenty", (end.tv_sec - start->tv_sec) * 1000 + (end.tv_usec - start->tv_usec) / 1000);
+}
+#endif
+
+
+static char output[1000000];
+
+static void Test(Local<String>* one, Local<String>* two, Local<String> prepend, const char* name, bool just_enough) {
+ timestamp start;
+ GetTime(&start);
+ for (int k = 0; k < 1000; k++) {
+ HandleScope handle_scope;
+ for (int i = 0; i < 8; i++) {
+ for (int j = 0; j < 8; j++) {
+ Local<String> str = String::Concat(one[i], two[j]);
+ str = String::Concat(str, one[j]);
+ str = String::Concat(prepend, str);
+ int len;
+ str->WriteUtf8(output, just_enough ? str->Utf8Length() + 1 : -1, &len);
+ }
+ }
+ }
+ PrintTime(&start, name, just_enough);
+}
+
+
+static void Unbalanced(Local<String> one, Local<String> two, bool left, Local<String> prepend, const char* name, bool just_enough, int flag = 0) {
+ timestamp start;
+ GetTime(&start);
+ for (int k = 0; k < 100; k++) {
+ HandleScope handle_scope;
+ Local<String> str = String::Concat(one, two);
+ for (int i = 0; i < 2000; i++) {
+ if (left) {
+ str = String::Concat(one, str);
+ } else {
+ str = String::Concat(str, two);
+ }
+ }
+ int len;
+ str = String::Concat(prepend, str);
+ str->WriteUtf8(output, just_enough ? str->Utf8Length() + 1 : -1, &len, flag);
+ }
+ PrintTime(&start, name, just_enough);
+}
+
+
+int main(int argc, char* argv[]) {
+ V8::SetFlagsFromCommandLine(&argc, argv, false);
+
+ HandleScope handle_scope;
+
+ Local<String> ascii_strings[8];
+ Local<String> unicode_strings[8];
+ for (int i = 0; i < 8; i++) {
+ ascii_strings[i] = v8::String::New(ascii[i]);
+ unicode_strings[i] = v8::String::New(utf[i]);
+ }
+
+
+ Persistent<Context> context = Context::New();
+ Context::Scope context_scope(context);
+
+
+ for (int i = 0; i < 2; i++) {
+ Test(&ascii_strings[0], &ascii_strings[0], ascii_strings[0], "Short Ascii", i != 0);
+ Test(&unicode_strings[0], &unicode_strings[0], ascii_strings[0], "Short Unicode", i != 0);
+ Test(&ascii_strings[0], &ascii_strings[0], unicode_strings[0], "Prepend Unicode", i != 0);
+
+ Unbalanced(ascii_strings[0], ascii_strings[0], true, ascii_strings[0], "Unbalanced left", i != 0);
+ Unbalanced(ascii_strings[0], ascii_strings[0], false, ascii_strings[0], "Unbalanced right", i != 0);
+ Unbalanced(unicode_strings[0], unicode_strings[0], true, unicode_strings[0], "Unbalanced Unicode left", i != 0);
+ Unbalanced(unicode_strings[0], unicode_strings[0], false, unicode_strings[0], "Unbalanced Unicode right", i != 0);
+
+ Unbalanced(ascii_strings[7], ascii_strings[7], true, ascii_strings[7], "Unbalanced long left", i != 0);
+ Unbalanced(ascii_strings[7], ascii_strings[7], false, ascii_strings[7], "Unbalanced long right", i != 0);
+ Unbalanced(unicode_strings[7], unicode_strings[7], true, unicode_strings[7], "Unbalanced Unicode long left", i != 0);
+ Unbalanced(unicode_strings[7], unicode_strings[7], false, unicode_strings[7], "Unbalanced Unicode long right", i != 0);
+
+ Unbalanced(ascii_strings[7], ascii_strings[7], true, unicode_strings[7], "Prepend Unbalanced long left", i != 0);
+ Unbalanced(ascii_strings[7], ascii_strings[7], false, unicode_strings[7], "Prepend Unbalanced long right", i != 0);
+
+ Unbalanced(unicode_strings[7], unicode_strings[7], true, unicode_strings[7], "Unbalanced Unicode long left hint-many", i != 0, 1);
+ Unbalanced(unicode_strings[7], unicode_strings[7], false, unicode_strings[7], "Unbalanced Unicode long right hint-many", i != 0, 1);
+ }
+
+ context.Dispose();
+ return 0;
+}
« no previous file with comments | « no previous file | benchmarks/write-utf8-benchmark.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698