Commit 04ccb975 authored by yangguo@chromium.org's avatar yangguo@chromium.org

Remove InputBuffer

R=yangguo@chromium.org
BUG=

Review URL: https://chromiumcodereview.appspot.com/11727004
Patch from Dan Carney <dcarney@google.com>.

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13298 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent a50259f7
This diff is collapsed.
......@@ -7313,18 +7313,6 @@ class String: public HeapObject {
const uc16* GetTwoByteData();
const uc16* GetTwoByteData(unsigned start);
// Support for StringInputBuffer
static const unibrow::byte* ReadBlock(String* input,
unibrow::byte* util_buffer,
unsigned capacity,
unsigned* remaining,
unsigned* offset);
static const unibrow::byte* ReadBlock(String** input,
unibrow::byte* util_buffer,
unsigned capacity,
unsigned* remaining,
unsigned* offset);
// Helper function for flattening strings.
template <typename sinkchar>
static void WriteToFlat(String* source,
......@@ -7383,33 +7371,6 @@ class String: public HeapObject {
int32_t type,
unsigned length);
protected:
class ReadBlockBuffer {
public:
ReadBlockBuffer(unibrow::byte* util_buffer_,
unsigned cursor_,
unsigned capacity_,
unsigned remaining_) :
util_buffer(util_buffer_),
cursor(cursor_),
capacity(capacity_),
remaining(remaining_) {
}
unibrow::byte* util_buffer;
unsigned cursor;
unsigned capacity;
unsigned remaining;
};
static inline const unibrow::byte* ReadBlock(String* input,
ReadBlockBuffer* buffer,
unsigned* offset,
unsigned max_chars);
static void ReadBlockIntoBuffer(String* input,
ReadBlockBuffer* buffer,
unsigned* offset_ptr,
unsigned max_chars);
private:
// Try to flatten the top level ConsString that is hiding behind this
// string. This is a no-op unless the string is a ConsString. Flatten
......@@ -7485,14 +7446,6 @@ class SeqOneByteString: public SeqString {
// Q.v. String::kMaxLength which is the maximal size of concatenated strings.
static const int kMaxLength = (kMaxSize - kHeaderSize);
// Support for StringInputBuffer.
inline void SeqOneByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
unsigned* offset,
unsigned chars);
inline const unibrow::byte* SeqOneByteStringReadBlock(unsigned* remaining,
unsigned* offset,
unsigned chars);
DECLARE_VERIFIER(SeqOneByteString)
private:
......@@ -7537,11 +7490,6 @@ class SeqTwoByteString: public SeqString {
// Q.v. String::kMaxLength which is the maximal size of concatenated strings.
static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
// Support for StringInputBuffer.
inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
unsigned* offset_ptr,
unsigned chars);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
};
......@@ -7584,14 +7532,6 @@ class ConsString: public String {
static const int kSecondOffset = kFirstOffset + kPointerSize;
static const int kSize = kSecondOffset + kPointerSize;
// Support for StringInputBuffer.
inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
unsigned* offset_ptr,
unsigned chars);
inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
unsigned* offset_ptr,
unsigned chars);
// Minimum length for a cons string.
static const int kMinLength = 13;
......@@ -7636,13 +7576,6 @@ class SlicedString: public String {
static const int kOffsetOffset = kParentOffset + kPointerSize;
static const int kSize = kOffsetOffset + kPointerSize;
// Support for StringInputBuffer
inline const unibrow::byte* SlicedStringReadBlock(ReadBlockBuffer* buffer,
unsigned* offset_ptr,
unsigned chars);
inline void SlicedStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
unsigned* offset_ptr,
unsigned chars);
// Minimum length for a sliced string.
static const int kMinLength = 13;
......@@ -7719,14 +7652,6 @@ class ExternalAsciiString: public ExternalString {
template<typename StaticVisitor>
inline void ExternalAsciiStringIterateBody();
// Support for StringInputBuffer.
const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
unsigned* offset,
unsigned chars);
inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
unsigned* offset,
unsigned chars);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
};
......@@ -7767,12 +7692,6 @@ class ExternalTwoByteString: public ExternalString {
template<typename StaticVisitor>
inline void ExternalTwoByteStringIterateBody();
// Support for StringInputBuffer.
void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
unsigned* offset_ptr,
unsigned chars);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
};
......@@ -7819,24 +7738,6 @@ class FlatStringReader : public Relocatable {
};
// Note that StringInputBuffers are not valid across a GC! To fix this
// it would have to store a String Handle instead of a String* and
// AsciiStringReadBlock would have to be modified to use memcpy.
//
// StringInputBuffer is able to traverse any string regardless of how
// deeply nested a sequence of ConsStrings it is made of. However,
// performance will be better if deep strings are flattened before they
// are traversed. Since flattening requires memory allocation this is
// not always desirable, however (esp. in debugging situations).
class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
public:
virtual void Seek(unsigned pos);
inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
explicit inline StringInputBuffer(String* backing):
unibrow::InputBuffer<String, String*, 1024>(backing) {}
};
// A ConsStringOp that returns null.
// Useful when the operation to apply on a ConsString
// requires an expensive data structure.
......
......@@ -430,10 +430,6 @@ class Scanner {
// be empty).
bool ScanRegExpFlags();
// Tells whether the buffer contains an identifier (no escapes).
// Used for checking if a property name is an identifier.
static bool IsIdentifier(unibrow::CharacterStream* buffer);
private:
// The current and look-ahead token.
struct TokenDesc {
......
......@@ -137,109 +137,6 @@ unsigned Utf8::Length(uchar c, int previous) {
}
}
uchar CharacterStream::GetNext() {
uchar result = DecodeCharacter(buffer_, &cursor_);
if (remaining_ == 1) {
cursor_ = 0;
FillBuffer();
} else {
remaining_--;
}
ASSERT(BoundsCheck(cursor_));
return result;
}
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define IF_LITTLE(expr) expr
#define IF_BIG(expr) ((void) 0)
#elif __BYTE_ORDER == __BIG_ENDIAN
#define IF_LITTLE(expr) ((void) 0)
#define IF_BIG(expr) expr
#else
#warning Unknown byte ordering
#endif
bool CharacterStream::EncodeAsciiCharacter(uchar c, byte* buffer,
unsigned capacity, unsigned& offset) {
if (offset >= capacity) return false;
buffer[offset] = c;
offset += 1;
return true;
}
bool CharacterStream::EncodeNonAsciiCharacter(uchar c, byte* buffer,
unsigned capacity, unsigned& offset) {
unsigned aligned = (offset + 0x3) & ~0x3;
if ((aligned + sizeof(uchar)) > capacity)
return false;
if (offset == aligned) {
IF_LITTLE(*reinterpret_cast<uchar*>(buffer + aligned) = (c << 8) | 0x80);
IF_BIG(*reinterpret_cast<uchar*>(buffer + aligned) = c | (1 << 31));
} else {
buffer[offset] = 0x80;
IF_LITTLE(*reinterpret_cast<uchar*>(buffer + aligned) = c << 8);
IF_BIG(*reinterpret_cast<uchar*>(buffer + aligned) = c);
}
offset = aligned + sizeof(uchar);
return true;
}
bool CharacterStream::EncodeCharacter(uchar c, byte* buffer, unsigned capacity,
unsigned& offset) {
if (c <= Utf8::kMaxOneByteChar) {
return EncodeAsciiCharacter(c, buffer, capacity, offset);
} else {
return EncodeNonAsciiCharacter(c, buffer, capacity, offset);
}
}
uchar CharacterStream::DecodeCharacter(const byte* buffer, unsigned* offset) {
byte b = buffer[*offset];
if (b <= Utf8::kMaxOneByteChar) {
(*offset)++;
return b;
} else {
unsigned aligned = (*offset + 0x3) & ~0x3;
*offset = aligned + sizeof(uchar);
IF_LITTLE(return *reinterpret_cast<const uchar*>(buffer + aligned) >> 8);
IF_BIG(return *reinterpret_cast<const uchar*>(buffer + aligned) &
~(1 << 31));
}
}
#undef IF_LITTLE
#undef IF_BIG
template <class R, class I, unsigned s>
void InputBuffer<R, I, s>::FillBuffer() {
buffer_ = R::ReadBlock(input_, util_buffer_, s, &remaining_, &offset_);
}
template <class R, class I, unsigned s>
void InputBuffer<R, I, s>::Rewind() {
Reset(input_);
}
template <class R, class I, unsigned s>
void InputBuffer<R, I, s>::Reset(unsigned position, I input) {
input_ = input;
remaining_ = 0;
cursor_ = 0;
offset_ = position;
buffer_ = R::ReadBlock(input_, util_buffer_, s, &remaining_, &offset_);
}
template <class R, class I, unsigned s>
void InputBuffer<R, I, s>::Reset(I input) {
Reset(0, input);
}
template <class R, class I, unsigned s>
void InputBuffer<R, I, s>::Seek(unsigned position) {
offset_ = position;
buffer_ = R::ReadBlock(input_, util_buffer_, s, &remaining_, &offset_);
}
Utf8DecoderBase::Utf8DecoderBase()
: unbuffered_start_(NULL),
utf16_length_(0),
......
......@@ -277,33 +277,6 @@ uchar Utf8::CalculateValue(const byte* str,
}
unsigned CharacterStream::Length() {
unsigned result = 0;
while (has_more()) {
result++;
GetNext();
}
Rewind();
return result;
}
unsigned CharacterStream::Utf16Length() {
unsigned result = 0;
while (has_more()) {
uchar c = GetNext();
result += c > Utf16::kMaxNonSurrogateCharCode ? 2 : 1;
}
Rewind();
return result;
}
void CharacterStream::Seek(unsigned position) {
Rewind();
for (unsigned i = 0; i < position; i++) {
GetNext();
}
}
void Utf8DecoderBase::Reset(uint16_t* buffer,
unsigned buffer_length,
const uint8_t* stream,
......
......@@ -100,21 +100,6 @@ class UnicodeData {
static const uchar kMaxCodePoint;
};
// --- U t f 8 a n d 16 ---
template <typename Data>
class Buffer {
public:
inline Buffer(Data data, unsigned length) : data_(data), length_(length) { }
inline Buffer() : data_(0), length_(0) { }
Data data() { return data_; }
unsigned length() { return length_; }
private:
Data data_;
unsigned length_;
};
class Utf16 {
public:
static inline bool IsLeadSurrogate(int code) {
......@@ -173,72 +158,6 @@ class Utf8 {
unsigned* cursor);
};
// --- C h a r a c t e r S t r e a m ---
class CharacterStream {
public:
inline uchar GetNext();
inline bool has_more() { return remaining_ != 0; }
// Note that default implementation is not efficient.
virtual void Seek(unsigned);
unsigned Length();
unsigned Utf16Length();
virtual ~CharacterStream() { }
static inline bool EncodeCharacter(uchar c, byte* buffer, unsigned capacity,
unsigned& offset);
static inline bool EncodeAsciiCharacter(uchar c, byte* buffer,
unsigned capacity, unsigned& offset);
static inline bool EncodeNonAsciiCharacter(uchar c, byte* buffer,
unsigned capacity, unsigned& offset);
static inline uchar DecodeCharacter(const byte* buffer, unsigned* offset);
virtual void Rewind() = 0;
protected:
virtual void FillBuffer() = 0;
virtual bool BoundsCheck(unsigned offset) = 0;
// The number of characters left in the current buffer
unsigned remaining_;
// The current offset within the buffer
unsigned cursor_;
// The buffer containing the decoded characters.
const byte* buffer_;
};
// --- I n p u t B u f f e r ---
/**
* Provides efficient access to encoded characters in strings. It
* does so by reading characters one block at a time, rather than one
* character at a time, which gives string implementations an
* opportunity to optimize the decoding.
*/
template <class Reader, class Input = Reader*, unsigned kSize = 256>
class InputBuffer : public CharacterStream {
public:
virtual void Rewind();
inline void Reset(Input input);
void Seek(unsigned position);
inline void Reset(unsigned position, Input input);
protected:
InputBuffer() { }
explicit InputBuffer(Input input) { Reset(input); }
virtual void FillBuffer();
virtual bool BoundsCheck(unsigned offset) {
return (buffer_ != util_buffer_) || (offset < kSize);
}
// A custom offset that can be used by the string implementation to
// mark progress within the encoded string.
unsigned offset_;
// The input string
Input input_;
// To avoid heap allocation, we keep an internal buffer to which
// the encoded string can write its characters. The string
// implementation is free to decide whether it wants to use this
// buffer or not.
byte util_buffer_[kSize];
};
class Utf8DecoderBase {
public:
......
// Copyright 2012 the V8 project authors. All rights reserved.
// Check that we can traverse very deep stacks of ConsStrings using
// StringInputBuffer. Check that Get(int) works on very deep stacks
// StringCharacterStram. Check that Get(int) works on very deep stacks
// of ConsStrings. These operations may not be very fast, but they
// should be possible without getting errors due to too deep recursion.
......@@ -514,23 +514,16 @@ static Handle<String> ConstructBalanced(
}
static StringInputBuffer buffer;
static ConsStringIteratorOp cons_string_iterator_op_1;
static ConsStringIteratorOp cons_string_iterator_op_2;
static void Traverse(Handle<String> s1, Handle<String> s2) {
int i = 0;
buffer.Reset(*s1);
StringCharacterStream character_stream_1(*s1, &cons_string_iterator_op_1);
StringCharacterStream character_stream_2(*s2, &cons_string_iterator_op_2);
StringInputBuffer buffer2(*s2);
while (buffer.has_more()) {
CHECK(buffer2.has_more());
CHECK(character_stream_1.HasMore());
while (character_stream_1.HasMore()) {
CHECK(character_stream_2.HasMore());
uint16_t c = buffer.GetNext();
CHECK_EQ(c, buffer2.GetNext());
CHECK_EQ(c, character_stream_1.GetNext());
uint16_t c = character_stream_1.GetNext();
CHECK_EQ(c, character_stream_2.GetNext());
i++;
}
......@@ -543,17 +536,11 @@ static void Traverse(Handle<String> s1, Handle<String> s2) {
static void TraverseFirst(Handle<String> s1, Handle<String> s2, int chars) {
int i = 0;
buffer.Reset(*s1);
StringInputBuffer buffer2(*s2);
StringCharacterStream character_stream_1(*s1, &cons_string_iterator_op_1);
StringCharacterStream character_stream_2(*s2, &cons_string_iterator_op_2);
while (buffer.has_more() && i < chars) {
CHECK(buffer2.has_more());
CHECK(character_stream_1.HasMore());
while (character_stream_1.HasMore() && i < chars) {
CHECK(character_stream_2.HasMore());
uint16_t c = buffer.GetNext();
CHECK_EQ(c, buffer2.GetNext());
CHECK_EQ(c, character_stream_1.GetNext());
uint16_t c = character_stream_1.GetNext();
CHECK_EQ(c, character_stream_2.GetNext());
i++;
}
......
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