Commit 7805c33c authored by Jakob Gruber's avatar Jakob Gruber Committed by V8 LUCI CQ

Compact DependentCode on growth

Refactor s.t. we now compact the DependentCode weak fixed array both
when adding new entries and when marked for deopts.

Bug: v8:12397
Change-Id: I842f3369644e88cc0b98a1f1371259c920cc8bbf
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3291320
Commit-Queue: Jakob Gruber <jgruber@chromium.org>
Commit-Queue: Dominik Inführ <dinfuehr@chromium.org>
Reviewed-by: 's avatarDominik Inführ <dinfuehr@chromium.org>
Auto-Submit: Jakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/main@{#77973}
parent 9889f08e
......@@ -800,6 +800,11 @@ void DependentCode::InstallDependency(Isolate* isolate, Handle<Code> code,
Handle<DependentCode> DependentCode::InsertWeakCode(
Isolate* isolate, Handle<DependentCode> entries, DependencyGroups groups,
Handle<Code> code) {
if (entries->length() == entries->capacity()) {
// We'd have to grow - try to compact first.
entries->IterateAndCompact([](CodeT, DependencyGroups) { return false; });
}
MaybeObjectHandle code_slot(HeapObjectReference::Weak(ToCodeT(*code)),
isolate);
MaybeObjectHandle group_slot(MaybeObject::FromSmi(Smi::FromInt(groups)),
......@@ -819,19 +824,17 @@ Handle<DependentCode> DependentCode::New(Isolate* isolate,
return result;
}
bool DependentCode::MarkCodeForDeoptimization(
DependentCode::DependencyGroups groups) {
void DependentCode::IterateAndCompact(const IterateAndCompactFn& fn) {
DisallowGarbageCollection no_gc;
int len = length();
if (len == 0) return false;
if (len == 0) return;
// We compact during traversal, thus use a somewhat custom loop construct:
//
// - Loop back-to-front s.t. trailing deoptimized entries can simply drop off
// - Loop back-to-front s.t. trailing cleared entries can simply drop off
// the back of the list.
// - Any cleared or deoptimized slots are filled from the back of the list.
bool marked_something = false;
// - Any cleared slots are filled from the back of the list.
int i = len - kSlotsPerEntry;
while (i >= 0) {
MaybeObject obj = Get(i + kCodeSlotOffset);
......@@ -841,23 +844,36 @@ bool DependentCode::MarkCodeForDeoptimization(
continue;
}
if ((Get(i + kGroupsSlotOffset).ToSmi().value() & groups) == 0) {
i -= kSlotsPerEntry;
continue;
if (fn(CodeT::cast(obj->GetHeapObjectAssumeWeak()),
static_cast<DependencyGroups>(
Get(i + kGroupsSlotOffset).ToSmi().value()))) {
len = FillEntryFromBack(i, len);
}
i -= kSlotsPerEntry;
}
set_length(len);
}
bool DependentCode::MarkCodeForDeoptimization(
DependentCode::DependencyGroups deopt_groups) {
DisallowGarbageCollection no_gc;
bool marked_something = false;
IterateAndCompact([&](CodeT codet, DependencyGroups groups) {
if ((groups & deopt_groups) == 0) return false;
// TODO(v8:11880): avoid roundtrips between cdc and code.
Code code = FromCodeT(CodeT::cast(obj->GetHeapObjectAssumeWeak()));
Code code = FromCodeT(codet);
if (!code.marked_for_deoptimization()) {
code.SetMarkedForDeoptimization("code dependencies");
marked_something = true;
}
len = FillEntryFromBack(i, len);
i -= kSlotsPerEntry;
}
return true;
});
set_length(len);
return marked_something;
}
......
......@@ -825,7 +825,7 @@ class DependentCode : public WeakArrayList {
void DeoptimizeDependentCodeGroup(Isolate* isolate, DependencyGroups groups);
bool MarkCodeForDeoptimization(DependencyGroups groups);
bool MarkCodeForDeoptimization(DependencyGroups deopt_groups);
V8_EXPORT_PRIVATE static DependentCode empty_dependent_code(
const ReadOnlyRoots& roots);
......@@ -850,6 +850,11 @@ class DependentCode : public WeakArrayList {
DependencyGroups groups,
Handle<Code> code);
// The callback is called for all non-cleared entries, and should return true
// iff the current entry should be cleared.
using IterateAndCompactFn = std::function<bool(CodeT, DependencyGroups)>;
void IterateAndCompact(const IterateAndCompactFn& fn);
// Fills the given entry with the last non-cleared entry in this list, and
// returns the new length after the last non-cleared entry has been moved.
int FillEntryFromBack(int index, int length);
......
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