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) {
}
void RegExpBytecodeGenerator::Emit16(uint32_t word) {
DCHECK(pc_ <= buffer_.length());
if (pc_ + 1 >= buffer_.length()) {
Expand();
DCHECK(pc_ <= static_cast<int>(buffer_.size()));
if (pc_ + 1 >= static_cast<int>(buffer_.size())) {
ExpandBuffer();
}
*reinterpret_cast<uint16_t*>(buffer_.begin() + pc_) = word;
*reinterpret_cast<uint16_t*>(buffer_.data() + pc_) = word;
pc_ += 2;
}
void RegExpBytecodeGenerator::Emit8(uint32_t word) {
DCHECK(pc_ <= buffer_.length());
if (pc_ == buffer_.length()) {
Expand();
DCHECK(pc_ <= static_cast<int>(buffer_.size()));
if (pc_ == static_cast<int>(buffer_.size())) {
ExpandBuffer();
}
*reinterpret_cast<unsigned char*>(buffer_.begin() + pc_) = word;
*reinterpret_cast<unsigned char*>(buffer_.data() + pc_) = word;
pc_ += 1;
}
void RegExpBytecodeGenerator::Emit32(uint32_t word) {
DCHECK(pc_ <= buffer_.length());
if (pc_ + 3 >= buffer_.length()) {
Expand();
DCHECK(pc_ <= static_cast<int>(buffer_.size()));
if (pc_ + 3 >= static_cast<int>(buffer_.size())) {
ExpandBuffer();
}
*reinterpret_cast<uint32_t*>(buffer_.begin() + pc_) = word;
*reinterpret_cast<uint32_t*>(buffer_.data() + pc_) = word;
pc_ += 4;
}
......
......@@ -16,7 +16,7 @@ namespace internal {
RegExpBytecodeGenerator::RegExpBytecodeGenerator(Isolate* isolate, Zone* zone)
: RegExpMacroAssembler(isolate, zone),
buffer_(base::Vector<byte>::New(1024)),
buffer_(kInitialBufferSize, zone),
pc_(0),
advance_current_end_(kInvalidPC),
jump_edges_(zone),
......@@ -24,7 +24,6 @@ RegExpBytecodeGenerator::RegExpBytecodeGenerator(Isolate* isolate, Zone* zone)
RegExpBytecodeGenerator::~RegExpBytecodeGenerator() {
if (backtrack_.is_linked()) backtrack_.Unuse();
buffer_.Dispose();
}
RegExpBytecodeGenerator::IrregexpImplementation
......@@ -39,8 +38,8 @@ void RegExpBytecodeGenerator::Bind(Label* l) {
int pos = l->pos();
while (pos != 0) {
int fixup = pos;
pos = *reinterpret_cast<int32_t*>(buffer_.begin() + fixup);
*reinterpret_cast<uint32_t*>(buffer_.begin() + fixup) = pc_;
pos = *reinterpret_cast<int32_t*>(buffer_.data() + fixup);
*reinterpret_cast<uint32_t*>(buffer_.data() + fixup) = pc_;
jump_edges_.emplace(fixup, pc_);
}
}
......@@ -383,7 +382,7 @@ Handle<HeapObject> RegExpBytecodeGenerator::GetCode(Handle<String> source) {
Handle<ByteArray> array;
if (FLAG_regexp_peephole_optimization) {
array = RegExpBytecodePeepholeOptimization::OptimizeBytecode(
isolate_, zone(), source, buffer_.begin(), length(), jump_edges_);
isolate_, zone(), source, buffer_.data(), length(), jump_edges_);
} else {
array = isolate_->factory()->NewByteArray(length());
Copy(array->GetDataStartAddress());
......@@ -395,14 +394,13 @@ Handle<HeapObject> RegExpBytecodeGenerator::GetCode(Handle<String> source) {
int RegExpBytecodeGenerator::length() { return pc_; }
void RegExpBytecodeGenerator::Copy(byte* a) {
MemCopy(a, buffer_.begin(), length());
MemCopy(a, buffer_.data(), length());
}
void RegExpBytecodeGenerator::Expand() {
base::Vector<byte> old_buffer = buffer_;
buffer_ = base::Vector<byte>::New(old_buffer.length() * 2);
MemCopy(buffer_.begin(), old_buffer.begin(), old_buffer.length());
old_buffer.Dispose();
void RegExpBytecodeGenerator::ExpandBuffer() {
// TODO(jgruber): The growth strategy could be smarter for large sizes.
// TODO(jgruber): It's not necessary to default-initialize new elements.
buffer_.resize(buffer_.size() * 2);
}
} // namespace internal
......
......@@ -83,7 +83,8 @@ class V8_EXPORT_PRIVATE RegExpBytecodeGenerator : public RegExpMacroAssembler {
Handle<HeapObject> GetCode(Handle<String> source) override;
private:
void Expand();
void ExpandBuffer();
// Code and bitmap emission.
inline void EmitOrLink(Label* label);
inline void Emit32(uint32_t x);
......@@ -96,7 +97,9 @@ class V8_EXPORT_PRIVATE RegExpBytecodeGenerator : public RegExpMacroAssembler {
void Copy(byte* a);
// 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.
int pc_;
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