Skip to content

Commit ebd1578

Browse files
marjakhV8 LUCI CQ
authored andcommitted
[objects]: Defer CallSiteInfo creation
Store the raw data in a FixedArray and create the CallSiteInfo objects only on demand. This can be further optimized to omit CallSiteInfo creation altogether in some code paths, but currently those code paths are not critically important. Change-Id: I6480862caf6b64020737527c571e3e3eac704ed3 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7673818 Commit-Queue: Marja Hölttä <[email protected]> Reviewed-by: Leszek Swirski <[email protected]> Cr-Commit-Position: refs/heads/main@{#106127}
1 parent 6294f47 commit ebd1578

11 files changed

Lines changed: 202 additions & 66 deletions

File tree

‎src/execution/isolate.cc‎

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -879,18 +879,21 @@ class CallSiteBuilder {
879879
// framework and library code, and stack depth tends to be more than
880880
// a dozen frames, so we over-allocate a bit here to avoid growing
881881
// the elements array in the common case.
882-
elements_ = isolate->factory()->NewFixedArray(std::min(64u, limit));
882+
elements_ = isolate->factory()->NewFixedArray(CallSiteInfo::Fields::kCount *
883+
std::min(64u, limit_));
883884
}
884885

885886
void SetPrevFrameAsConstructCall() {
886887
if (skipped_prev_frame_) return;
887888
DCHECK_GT(index_, 0);
888-
Tagged<CallSiteInfo> info =
889-
Tagged<CallSiteInfo>::cast(elements_->get(index_ - 1));
889+
int base_index = (index_ - 1) * CallSiteInfo::Fields::kCount;
890+
int flags =
891+
Smi::ToInt(elements_->get(base_index + CallSiteInfo::Fields::kFlags));
890892
#if V8_ENABLE_WEBASSEMBLY
891-
if (info->IsWasm()) return;
893+
if (flags & CallSiteInfo::kIsWasm) return;
892894
#endif
893-
info->set_flags(info->flags() | CallSiteInfo::kIsConstructor);
895+
elements_->set(base_index + CallSiteInfo::Fields::kFlags,
896+
Smi::FromInt(flags | CallSiteInfo::kIsConstructor));
894897
}
895898

896899
bool Visit(FrameSummary const& summary) {
@@ -1036,7 +1039,8 @@ class CallSiteBuilder {
10361039
bool Full() { return index_ >= limit_; }
10371040

10381041
Handle<FixedArray> Build() {
1039-
return FixedArray::RightTrimOrEmpty(isolate_, elements_, index_);
1042+
return FixedArray::RightTrimOrEmpty(isolate_, elements_,
1043+
CallSiteInfo::Fields::kCount * index_);
10401044
}
10411045

10421046
private:
@@ -1101,16 +1105,42 @@ class CallSiteBuilder {
11011105

11021106
void AppendFrame(DirectHandle<UnionOf<JSAny, Hole>> receiver_or_instance,
11031107
DirectHandle<UnionOf<Smi, JSFunction>> function,
1104-
DirectHandle<Union<Code, BytecodeArray, Undefined>> code,
1108+
DirectHandle<Union<Code, BytecodeArray, Undefined>> code_obj,
11051109
int offset, int flags) {
11061110
if (IsTheHole(*receiver_or_instance, isolate_)) {
11071111
// TODO(jgruber): Fix all cases in which frames give us a hole value
11081112
// (e.g. the receiver in RegExp constructor frames).
11091113
receiver_or_instance = isolate_->factory()->undefined_value();
11101114
}
1111-
auto info = isolate_->factory()->NewCallSiteInfo(
1112-
Cast<JSAny>(receiver_or_instance), function, code, offset, flags);
1113-
elements_ = FixedArray::SetAndGrow(isolate_, elements_, index_++, info);
1115+
1116+
int base_index = index_ * CallSiteInfo::Fields::kCount;
1117+
1118+
// Set the last field first and grow the array if needed.
1119+
static_assert(CallSiteInfo::Fields::kFlags ==
1120+
CallSiteInfo::Fields::kCount - 1);
1121+
elements_ = FixedArray::SetAndGrow(
1122+
isolate_, elements_, base_index + CallSiteInfo::Fields::kFlags,
1123+
Smi::FromInt(flags));
1124+
1125+
elements_->set(base_index + CallSiteInfo::Fields::kReceiver,
1126+
*receiver_or_instance);
1127+
elements_->set(base_index + CallSiteInfo::Fields::kFunction, *function);
1128+
1129+
if (DirectHandle<Code> code; TryCast(code_obj, &code)) {
1130+
elements_->set(base_index + CallSiteInfo::Fields::kCode, code->wrapper());
1131+
} else if (DirectHandle<BytecodeArray> bytecode;
1132+
TryCast(code_obj, &bytecode)) {
1133+
elements_->set(base_index + CallSiteInfo::Fields::kCode,
1134+
bytecode->wrapper());
1135+
} else {
1136+
elements_->set(base_index + CallSiteInfo::Fields::kCode,
1137+
*isolate_->factory()->undefined_value());
1138+
}
1139+
1140+
elements_->set(base_index + CallSiteInfo::Fields::kOffset,
1141+
Smi::FromInt(offset));
1142+
1143+
index_++;
11141144
skipped_prev_frame_ = false;
11151145
}
11161146

@@ -1423,15 +1453,18 @@ Handle<FixedArray> CaptureSimpleStackTrace(Isolate* isolate, int limit,
14231453
}
14241454

14251455
DirectHandle<StackTraceInfo> GetDetailedStackTraceFromCallSiteInfos(
1426-
Isolate* isolate, DirectHandle<FixedArray> call_site_infos,
1456+
Isolate* isolate, DirectHandle<FixedArray> raw_data_for_call_site_infos,
14271457
uint32_t limit) {
1428-
uint32_t call_site_infos_len = call_site_infos->ulength().value();
1458+
uint32_t call_site_infos_len =
1459+
raw_data_for_call_site_infos->ulength().value() /
1460+
CallSiteInfo::Fields::kCount;
14291461
auto frames =
14301462
isolate->factory()->NewFixedArray(std::min(limit, call_site_infos_len));
14311463
uint32_t index = 0;
14321464
for (uint32_t i = 0; i < call_site_infos_len && index < limit; ++i) {
1433-
DirectHandle<CallSiteInfo> call_site_info(
1434-
Cast<CallSiteInfo>(call_site_infos->get(i)), isolate);
1465+
DirectHandle<CallSiteInfo> call_site_info =
1466+
CallSiteInfo::ConstructFromRawData(isolate,
1467+
raw_data_for_call_site_infos, i);
14351468
if (call_site_info->IsAsync()) {
14361469
break;
14371470
}
@@ -1502,18 +1535,20 @@ MaybeDirectHandle<JSObject> Isolate::CaptureAndSetErrorStack(
15021535
stack_trace_for_uncaught_exceptions_frame_limit_,
15031536
stack_trace_for_uncaught_exceptions_options_);
15041537
} else {
1505-
auto call_site_infos =
1538+
auto raw_data_for_call_site_infos =
15061539
Cast<FixedArray>(call_site_infos_or_formatted_stack);
15071540
DCHECK_GE(stack_trace_for_uncaught_exceptions_frame_limit_, 0);
15081541
stack_trace = GetDetailedStackTraceFromCallSiteInfos(
1509-
this, call_site_infos,
1542+
this, raw_data_for_call_site_infos,
15101543
static_cast<uint32_t>(
15111544
stack_trace_for_uncaught_exceptions_frame_limit_));
15121545
DCHECK_GE(stack_trace_limit, 0);
1513-
if (static_cast<uint32_t>(stack_trace_limit) <
1514-
call_site_infos->ulength().value()) {
1546+
if (static_cast<uint32_t>(stack_trace_limit) *
1547+
CallSiteInfo::Fields::kCount <
1548+
raw_data_for_call_site_infos->ulength().value()) {
15151549
call_site_infos_or_formatted_stack = FixedArray::RightTrimOrEmpty(
1516-
this, call_site_infos, stack_trace_limit);
1550+
this, raw_data_for_call_site_infos,
1551+
stack_trace_limit * CallSiteInfo::Fields::kCount);
15171552
}
15181553
// Notify the debugger.
15191554
OnStackTraceCaptured(stack_trace);
@@ -1543,17 +1578,27 @@ Handle<FixedArray> Isolate::GetSimpleStackTrace(
15431578
ErrorUtils::StackPropertyLookupResult lookup =
15441579
ErrorUtils::GetErrorStackProperty(this, maybe_error_object);
15451580

1581+
Handle<FixedArray> raw_data;
15461582
if (IsFixedArray(*lookup.error_stack)) {
1547-
return Cast<FixedArray>(lookup.error_stack);
1548-
}
1549-
if (!IsErrorStackData(*lookup.error_stack)) {
1583+
raw_data = Cast<FixedArray>(lookup.error_stack);
1584+
} else if (IsErrorStackData(*lookup.error_stack)) {
1585+
auto error_stack_data = Cast<ErrorStackData>(lookup.error_stack);
1586+
if (!error_stack_data->HasRawDataForCallSiteInfos()) {
1587+
return factory()->empty_fixed_array();
1588+
}
1589+
raw_data = handle(error_stack_data->raw_data_for_call_site_infos(), this);
1590+
} else {
15501591
return factory()->empty_fixed_array();
15511592
}
1552-
auto error_stack_data = Cast<ErrorStackData>(lookup.error_stack);
1553-
if (!error_stack_data->HasCallSiteInfos()) {
1554-
return factory()->empty_fixed_array();
1593+
1594+
int frame_count = raw_data->ulength().value() / CallSiteInfo::Fields::kCount;
1595+
Handle<FixedArray> call_site_infos = factory()->NewFixedArray(frame_count);
1596+
for (int i = 0; i < frame_count; ++i) {
1597+
DirectHandle<CallSiteInfo> call_site_info =
1598+
CallSiteInfo::ConstructFromRawData(this, raw_data, i);
1599+
call_site_infos->set(i, *call_site_info);
15551600
}
1556-
return handle(error_stack_data->call_site_infos(), this);
1601+
return call_site_infos;
15571602
}
15581603

15591604
Address Isolate::GetAbstractPC(int* line, int* column) {
@@ -3219,9 +3264,11 @@ void Isolate::PrintCurrentStackTrace(
32193264
SKIP_NONE, factory()->undefined_value());
32203265

32213266
IncrementalStringBuilder builder(this);
3222-
uint32_t frames_len = frames->ulength().value();
3223-
for (uint32_t i = 0; i < frames_len; ++i) {
3224-
DirectHandle<CallSiteInfo> frame(Cast<CallSiteInfo>(frames->get(i)), this);
3267+
uint32_t frame_count =
3268+
frames->ulength().value() / CallSiteInfo::Fields::kCount;
3269+
for (uint32_t i = 0; i < frame_count; ++i) {
3270+
DirectHandle<CallSiteInfo> frame =
3271+
CallSiteInfo::ConstructFromRawData(this, frames, i);
32253272

32263273
if (should_include_frame_callback) {
32273274
Tagged<Object> raw_script_name = frame->GetScriptNameOrSourceURL();
@@ -3245,7 +3292,7 @@ void Isolate::PrintCurrentStackTrace(
32453292
SerializeCallSiteInfo(this, frame, &builder);
32463293
}
32473294

3248-
if (i != frames_len - 1) builder.AppendCharacter('\n');
3295+
if (i != frame_count - 1) builder.AppendCharacter('\n');
32493296
}
32503297

32513298
DirectHandle<String> stack_trace = builder.Finish().ToHandleChecked();
@@ -3315,8 +3362,8 @@ bool Isolate::ComputeLocationFromSimpleStackTrace(
33153362
}
33163363
DirectHandle<FixedArray> call_site_infos =
33173364
GetSimpleStackTrace(Cast<JSReceiver>(exception));
3318-
uint32_t call_site_infos_len = call_site_infos->ulength().value();
3319-
for (uint32_t i = 0; i < call_site_infos_len; ++i) {
3365+
uint32_t call_site_infos_count = call_site_infos->ulength().value();
3366+
for (uint32_t i = 0; i < call_site_infos_count; ++i) {
33203367
DirectHandle<CallSiteInfo> call_site_info(
33213368
Cast<CallSiteInfo>(call_site_infos->get(i)), this);
33223369
if (CallSiteInfo::ComputeLocation(call_site_info, target)) {

‎src/execution/messages.cc‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "src/parsing/parse-info.h"
2525
#include "src/parsing/parsing.h"
2626
#include "src/roots/roots.h"
27+
#include "src/sandbox/indirect-pointer-inl.h"
2728
#include "src/strings/string-builder-inl.h"
2829

2930
namespace v8 {
@@ -202,15 +203,16 @@ namespace {
202203

203204
// Convert the raw frames as written by Isolate::CaptureSimpleStackTrace into
204205
// a JSArray of JSCallSite objects.
205-
MaybeDirectHandle<JSArray> GetStackFrames(Isolate* isolate,
206-
DirectHandle<FixedArray> frames) {
207-
uint32_t frame_count = frames->ulength().value();
206+
MaybeDirectHandle<JSArray> GetStackFrames(
207+
Isolate* isolate, DirectHandle<FixedArray> raw_data_for_call_site_infos) {
208+
uint32_t frame_count = raw_data_for_call_site_infos->ulength().value() /
209+
CallSiteInfo::Fields::kCount;
208210
DirectHandle<JSFunction> constructor = isolate->callsite_function();
209211
DirectHandle<FixedArray> sites =
210212
isolate->factory()->NewFixedArray(frame_count);
211213
for (uint32_t i = 0; i < frame_count; ++i) {
212-
DirectHandle<CallSiteInfo> frame(Cast<CallSiteInfo>(frames->get(i)),
213-
isolate);
214+
DirectHandle<CallSiteInfo> frame = CallSiteInfo::ConstructFromRawData(
215+
isolate, raw_data_for_call_site_infos, i);
214216
DirectHandle<JSObject> site;
215217
ASSIGN_RETURN_ON_EXCEPTION(isolate, site,
216218
JSObject::New(constructor, constructor,
@@ -292,7 +294,7 @@ MaybeDirectHandle<Object> ErrorUtils::FormatStackTrace(
292294
return isolate->factory()->empty_string();
293295
}
294296
DCHECK(IsFixedArray(*raw_stack));
295-
auto elems = Cast<FixedArray>(raw_stack);
297+
auto raw_data_for_call_site_infos = Cast<FixedArray>(raw_stack);
296298

297299
const bool in_recursion = isolate->formatting_stack_trace();
298300
const bool has_overflowed = i::StackLimitCheck{isolate}.HasOverflowed();
@@ -303,8 +305,9 @@ MaybeDirectHandle<Object> ErrorUtils::FormatStackTrace(
303305
PrepareStackTraceScope scope(isolate);
304306

305307
DirectHandle<JSArray> sites;
306-
ASSIGN_RETURN_ON_EXCEPTION(isolate, sites,
307-
GetStackFrames(isolate, elems));
308+
ASSIGN_RETURN_ON_EXCEPTION(
309+
isolate, sites,
310+
GetStackFrames(isolate, raw_data_for_call_site_infos));
308311

309312
DirectHandle<Object> result;
310313
ASSIGN_RETURN_ON_EXCEPTION(
@@ -329,8 +332,9 @@ MaybeDirectHandle<Object> ErrorUtils::FormatStackTrace(
329332
isolate->CountUsage(v8::Isolate::kErrorPrepareStackTrace);
330333

331334
DirectHandle<JSArray> sites;
332-
ASSIGN_RETURN_ON_EXCEPTION(isolate, sites,
333-
GetStackFrames(isolate, elems));
335+
ASSIGN_RETURN_ON_EXCEPTION(
336+
isolate, sites,
337+
GetStackFrames(isolate, raw_data_for_call_site_infos));
334338

335339
constexpr int argc = 2;
336340
std::array<DirectHandle<Object>, argc> args;
@@ -359,13 +363,16 @@ MaybeDirectHandle<Object> ErrorUtils::FormatStackTrace(
359363

360364
RETURN_ON_EXCEPTION(isolate, AppendErrorString(isolate, error, &builder));
361365

362-
uint32_t elems_len = elems->ulength().value();
366+
uint32_t elems_len = raw_data_for_call_site_infos->ulength().value() /
367+
CallSiteInfo::Fields::kCount;
363368
for (uint32_t i = 0; i < elems_len; ++i) {
364369
builder.AppendCStringLiteral("\n at ");
365370

366-
DirectHandle<CallSiteInfo> frame(Cast<CallSiteInfo>(elems->get(i)),
367-
isolate);
371+
DirectHandle<CallSiteInfo> frame = CallSiteInfo::ConstructFromRawData(
372+
isolate, raw_data_for_call_site_infos, i);
368373

374+
// TODO(marja): Avoid CallSiteInfo creation since we serialize it right
375+
// away.
369376
v8::TryCatch try_catch(reinterpret_cast<v8::Isolate*>(isolate));
370377
SerializeCallSiteInfo(isolate, frame, &builder);
371378

@@ -1166,7 +1173,8 @@ MaybeDirectHandle<Object> ErrorUtils::GetFormattedStack(
11661173
isolate, formatted_stack,
11671174
FormatStackTrace(
11681175
isolate, error_object,
1169-
direct_handle(error_stack_data->call_site_infos(), isolate)));
1176+
direct_handle(error_stack_data->raw_data_for_call_site_infos(),
1177+
isolate)));
11701178
error_stack_data->set_formatted_stack(*formatted_stack);
11711179
return formatted_stack;
11721180
}

‎src/heap/factory.cc‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,13 +1719,14 @@ DirectHandle<InterceptorInfo> Factory::NewInterceptorInfo(
17191719
}
17201720

17211721
DirectHandle<ErrorStackData> Factory::NewErrorStackData(
1722-
DirectHandle<UnionOf<JSAny, FixedArray>> call_site_infos_or_formatted_stack,
1722+
DirectHandle<UnionOf<JSAny, FixedArray>>
1723+
raw_data_for_call_site_infos_or_formatted_stack,
17231724
DirectHandle<StackTraceInfo> stack_trace) {
17241725
Tagged<ErrorStackData> error_stack_data = NewStructInternal<ErrorStackData>(
17251726
ERROR_STACK_DATA_TYPE, AllocationType::kYoung);
17261727
DisallowGarbageCollection no_gc;
1727-
error_stack_data->set_call_site_infos_or_formatted_stack(
1728-
*call_site_infos_or_formatted_stack, SKIP_WRITE_BARRIER);
1728+
error_stack_data->set_raw_data_for_call_site_infos_or_formatted_stack(
1729+
*raw_data_for_call_site_infos_or_formatted_stack, SKIP_WRITE_BARRIER);
17291730
error_stack_data->set_stack_trace(*stack_trace, SKIP_WRITE_BARRIER);
17301731
return direct_handle(error_stack_data, isolate());
17311732
}

‎src/heap/factory.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ class V8_EXPORT_PRIVATE Factory : public FactoryBase<Factory> {
487487

488488
DirectHandle<ErrorStackData> NewErrorStackData(
489489
DirectHandle<UnionOf<JSAny, FixedArray>>
490-
call_site_infos_or_formatted_stack,
490+
raw_data_for_call_site_infos_or_formatted_stack,
491491
DirectHandle<StackTraceInfo> stack_trace);
492492

493493
Handle<Script> CloneScript(DirectHandle<Script> script,

‎src/objects/call-site-info.cc‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,36 @@
1717

1818
namespace v8::internal {
1919

20+
// static
21+
DirectHandle<CallSiteInfo> CallSiteInfo::ConstructFromRawData(
22+
Isolate* isolate, DirectHandle<FixedArray> frames, int index) {
23+
int base_index = index * Fields::kCount;
24+
DirectHandle<Object> receiver(frames->get(base_index + Fields::kReceiver),
25+
isolate);
26+
DirectHandle<Object> function(frames->get(base_index + Fields::kFunction),
27+
isolate);
28+
DirectHandle<Object> code_obj(frames->get(base_index + Fields::kCode),
29+
isolate);
30+
DirectHandle<Union<Code, BytecodeArray, Undefined>> resolved_code;
31+
if (IsCodeWrapper(*code_obj)) {
32+
resolved_code = handle(Cast<CodeWrapper>(code_obj)->code(isolate), isolate);
33+
} else if (IsBytecodeWrapper(*code_obj)) {
34+
resolved_code =
35+
handle(Cast<BytecodeWrapper>(code_obj)->bytecode(isolate), isolate);
36+
} else if (IsUndefined(*code_obj)) {
37+
resolved_code = isolate->factory()->undefined_value();
38+
} else {
39+
UNREACHABLE();
40+
}
41+
42+
int offset = Smi::ToInt(frames->get(base_index + Fields::kOffset));
43+
int flags = Smi::ToInt(frames->get(base_index + Fields::kFlags));
44+
45+
return isolate->factory()->NewCallSiteInfo(
46+
Cast<JSAny>(receiver), Cast<UnionOf<Smi, JSFunction>>(function),
47+
resolved_code, offset, flags);
48+
}
49+
2050
bool CallSiteInfo::IsPromiseAll() const {
2151
if (!IsAsync()) return false;
2252
Tagged<JSFunction> fun = Cast<JSFunction>(function());

‎src/objects/call-site-info.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ V8_OBJECT class CallSiteInfo : public StructLayout {
7777
// Used to signal that the requested field is unknown.
7878
static constexpr int kUnknown = kNoSourcePosition;
7979

80+
// For delayed CallSiteInfo creation (storing the raw data for several
81+
// CallSiteInfos in a FixedArray).
82+
enum Fields { kCode = 0, kReceiver, kFunction, kOffset, kFlags, kCount };
83+
84+
static DirectHandle<CallSiteInfo> ConstructFromRawData(
85+
Isolate* isolate, DirectHandle<FixedArray> frames, int index);
86+
8087
V8_EXPORT_PRIVATE static int GetLineNumber(DirectHandle<CallSiteInfo> info);
8188
V8_EXPORT_PRIVATE static int GetColumnNumber(DirectHandle<CallSiteInfo> info);
8289

0 commit comments

Comments
 (0)