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

Side by Side Diff: src/api.cc

Issue 12783002: Some Utf8Length microoptimizations (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | src/unicode.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 3993 matching lines...) Expand 10 before | Expand all | Expand 10 after
4004 static inline bool EndsWithSurrogate(uint8_t state) { 4004 static inline bool EndsWithSurrogate(uint8_t state) {
4005 return state & kEndsWithLeadingSurrogate; 4005 return state & kEndsWithLeadingSurrogate;
4006 } 4006 }
4007 4007
4008 static inline bool StartsWithSurrogate(uint8_t state) { 4008 static inline bool StartsWithSurrogate(uint8_t state) {
4009 return state & kStartsWithTrailingSurrogate; 4009 return state & kStartsWithTrailingSurrogate;
4010 } 4010 }
4011 4011
4012 class Visitor { 4012 class Visitor {
4013 public: 4013 public:
4014 explicit Visitor() 4014 inline explicit Visitor()
4015 : utf8_length_(0), 4015 : utf8_length_(0),
4016 state_(kInitialState) {} 4016 state_(kInitialState) {}
4017 4017
4018 template<typename Char> 4018 void VisitOneByteString(const uint8_t* chars, int length) {
4019 inline void Visit(const Char* chars, int length) { 4019 int utf8_length = 0;
4020 // Add in length 1 for each non-ASCII character.
4021 for (int i = 0; i < length; i++) {
4022 utf8_length += *chars++ >> 7;
4023 }
4024 // Add in length 1 for each character.
4025 utf8_length_ = utf8_length + length;
4026 state_ = kInitialState;
4027 }
4028
4029 void VisitTwoByteString(const uint16_t* chars, int length) {
4020 int utf8_length = 0; 4030 int utf8_length = 0;
4021 int last_character = unibrow::Utf16::kNoPreviousCharacter; 4031 int last_character = unibrow::Utf16::kNoPreviousCharacter;
4022 for (int i = 0; i < length; i++) { 4032 for (int i = 0; i < length; i++) {
4023 uint16_t c = chars[i]; 4033 uint16_t c = chars[i];
4024 utf8_length += unibrow::Utf8::Length(c, last_character); 4034 utf8_length += unibrow::Utf8::Length(c, last_character);
4025 if (sizeof(Char) > 1) { 4035 last_character = c;
4026 last_character = c;
4027 }
4028 } 4036 }
4029 utf8_length_ = utf8_length; 4037 utf8_length_ = utf8_length;
4030 }
4031
4032 void VisitOneByteString(const uint8_t* chars, int length) {
4033 Visit(chars, length);
4034 state_ = kInitialState;
4035 }
4036
4037 void VisitTwoByteString(const uint16_t* chars, int length) {
4038 Visit(chars, length);
4039 uint8_t state = 0; 4038 uint8_t state = 0;
4040 if (unibrow::Utf16::IsTrailSurrogate(chars[0])) { 4039 if (unibrow::Utf16::IsTrailSurrogate(chars[0])) {
4041 state |= kStartsWithTrailingSurrogate; 4040 state |= kStartsWithTrailingSurrogate;
4042 } 4041 }
4043 if (unibrow::Utf16::IsLeadSurrogate(chars[length-1])) { 4042 if (unibrow::Utf16::IsLeadSurrogate(chars[length-1])) {
4044 state |= kEndsWithLeadingSurrogate; 4043 state |= kEndsWithLeadingSurrogate;
4045 } 4044 }
4046 state_ = state; 4045 state_ = state;
4047 } 4046 }
4048 4047
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
4125 Visitor::VisitFlat(left, &leaf_length, &left_leaf_state); 4124 Visitor::VisitFlat(left, &leaf_length, &left_leaf_state);
4126 if (left_as_cons == NULL) { 4125 if (left_as_cons == NULL) {
4127 total_length += leaf_length; 4126 total_length += leaf_length;
4128 MergeLeafLeft(&total_length, &state, left_leaf_state); 4127 MergeLeafLeft(&total_length, &state, left_leaf_state);
4129 } 4128 }
4130 ConsString* right_as_cons = 4129 ConsString* right_as_cons =
4131 Visitor::VisitFlat(right, &leaf_length, &right_leaf_state); 4130 Visitor::VisitFlat(right, &leaf_length, &right_leaf_state);
4132 if (right_as_cons == NULL) { 4131 if (right_as_cons == NULL) {
4133 total_length += leaf_length; 4132 total_length += leaf_length;
4134 MergeLeafRight(&total_length, &state, right_leaf_state); 4133 MergeLeafRight(&total_length, &state, right_leaf_state);
4135 // Terminal node. 4134 if (left_as_cons != NULL) {
4136 if (left_as_cons == NULL) { 4135 // 1 Leaf node. Descend in place.
4136 current = left_as_cons;
4137 continue;
4138 } else {
4139 // Terminal node.
4137 MergeTerminal(&total_length, state, state_out); 4140 MergeTerminal(&total_length, state, state_out);
4138 return total_length; 4141 return total_length;
4139 } 4142 }
4140 } else if (left_as_cons != NULL) { 4143 } else if (left_as_cons == NULL) {
4141 // Both strings are ConsStrings. 4144 // 1 Leaf node. Descend in place.
4142 // Recurse on smallest. 4145 current = right_as_cons;
4143 if (left->length() < right->length()) { 4146 continue;
4144 total_length += Calculate(left_as_cons, &left_leaf_state);
4145 MergeLeafLeft(&total_length, &state, left_leaf_state);
4146 current = right_as_cons;
4147 continue;
4148 } else {
4149 total_length += Calculate(right_as_cons, &right_leaf_state);
4150 MergeLeafRight(&total_length, &state, right_leaf_state);
4151 current = left_as_cons;
4152 continue;
4153 }
4154 } 4147 }
4155 // 1 leaf node. Do in place descent. 4148 // Both strings are ConsStrings.
4156 if (left_as_cons != NULL) { 4149 // Recurse on smallest.
4150 if (left->length() < right->length()) {
4151 total_length += Calculate(left_as_cons, &left_leaf_state);
4152 MergeLeafLeft(&total_length, &state, left_leaf_state);
4153 current = right_as_cons;
4154 } else {
4155 total_length += Calculate(right_as_cons, &right_leaf_state);
4156 MergeLeafRight(&total_length, &state, right_leaf_state);
4157 current = left_as_cons; 4157 current = left_as_cons;
4158 } else {
4159 ASSERT(right_as_cons != NULL);
4160 current = right_as_cons;
4161 } 4158 }
4162 } 4159 }
4163 UNREACHABLE(); 4160 UNREACHABLE();
4164 return 0; 4161 return 0;
4165 } 4162 }
4166 4163
4167 static inline int Calculate(i::ConsString* current) { 4164 static inline int Calculate(i::ConsString* current) {
4168 uint8_t state = kInitialState; 4165 uint8_t state = kInitialState;
4169 return Calculate(current, &state); 4166 return Calculate(current, &state);
4170 } 4167 }
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
4260 (remaining_capacity - max_size_per_char)/max_size_per_char; 4257 (remaining_capacity - max_size_per_char)/max_size_per_char;
4261 // Need to drop into slow loop. 4258 // Need to drop into slow loop.
4262 if (writable_length <= 0) break; 4259 if (writable_length <= 0) break;
4263 fast_length = i + writable_length; 4260 fast_length = i + writable_length;
4264 if (fast_length > length) fast_length = length; 4261 if (fast_length > length) fast_length = length;
4265 } 4262 }
4266 // Write the characters to the stream. 4263 // Write the characters to the stream.
4267 if (sizeof(Char) == 1) { 4264 if (sizeof(Char) == 1) {
4268 for (; i < fast_length; i++) { 4265 for (; i < fast_length; i++) {
4269 buffer += 4266 buffer +=
4270 Utf8::Encode(buffer, *chars++, Utf16::kNoPreviousCharacter); 4267 Utf8::EncodeOneByte(buffer, static_cast<uint8_t>(*chars++));
4271 ASSERT(capacity_ == -1 || (buffer - start_) <= capacity_); 4268 ASSERT(capacity_ == -1 || (buffer - start_) <= capacity_);
4272 } 4269 }
4273 } else { 4270 } else {
4274 for (; i < fast_length; i++) { 4271 for (; i < fast_length; i++) {
4275 uint16_t character = *chars++; 4272 uint16_t character = *chars++;
4276 buffer += Utf8::Encode(buffer, character, last_character); 4273 buffer += Utf8::Encode(buffer, character, last_character);
4277 last_character = character; 4274 last_character = character;
4278 ASSERT(capacity_ == -1 || (buffer - start_) <= capacity_); 4275 ASSERT(capacity_ == -1 || (buffer - start_) <= capacity_);
4279 } 4276 }
4280 } 4277 }
(...skipping 2783 matching lines...) Expand 10 before | Expand all | Expand 10 after
7064 7061
7065 v->VisitPointers(blocks_.first(), first_block_limit_); 7062 v->VisitPointers(blocks_.first(), first_block_limit_);
7066 7063
7067 for (int i = 1; i < blocks_.length(); i++) { 7064 for (int i = 1; i < blocks_.length(); i++) {
7068 v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]); 7065 v->VisitPointers(blocks_[i], &blocks_[i][kHandleBlockSize]);
7069 } 7066 }
7070 } 7067 }
7071 7068
7072 7069
7073 } } // namespace v8::internal 7070 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | src/unicode.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698