Extract String class from objects.cc

I extracted following class member functions to string.cc
* String
* SeqString
* SeqOneByteString
* SeqTwoByteString
* ConsString
* ThinString
* SlicedString
* ExternalString
* FlatStringReader
* ConsStringIterator

Declaration of all above class are in string.h.

This patch makes compile time of objects.cc from 20.6s to 19.2s on Z840 Linux.

Bug: v8:7629
Change-Id: If74b868b3a3d9a1df2887f82e2557da43ad221f0
Reviewed-on: https://chromium-review.googlesource.com/c/1446342
Reviewed-by: Marja Hölttä <marja@chromium.org>
Reviewed-by: Ben Titzer <titzer@chromium.org>
Commit-Queue: Takuto Ikuta <tikuta@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59235}
This commit is contained in:
Takuto Ikuta 2019-01-31 18:46:40 +09:00 committed by Commit Bot
parent 77a643e393
commit 26321c072c
5 changed files with 1722 additions and 1670 deletions

View File

@ -842,7 +842,10 @@ action("postmortem-metadata") {
"src/objects/script-inl.h",
"src/objects/shared-function-info.h",
"src/objects/shared-function-info-inl.h",
"src/objects/string.cc",
"src/objects/string.h",
"src/objects/string-comparator.cc",
"src/objects/string-comparator.h",
"src/objects/string-inl.h",
"src/objects/struct.h",
"src/objects/struct-inl.h",
@ -2309,9 +2312,12 @@ v8_source_set("v8_base") {
"src/objects/slots.h",
"src/objects/stack-frame-info-inl.h",
"src/objects/stack-frame-info.h",
"src/objects/string-comparator.cc",
"src/objects/string-comparator.h",
"src/objects/string-inl.h",
"src/objects/string-table-inl.h",
"src/objects/string-table.h",
"src/objects/string.cc",
"src/objects/string.h",
"src/objects/struct-inl.h",
"src/objects/struct.h",

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,74 @@
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/objects/string-comparator.h"
#include "src/objects/string-inl.h"
namespace v8 {
namespace internal {
void StringComparator::State::Init(String string) {
ConsString cons_string = String::VisitFlat(this, string);
iter_.Reset(cons_string);
if (!cons_string.is_null()) {
int offset;
string = iter_.Next(&offset);
String::VisitFlat(this, string, offset);
}
}
void StringComparator::State::Advance(int consumed) {
DCHECK(consumed <= length_);
// Still in buffer.
if (length_ != consumed) {
if (is_one_byte_) {
buffer8_ += consumed;
} else {
buffer16_ += consumed;
}
length_ -= consumed;
return;
}
// Advance state.
int offset;
String next = iter_.Next(&offset);
DCHECK_EQ(0, offset);
DCHECK(!next.is_null());
String::VisitFlat(this, next);
}
bool StringComparator::Equals(String string_1, String string_2) {
int length = string_1->length();
state_1_.Init(string_1);
state_2_.Init(string_2);
while (true) {
int to_check = Min(state_1_.length_, state_2_.length_);
DCHECK(to_check > 0 && to_check <= length);
bool is_equal;
if (state_1_.is_one_byte_) {
if (state_2_.is_one_byte_) {
is_equal = Equals<uint8_t, uint8_t>(&state_1_, &state_2_, to_check);
} else {
is_equal = Equals<uint8_t, uint16_t>(&state_1_, &state_2_, to_check);
}
} else {
if (state_2_.is_one_byte_) {
is_equal = Equals<uint16_t, uint8_t>(&state_1_, &state_2_, to_check);
} else {
is_equal = Equals<uint16_t, uint16_t>(&state_1_, &state_2_, to_check);
}
}
// Looping done.
if (!is_equal) return false;
length -= to_check;
// Exit condition. Strings are equal.
if (length == 0) return true;
state_1_.Advance(to_check);
state_2_.Advance(to_check);
}
}
} // namespace internal
} // namespace v8

View File

@ -0,0 +1,109 @@
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_OBJECTS_STRING_COMPARATOR_H_
#define V8_OBJECTS_STRING_COMPARATOR_H_
#include "src/base/logging.h"
#include "src/globals.h"
#include "src/objects/string.h"
#include "src/utils.h"
namespace v8 {
namespace internal {
// Compares the contents of two strings by reading and comparing
// int-sized blocks of characters.
template <typename Char>
static inline bool CompareRawStringContents(const Char* const a,
const Char* const b, int length) {
return CompareChars(a, b, length) == 0;
}
template <typename Chars1, typename Chars2>
class RawStringComparator : public AllStatic {
public:
static inline bool compare(const Chars1* a, const Chars2* b, int len) {
DCHECK(sizeof(Chars1) != sizeof(Chars2));
for (int i = 0; i < len; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
};
template <>
class RawStringComparator<uint16_t, uint16_t> {
public:
static inline bool compare(const uint16_t* a, const uint16_t* b, int len) {
return CompareRawStringContents(a, b, len);
}
};
template <>
class RawStringComparator<uint8_t, uint8_t> {
public:
static inline bool compare(const uint8_t* a, const uint8_t* b, int len) {
return CompareRawStringContents(a, b, len);
}
};
class StringComparator {
class State {
public:
State() : is_one_byte_(true), length_(0), buffer8_(nullptr) {}
void Init(String string);
inline void VisitOneByteString(const uint8_t* chars, int length) {
is_one_byte_ = true;
buffer8_ = chars;
length_ = length;
}
inline void VisitTwoByteString(const uint16_t* chars, int length) {
is_one_byte_ = false;
buffer16_ = chars;
length_ = length;
}
void Advance(int consumed);
ConsStringIterator iter_;
bool is_one_byte_;
int length_;
union {
const uint8_t* buffer8_;
const uint16_t* buffer16_;
};
private:
DISALLOW_COPY_AND_ASSIGN(State);
};
public:
inline StringComparator() = default;
template <typename Chars1, typename Chars2>
static inline bool Equals(State* state_1, State* state_2, int to_check) {
const Chars1* a = reinterpret_cast<const Chars1*>(state_1->buffer8_);
const Chars2* b = reinterpret_cast<const Chars2*>(state_2->buffer8_);
return RawStringComparator<Chars1, Chars2>::compare(a, b, to_check);
}
bool Equals(String string_1, String string_2);
private:
State state_1_;
State state_2_;
DISALLOW_COPY_AND_ASSIGN(StringComparator);
};
} // namespace internal
} // namespace v8
#endif // V8_OBJECTS_STRING_COMPARATOR_H_

1532
src/objects/string.cc Normal file

File diff suppressed because it is too large Load Diff