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

Unified Diff: runtime/vm/unicode.cc

Issue 2952193002: VM: Speed up output of UTF8 for 1-byte strings.
Patch Set: Add test that would have caught bug pointed out by Slava Created 3 years, 3 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 | « runtime/vm/symbols.cc ('k') | runtime/vm/unicode_test.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/unicode.cc
diff --git a/runtime/vm/unicode.cc b/runtime/vm/unicode.cc
index e9cd101026072f0282b8bd79cbb7ac668c8746dd..6d38f9e190d597c8c2523519f4d3ddcc33a6457d 100644
--- a/runtime/vm/unicode.cc
+++ b/runtime/vm/unicode.cc
@@ -107,7 +107,53 @@ intptr_t Utf8::Length(int32_t ch) {
return 4;
}
+// A constant mask that can be 'and'ed with a word of data to determine if it
+// is all ASCII (with no Latin1 characters).
+#if defined(ARCH_IS_64_BIT)
+static const uintptr_t kAsciiWordMask = DART_UINT64_C(0x8080808080808080);
+#else
+static const uintptr_t kAsciiWordMask = 0x80808080u;
+#endif
+
intptr_t Utf8::Length(const String& str) {
+ if (str.IsOneByteString() || str.IsExternalOneByteString()) {
+ // For 1-byte strings, all code points < 0x80 have single-byte UTF-8
+ // encodings and all >= 0x80 have two-byte encodings. To get the length,
+ // start with the number of code points and add the number of high bits in
+ // the bytes.
+ uintptr_t char_length = str.Length();
+ uintptr_t length = char_length;
+ const uintptr_t* data;
+ NoSafepointScope no_safepoint;
+ if (str.IsOneByteString()) {
+ data = reinterpret_cast<const uintptr_t*>(OneByteString::DataStart(str));
+ } else {
+ data = reinterpret_cast<const uintptr_t*>(
+ ExternalOneByteString::DataStart(str));
+ }
+ uintptr_t i;
+ for (i = 0; i + sizeof(uintptr_t) < char_length; i += sizeof(uintptr_t)) {
+ uintptr_t chunk = *data++;
+ chunk &= kAsciiWordMask;
+ if (chunk != 0) {
+// Shuffle the bits until we have a count of bits in the low nibble.
+#if defined(ARCH_IS_64_BIT)
+ chunk += chunk >> 32;
+#endif
+ chunk += chunk >> 16;
+ chunk += chunk >> 8;
+ length += (chunk >> 7) & 0xf;
+ }
+ }
+ // Take care of the tail of the string, the last length % wordsize chars.
+ for (; i < char_length; i++) {
+ if (str.CharAt(i) > kMaxOneByteChar) length++;
+ }
+ return length;
+ }
+
+ // Slow case for 2-byte strings that handles surrogate pairs and longer UTF-8
+ // encodings.
intptr_t length = 0;
String::CodePointIterator it(str);
while (it.Next()) {
@@ -143,16 +189,64 @@ intptr_t Utf8::Encode(int32_t ch, char* dst) {
}
intptr_t Utf8::Encode(const String& src, char* dst, intptr_t len) {
+ uintptr_t array_len = len;
intptr_t pos = 0;
- String::CodePointIterator it(src);
- while (it.Next()) {
- int32_t ch = it.Current();
- intptr_t num_bytes = Utf8::Length(ch);
- if (pos + num_bytes > len) {
- break;
+ ASSERT(static_cast<intptr_t>(array_len) >= Length(src));
+ if (src.IsOneByteString() || src.IsExternalOneByteString()) {
+ // For 1-byte strings, all code points < 0x80 have single-byte UTF-8
+ // encodings and all >= 0x80 have two-byte encodings.
+ const uintptr_t* data;
+ NoSafepointScope scope;
+ if (src.IsOneByteString()) {
+ data = reinterpret_cast<const uintptr_t*>(OneByteString::DataStart(src));
+ } else {
+ data = reinterpret_cast<const uintptr_t*>(
+ ExternalOneByteString::DataStart(src));
+ }
+ uintptr_t char_length = src.Length();
+ uintptr_t pos = 0;
+ ASSERT(kMaxOneByteChar + 1 == 0x80);
+ for (uintptr_t i = 0; i < char_length; i += sizeof(uintptr_t)) {
+ // Read the input one word at a time and just write it verbatim if it is
+ // plain ASCII, as determined by the mask.
+ if (i + sizeof(uintptr_t) <= char_length &&
+ (*data & kAsciiWordMask) == 0 &&
+ pos + sizeof(uintptr_t) <= array_len) {
+ StoreUnaligned(reinterpret_cast<uintptr_t*>(dst + pos), *data);
+ pos += sizeof(uintptr_t);
+ } else {
+ // Process up to one word of input that contains non-ASCII Latin1
+ // characters.
+ const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
+ const uint8_t* limit =
+ Utils::Minimum(p + sizeof(uintptr_t), p + (char_length - i));
+ for (; p < limit; p++) {
+ uint8_t c = *p;
+ // These calls to Length and Encode get inlined and the cases for 3
+ // and 4 byte sequences are removed.
+ intptr_t bytes = Length(c);
+ if (pos + bytes > array_len) {
+ return pos;
+ }
+ Encode(c, reinterpret_cast<char*>(dst) + pos);
+ pos += bytes;
+ }
+ }
+ data++;
+ }
+ } else {
+ // For two-byte strings, which can contain 3 and 4-byte UTF-8 encodings,
+ // which can result in surrogate pairs, use the more general code.
+ String::CodePointIterator it(src);
+ while (it.Next()) {
+ int32_t ch = it.Current();
+ intptr_t num_bytes = Utf8::Length(ch);
+ if (pos + num_bytes > len) {
+ break;
+ }
+ Utf8::Encode(ch, &dst[pos]);
+ pos += num_bytes;
}
- Utf8::Encode(ch, &dst[pos]);
- pos += num_bytes;
}
return pos;
}
« no previous file with comments | « runtime/vm/symbols.cc ('k') | runtime/vm/unicode_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698