Commit 69e1a42e authored by Jakob Gruber's avatar Jakob Gruber Committed by V8 LUCI CQ

[regexp] Use ZoneVector in RegExpBytecodeGenerator

.. to avoid the expensive malloc call.

Fixed: v8:9455
Change-Id: I6734fe07a3884b228d818f60be83d9e45c2ee383
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3190105
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Commit-Queue: Patrick Thier <pthier@chromium.org>
Auto-Submit: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: 's avatarPatrick Thier <pthier@chromium.org>
Cr-Commit-Position: refs/heads/main@{#77118}
parent e34ac477
...@@ -23,29 +23,29 @@ void RegExpBytecodeGenerator::Emit(uint32_t byte, int32_t twenty_four_bits) { ...@@ -23,29 +23,29 @@ void RegExpBytecodeGenerator::Emit(uint32_t byte, int32_t twenty_four_bits) {
} }
void RegExpBytecodeGenerator::Emit16(uint32_t word) { void RegExpBytecodeGenerator::Emit16(uint32_t word) {
DCHECK(pc_ <= buffer_.length()); DCHECK(pc_ <= static_cast<int>(buffer_.size()));
if (pc_ + 1 >= buffer_.length()) { if (pc_ + 1 >= static_cast<int>(buffer_.size())) {
Expand(); ExpandBuffer();
} }
*reinterpret_cast<uint16_t*>(buffer_.begin() + pc_) = word; *reinterpret_cast<uint16_t*>(buffer_.data() + pc_) = word;
pc_ += 2; pc_ += 2;
} }
void RegExpBytecodeGenerator::Emit8(uint32_t word) { void RegExpBytecodeGenerator::Emit8(uint32_t word) {
DCHECK(pc_ <= buffer_.length()); DCHECK(pc_ <= static_cast<int>(buffer_.size()));
if (pc_ == buffer_.length()) { if (pc_ == static_cast<int>(buffer_.size())) {
Expand(); ExpandBuffer();
} }
*reinterpret_cast<unsigned char*>(buffer_.begin() + pc_) = word; *reinterpret_cast<unsigned char*>(buffer_.data() + pc_) = word;
pc_ += 1; pc_ += 1;
} }
void RegExpBytecodeGenerator::Emit32(uint32_t word) { void RegExpBytecodeGenerator::Emit32(uint32_t word) {
DCHECK(pc_ <= buffer_.length()); DCHECK(pc_ <= static_cast<int>(buffer_.size()));
if (pc_ + 3 >= buffer_.length()) { if (pc_ + 3 >= static_cast<int>(buffer_.size())) {
Expand(); ExpandBuffer();
} }
*reinterpret_cast<uint32_t*>(buffer_.begin() + pc_) = word; *reinterpret_cast<uint32_t*>(buffer_.data() + pc_) = word;
pc_ += 4; pc_ += 4;
} }
......
...@@ -16,7 +16,7 @@ namespace internal { ...@@ -16,7 +16,7 @@ namespace internal {
RegExpBytecodeGenerator::RegExpBytecodeGenerator(Isolate* isolate, Zone* zone) RegExpBytecodeGenerator::RegExpBytecodeGenerator(Isolate* isolate, Zone* zone)
: RegExpMacroAssembler(isolate, zone), : RegExpMacroAssembler(isolate, zone),
buffer_(base::Vector<byte>::New(1024)), buffer_(kInitialBufferSize, zone),
pc_(0), pc_(0),
advance_current_end_(kInvalidPC), advance_current_end_(kInvalidPC),
jump_edges_(zone), jump_edges_(zone),
...@@ -24,7 +24,6 @@ RegExpBytecodeGenerator::RegExpBytecodeGenerator(Isolate* isolate, Zone* zone) ...@@ -24,7 +24,6 @@ RegExpBytecodeGenerator::RegExpBytecodeGenerator(Isolate* isolate, Zone* zone)
RegExpBytecodeGenerator::~RegExpBytecodeGenerator() { RegExpBytecodeGenerator::~RegExpBytecodeGenerator() {
if (backtrack_.is_linked()) backtrack_.Unuse(); if (backtrack_.is_linked()) backtrack_.Unuse();
buffer_.Dispose();
} }
RegExpBytecodeGenerator::IrregexpImplementation RegExpBytecodeGenerator::IrregexpImplementation
...@@ -39,8 +38,8 @@ void RegExpBytecodeGenerator::Bind(Label* l) { ...@@ -39,8 +38,8 @@ void RegExpBytecodeGenerator::Bind(Label* l) {
int pos = l->pos(); int pos = l->pos();
while (pos != 0) { while (pos != 0) {
int fixup = pos; int fixup = pos;
pos = *reinterpret_cast<int32_t*>(buffer_.begin() + fixup); pos = *reinterpret_cast<int32_t*>(buffer_.data() + fixup);
*reinterpret_cast<uint32_t*>(buffer_.begin() + fixup) = pc_; *reinterpret_cast<uint32_t*>(buffer_.data() + fixup) = pc_;
jump_edges_.emplace(fixup, pc_); jump_edges_.emplace(fixup, pc_);
} }
} }
...@@ -383,7 +382,7 @@ Handle<HeapObject> RegExpBytecodeGenerator::GetCode(Handle<String> source) { ...@@ -383,7 +382,7 @@ Handle<HeapObject> RegExpBytecodeGenerator::GetCode(Handle<String> source) {
Handle<ByteArray> array; Handle<ByteArray> array;
if (FLAG_regexp_peephole_optimization) { if (FLAG_regexp_peephole_optimization) {
array = RegExpBytecodePeepholeOptimization::OptimizeBytecode( array = RegExpBytecodePeepholeOptimization::OptimizeBytecode(
isolate_, zone(), source, buffer_.begin(), length(), jump_edges_); isolate_, zone(), source, buffer_.data(), length(), jump_edges_);
} else { } else {
array = isolate_->factory()->NewByteArray(length()); array = isolate_->factory()->NewByteArray(length());
Copy(array->GetDataStartAddress()); Copy(array->GetDataStartAddress());
...@@ -395,14 +394,13 @@ Handle<HeapObject> RegExpBytecodeGenerator::GetCode(Handle<String> source) { ...@@ -395,14 +394,13 @@ Handle<HeapObject> RegExpBytecodeGenerator::GetCode(Handle<String> source) {
int RegExpBytecodeGenerator::length() { return pc_; } int RegExpBytecodeGenerator::length() { return pc_; }
void RegExpBytecodeGenerator::Copy(byte* a) { void RegExpBytecodeGenerator::Copy(byte* a) {
MemCopy(a, buffer_.begin(), length()); MemCopy(a, buffer_.data(), length());
} }
void RegExpBytecodeGenerator::Expand() { void RegExpBytecodeGenerator::ExpandBuffer() {
base::Vector<byte> old_buffer = buffer_; // TODO(jgruber): The growth strategy could be smarter for large sizes.
buffer_ = base::Vector<byte>::New(old_buffer.length() * 2); // TODO(jgruber): It's not necessary to default-initialize new elements.
MemCopy(buffer_.begin(), old_buffer.begin(), old_buffer.length()); buffer_.resize(buffer_.size() * 2);
old_buffer.Dispose();
} }
} // namespace internal } // namespace internal
......
...@@ -83,7 +83,8 @@ class V8_EXPORT_PRIVATE RegExpBytecodeGenerator : public RegExpMacroAssembler { ...@@ -83,7 +83,8 @@ class V8_EXPORT_PRIVATE RegExpBytecodeGenerator : public RegExpMacroAssembler {
Handle<HeapObject> GetCode(Handle<String> source) override; Handle<HeapObject> GetCode(Handle<String> source) override;
private: private:
void Expand(); void ExpandBuffer();
// Code and bitmap emission. // Code and bitmap emission.
inline void EmitOrLink(Label* label); inline void EmitOrLink(Label* label);
inline void Emit32(uint32_t x); inline void Emit32(uint32_t x);
...@@ -96,7 +97,9 @@ class V8_EXPORT_PRIVATE RegExpBytecodeGenerator : public RegExpMacroAssembler { ...@@ -96,7 +97,9 @@ class V8_EXPORT_PRIVATE RegExpBytecodeGenerator : public RegExpMacroAssembler {
void Copy(byte* a); void Copy(byte* a);
// The buffer into which code and relocation info are generated. // The buffer into which code and relocation info are generated.
base::Vector<byte> buffer_; static constexpr int kInitialBufferSize = 1024;
ZoneVector<byte> buffer_;
// The program counter. // The program counter.
int pc_; int pc_;
Label backtrack_; Label backtrack_;
......
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