AK+LibRegex+LibWasm: Remove the non-const COWVector::operator[]

This was copying the vector behind our backs, let's remove it and make
the copying explicit by putting it behind COWVector::mutable_at().
This is a further 64% performance improvement on Wasm validation.
This commit is contained in:
Ali Mohammad Pur 2024-03-11 16:55:29 +01:00 committed by Ali Mohammad Pur
parent cced555879
commit 8003bde03d
4 changed files with 14 additions and 22 deletions

View file

@ -110,7 +110,7 @@ public:
m_detail->m_members.clear();
}
T& at(size_t index)
T& mutable_at(size_t index)
{
// We're handing out a mutable reference, so make sure we own the data exclusively.
copy();
@ -122,13 +122,6 @@ public:
return m_detail->m_members.at(index);
}
T& operator[](size_t index)
{
// We're handing out a mutable reference, so make sure we own the data exclusively.
copy();
return m_detail->m_members[index];
}
T const& operator[](size_t index) const
{
return m_detail->m_members[index];

View file

@ -332,7 +332,7 @@ ALWAYS_INLINE ExecutionResult OpCode_CheckEnd::execute(MatchInput const& input,
ALWAYS_INLINE ExecutionResult OpCode_ClearCaptureGroup::execute(MatchInput const& input, MatchState& state) const
{
if (input.match_index < state.capture_group_matches.size()) {
auto& group = state.capture_group_matches[input.match_index];
auto& group = state.capture_group_matches.mutable_at(input.match_index);
auto group_id = id();
if (group_id >= group.size())
group.resize(group_id + 1);
@ -352,19 +352,19 @@ ALWAYS_INLINE ExecutionResult OpCode_SaveLeftCaptureGroup::execute(MatchInput co
}
if (id() >= state.capture_group_matches.at(input.match_index).size()) {
state.capture_group_matches.at(input.match_index).ensure_capacity(id());
state.capture_group_matches.mutable_at(input.match_index).ensure_capacity(id());
auto capacity = state.capture_group_matches.at(input.match_index).capacity();
for (size_t i = state.capture_group_matches.at(input.match_index).size(); i <= capacity; ++i)
state.capture_group_matches.at(input.match_index).empend();
state.capture_group_matches.mutable_at(input.match_index).empend();
}
state.capture_group_matches.at(input.match_index).at(id()).left_column = state.string_position;
state.capture_group_matches.mutable_at(input.match_index).at(id()).left_column = state.string_position;
return ExecutionResult::Continue;
}
ALWAYS_INLINE ExecutionResult OpCode_SaveRightCaptureGroup::execute(MatchInput const& input, MatchState& state) const
{
auto& match = state.capture_group_matches.at(input.match_index).at(id());
auto& match = state.capture_group_matches.mutable_at(input.match_index).at(id());
auto start_position = match.left_column;
if (state.string_position < start_position) {
dbgln("Right capture group {} is before left capture group {}!", state.string_position, start_position);
@ -391,7 +391,7 @@ ALWAYS_INLINE ExecutionResult OpCode_SaveRightCaptureGroup::execute(MatchInput c
ALWAYS_INLINE ExecutionResult OpCode_SaveRightNamedCaptureGroup::execute(MatchInput const& input, MatchState& state) const
{
auto& match = state.capture_group_matches.at(input.match_index).at(id());
auto& match = state.capture_group_matches.mutable_at(input.match_index).at(id());
auto start_position = match.left_column;
if (state.string_position < start_position)
return ExecutionResult::Failed_ExecuteLowPrioForks;
@ -1051,7 +1051,7 @@ ALWAYS_INLINE ExecutionResult OpCode_Repeat::execute(MatchInput const&, MatchSta
if (id() >= state.repetition_marks.size())
state.repetition_marks.resize(id() + 1);
auto& repetition_mark = state.repetition_marks.at(id());
auto& repetition_mark = state.repetition_marks.mutable_at(id());
if (repetition_mark == count() - 1) {
repetition_mark = 0;
@ -1068,7 +1068,7 @@ ALWAYS_INLINE ExecutionResult OpCode_ResetRepeat::execute(MatchInput const&, Mat
if (id() >= state.repetition_marks.size())
state.repetition_marks.resize(id() + 1);
state.repetition_marks.at(id()) = 0;
state.repetition_marks.mutable_at(id()) = 0;
return ExecutionResult::Continue;
}

View file

@ -157,9 +157,9 @@ RegexResult Matcher<Parser>::match(Vector<RegexStringView> const& views, Optiona
for (size_t j = 0; j < c_match_preallocation_count; ++j) {
state.matches.empend();
state.capture_group_matches.empend();
state.capture_group_matches.at(j).ensure_capacity(capture_groups_count);
state.capture_group_matches.mutable_at(j).ensure_capacity(capture_groups_count);
for (size_t k = 0; k < capture_groups_count; ++k)
state.capture_group_matches.at(j).unchecked_append({});
state.capture_group_matches.mutable_at(j).unchecked_append({});
}
}
@ -169,9 +169,9 @@ RegexResult Matcher<Parser>::match(Vector<RegexStringView> const& views, Optiona
VERIFY(start_position + state.string_position - start_position <= input.view.length());
if (input.regex_options.has_flag_set(AllFlags::StringCopyMatches)) {
state.matches.at(input.match_index) = { input.view.substring_view(start_position, state.string_position - start_position).to_byte_string(), input.line, start_position, input.global_offset + start_position };
state.matches.mutable_at(input.match_index) = { input.view.substring_view(start_position, state.string_position - start_position).to_byte_string(), input.line, start_position, input.global_offset + start_position };
} else { // let the view point to the original string ...
state.matches.at(input.match_index) = { input.view.substring_view(start_position, state.string_position - start_position), input.line, start_position, input.global_offset + start_position };
state.matches.mutable_at(input.match_index) = { input.view.substring_view(start_position, state.string_position - start_position), input.line, start_position, input.global_offset + start_position };
}
};

View file

@ -38,8 +38,7 @@ ErrorOr<void, ValidationError> Validator::validate(Module& module)
m_context = {};
module.for_each_section_of_type<TypeSection>([this](TypeSection const& section) {
for (auto& type : section.types())
m_context.types.append(type);
m_context.types.extend(section.types());
});
module.for_each_section_of_type<ImportSection>([&](ImportSection const& section) {