Commit c30e65e1 authored by Yang Guo's avatar Yang Guo Committed by Commit Bot

[api] simplify String::WriteToUtf8.

Since we always flatten the string upfront, we don't need to
implement the the UTF8 conversion as a string visitor anymore.

R=petermarshall@chromium.org

Bug: v8:6780, v8:8605
Change-Id: I27946551d7c3742f47ac36d5c909c19a7f2b0371
Reviewed-on: https://chromium-review.googlesource.com/c/1371828
Commit-Queue: Yang Guo <yangguo@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58312}
parent e66c6ef7
This diff is collapsed.
......@@ -8452,6 +8452,13 @@ THREADED_TEST(StringWrite) {
int len;
int charlen;
memset(utf8buf, 0x1, 1000);
len = v8::String::Empty(isolate)->WriteUtf8(isolate, utf8buf, sizeof(utf8buf),
&charlen);
CHECK_EQ(1, len);
CHECK_EQ(0, charlen);
CHECK_EQ(0, strcmp(utf8buf, ""));
memset(utf8buf, 0x1, 1000);
len = str2->WriteUtf8(isolate, utf8buf, sizeof(utf8buf), &charlen);
CHECK_EQ(9, len);
......
......@@ -35,6 +35,7 @@
#include "src/v8.h"
#include "src/api-inl.h"
#include "src/base/platform/elapsed-timer.h"
#include "src/heap/factory.h"
#include "src/messages.h"
#include "src/objects-inl.h"
......@@ -957,6 +958,109 @@ TEST(Utf8Conversion) {
}
}
TEST(Utf8ConversionPerf) {
// Smoke test for converting strings to utf-8.
LocalContext context;
v8::HandleScope handle_scope(CcTest::isolate());
v8::Local<v8::String> ascii_string =
CompileRun("'abc'.repeat(1E6)").As<v8::String>();
v8::Local<v8::String> one_byte_string =
CompileRun("'\\u0255\\u0254\\u0253'.repeat(1E6)").As<v8::String>();
v8::Local<v8::String> two_byte_string =
CompileRun("'\\u2255\\u2254\\u2253'.repeat(1E6)").As<v8::String>();
v8::Local<v8::String> surrogate_string =
CompileRun("'\\u{12345}\\u2244'.repeat(1E6)").As<v8::String>();
int size = 1E7;
char* buffer = new char[4 * size];
{
v8::base::ElapsedTimer timer;
timer.Start();
ascii_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
printf("ascii string %0.3f\n", timer.Elapsed().InMillisecondsF());
timer.Stop();
}
{
v8::base::ElapsedTimer timer;
timer.Start();
ascii_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
printf("ascii string %0.3f\n", timer.Elapsed().InMillisecondsF());
timer.Stop();
}
{
v8::base::ElapsedTimer timer;
timer.Start();
ascii_string->WriteUtf8(CcTest::isolate(), buffer, 4 * size, nullptr);
printf("ascii string %0.3f\n", timer.Elapsed().InMillisecondsF());
timer.Stop();
}
{
v8::base::ElapsedTimer timer;
timer.Start();
one_byte_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
printf("one byte string %0.3f\n", timer.Elapsed().InMillisecondsF());
timer.Stop();
}
{
v8::base::ElapsedTimer timer;
timer.Start();
one_byte_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
printf("one byte string %0.3f\n", timer.Elapsed().InMillisecondsF());
timer.Stop();
}
{
v8::base::ElapsedTimer timer;
timer.Start();
one_byte_string->WriteUtf8(CcTest::isolate(), buffer, 4 * size, nullptr);
printf("one byte string %0.3f\n", timer.Elapsed().InMillisecondsF());
timer.Stop();
}
{
v8::base::ElapsedTimer timer;
timer.Start();
two_byte_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
printf("two byte string %0.3f\n", timer.Elapsed().InMillisecondsF());
timer.Stop();
}
{
v8::base::ElapsedTimer timer;
timer.Start();
two_byte_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
printf("two byte string %0.3f\n", timer.Elapsed().InMillisecondsF());
timer.Stop();
}
{
v8::base::ElapsedTimer timer;
timer.Start();
two_byte_string->WriteUtf8(CcTest::isolate(), buffer, 4 * size, nullptr);
printf("two byte string %0.3f\n", timer.Elapsed().InMillisecondsF());
timer.Stop();
}
{
v8::base::ElapsedTimer timer;
timer.Start();
surrogate_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
printf("surrogate string %0.3f\n", timer.Elapsed().InMillisecondsF());
timer.Stop();
}
{
v8::base::ElapsedTimer timer;
timer.Start();
surrogate_string->WriteUtf8(CcTest::isolate(), buffer, size, nullptr);
printf("surrogate string %0.3f\n", timer.Elapsed().InMillisecondsF());
timer.Stop();
}
{
v8::base::ElapsedTimer timer;
timer.Start();
surrogate_string->WriteUtf8(CcTest::isolate(), buffer, 4 * size, nullptr);
printf("surrogate string %0.3f\n", timer.Elapsed().InMillisecondsF());
timer.Stop();
}
delete[] buffer;
}
TEST(ExternalShortStringAdd) {
LocalContext context;
......@@ -1064,6 +1168,27 @@ TEST(ExternalShortStringAdd) {
CHECK_EQ(0, CompileRun(source)->Int32Value(context.local()).FromJust());
}
TEST(ReplaceInvalidUtf8) {
LocalContext context;
v8::HandleScope handle_scope(CcTest::isolate());
v8::Local<v8::String> string = CompileRun("'ab\\ud800cd'").As<v8::String>();
char buffer[7];
memset(buffer, 0, 7);
int chars_written = 0;
int size = string->WriteUtf8(CcTest::isolate(), buffer, 7, &chars_written,
v8::String::REPLACE_INVALID_UTF8);
CHECK_EQ(7, size);
CHECK_EQ(5, chars_written);
CHECK_EQ(0, memcmp("\x61\x62\xef\xbf\xbd\x63\x64", buffer, 7));
memset(buffer, 0, 7);
chars_written = 0;
size = string->WriteUtf8(CcTest::isolate(), buffer, 6, &chars_written,
v8::String::REPLACE_INVALID_UTF8);
CHECK_EQ(6, size);
CHECK_EQ(4, chars_written);
CHECK_EQ(0, memcmp("\x61\x62\xef\xbf\xbd\x63", buffer, 6));
}
TEST(JSONStringifySliceMadeExternal) {
if (!FLAG_string_slices) return;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment