Clean up is-ASCII checks.

Review URL: http://codereview.chromium.org/5963003

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@6109 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
vitalyr@chromium.org 2010-12-22 11:31:18 +00:00
parent 4330070e32
commit 52d2ce3b5a
6 changed files with 42 additions and 38 deletions

View File

@ -43,16 +43,14 @@ int Heap::MaxObjectSizeInPagedSpace() {
MaybeObject* Heap::AllocateStringFromUtf8(Vector<const char> str, MaybeObject* Heap::AllocateStringFromUtf8(Vector<const char> str,
PretenureFlag pretenure) { PretenureFlag pretenure) {
// Check for ASCII first since this is the common case. // Check for ASCII first since this is the common case.
for (int i = 0; i < str.length(); ++i) { if (String::IsAscii(str.start(), str.length())) {
if (static_cast<uint8_t>(str[i]) > String::kMaxAsciiCharCodeU) {
// Non-ASCII and we need to decode.
return AllocateStringFromUtf8Slow(str, pretenure);
}
}
// If the string is ASCII, we do not need to convert the characters // If the string is ASCII, we do not need to convert the characters
// since UTF8 is backwards compatible with ASCII. // since UTF8 is backwards compatible with ASCII.
return AllocateStringFromAscii(str, pretenure); return AllocateStringFromAscii(str, pretenure);
} }
// Non-ASCII and we need to decode.
return AllocateStringFromUtf8Slow(str, pretenure);
}
MaybeObject* Heap::AllocateSymbol(Vector<const char> str, MaybeObject* Heap::AllocateSymbol(Vector<const char> str,

View File

@ -2549,20 +2549,10 @@ MaybeObject* Heap::AllocateExternalStringFromTwoByte(
} }
// For small strings we check whether the resource contains only // For small strings we check whether the resource contains only
// ascii characters. If yes, we use a different string map. // ASCII characters. If yes, we use a different string map.
bool is_ascii = true; static const size_t kAsciiCheckLengthLimit = 32;
if (length >= static_cast<size_t>(String::kMinNonFlatLength)) { bool is_ascii = length <= kAsciiCheckLengthLimit &&
is_ascii = false; String::IsAscii(resource->data(), length);
} else {
const uc16* data = resource->data();
for (size_t i = 0; i < length; i++) {
if (data[i] > String::kMaxAsciiCharCode) {
is_ascii = false;
break;
}
}
}
Map* map = is_ascii ? Map* map = is_ascii ?
Heap::external_string_with_ascii_data_map() : Heap::external_string_map(); Heap::external_string_with_ascii_data_map() : Heap::external_string_map();
Object* result; Object* result;
@ -3342,11 +3332,8 @@ MaybeObject* Heap::AllocateStringFromUtf8Slow(Vector<const char> string,
MaybeObject* Heap::AllocateStringFromTwoByte(Vector<const uc16> string, MaybeObject* Heap::AllocateStringFromTwoByte(Vector<const uc16> string,
PretenureFlag pretenure) { PretenureFlag pretenure) {
// Check if the string is an ASCII string. // Check if the string is an ASCII string.
int i = 0;
while (i < string.length() && string[i] <= String::kMaxAsciiCharCode) i++;
MaybeObject* maybe_result; MaybeObject* maybe_result;
if (i == string.length()) { // It's an ASCII string. if (String::IsAscii(string.start(), string.length())) {
maybe_result = AllocateRawAsciiString(string.length(), pretenure); maybe_result = AllocateRawAsciiString(string.length(), pretenure);
} else { // It's not an ASCII string. } else { // It's not an ASCII string.
maybe_result = AllocateRawTwoByteString(string.length(), pretenure); maybe_result = AllocateRawTwoByteString(string.length(), pretenure);

View File

@ -211,9 +211,7 @@ void RegExpMacroAssemblerIA32::CheckCharacters(Vector<const uc16> str,
// If input is ASCII, don't even bother calling here if the string to // If input is ASCII, don't even bother calling here if the string to
// match contains a non-ascii character. // match contains a non-ascii character.
if (mode_ == ASCII) { if (mode_ == ASCII) {
for (int i = 0; i < str.length(); i++) { ASSERT(String::IsAscii(str.start(), str.length()));
ASSERT(str[i] <= String::kMaxAsciiCharCodeU);
}
} }
#endif #endif
int byte_length = str.length() * char_size(); int byte_length = str.length() * char_size();

View File

@ -5245,6 +5245,34 @@ class String: public HeapObject {
int from, int from,
int to); int to);
static inline bool IsAscii(const char* chars, int length) {
const char* limit = chars + length;
#ifdef V8_HOST_CAN_READ_UNALIGNED
ASSERT(kMaxAsciiCharCode == 0x7F);
const uintptr_t non_ascii_mask = kUintptrAllBitsSet / 0xFF * 0x80;
while (chars <= limit - sizeof(uintptr_t)) {
if (*reinterpret_cast<const uintptr_t*>(chars) & non_ascii_mask) {
return false;
}
chars += sizeof(uintptr_t);
}
#endif
while (chars < limit) {
if (static_cast<uint8_t>(*chars) > kMaxAsciiCharCodeU) return false;
++chars;
}
return true;
}
static inline bool IsAscii(const uc16* chars, int length) {
const uc16* limit = chars + length;
while (chars < limit) {
if (*chars > kMaxAsciiCharCodeU) return false;
++chars;
}
return true;
}
protected: protected:
class ReadBlockBuffer { class ReadBlockBuffer {
public: public:

View File

@ -66,12 +66,7 @@ class StringSearchBase {
} }
static inline bool IsAsciiString(Vector<const uc16> string) { static inline bool IsAsciiString(Vector<const uc16> string) {
for (int i = 0, n = string.length(); i < n; i++) { return String::IsAscii(string.start(), string.length());
if (static_cast<unsigned>(string[i]) > String::kMaxAsciiCharCodeU) {
return false;
}
}
return true;
} }
// The following tables are shared by all searches. // The following tables are shared by all searches.

View File

@ -223,9 +223,7 @@ void RegExpMacroAssemblerX64::CheckCharacters(Vector<const uc16> str,
// If input is ASCII, don't even bother calling here if the string to // If input is ASCII, don't even bother calling here if the string to
// match contains a non-ascii character. // match contains a non-ascii character.
if (mode_ == ASCII) { if (mode_ == ASCII) {
for (int i = 0; i < str.length(); i++) { ASSERT(String::IsAscii(str.start(), str.length()));
ASSERT(str[i] <= String::kMaxAsciiCharCodeU);
}
} }
#endif #endif
int byte_length = str.length() * char_size(); int byte_length = str.length() * char_size();