Skip to content

Commit 37180c7

Browse files
committed
deps: add LookupAlsValue to V8 heap profiler
Expose a public API to look up ALS values from the CPED map, so the allocator can resolve labels without duplicating V8 internal logic. Signed-off-by: Rudolf Meijering <[email protected]>
1 parent 18a6b90 commit 37180c7

4 files changed

Lines changed: 43 additions & 0 deletions

File tree

deps/v8/include/v8-profiler.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,16 @@ class V8_EXPORT HeapProfiler {
13301330
* Pass an empty handle to clear.
13311331
*/
13321332
void SetHeapProfileSampleLabelsKey(Local<Value> key);
1333+
1334+
/**
1335+
* Extracts the ALS value from a CPED (ContinuationPreservedEmbedderData)
1336+
* Map using the stored ALS key (set via SetHeapProfileSampleLabelsKey).
1337+
*
1338+
* Uses OrderedHashMap::FindEntry internally — zero-allocation, GC-safe.
1339+
* Returns an empty MaybeLocal if the CPED is not a Map, no ALS key is set,
1340+
* or the key is not found in the Map.
1341+
*/
1342+
MaybeLocal<Value> LookupAlsValue(Local<Value> cped);
13331343
#endif // V8_HEAP_PROFILER_SAMPLE_LABELS
13341344

13351345
/**

deps/v8/src/api/api.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11840,6 +11840,10 @@ void HeapProfiler::SetHeapProfileSampleLabelsKey(Local<Value> key) {
1184011840
reinterpret_cast<i::HeapProfiler*>(this)
1184111841
->SetHeapProfileSampleLabelsKey(key);
1184211842
}
11843+
11844+
MaybeLocal<Value> HeapProfiler::LookupAlsValue(Local<Value> cped) {
11845+
return reinterpret_cast<i::HeapProfiler*>(this)->LookupAlsValue(cped);
11846+
}
1184311847
#endif // V8_HEAP_PROFILER_SAMPLE_LABELS
1184411848

1184511849
bool HeapProfiler::IsTakingSnapshot() {

deps/v8/src/profiler/heap-profiler.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "src/heap/heap.h"
1919
#include "src/objects/cpp-heap-object-wrapper-inl.h"
2020
#include "src/objects/js-array-buffer-inl.h"
21+
#include "src/objects/js-collection-inl.h"
22+
#include "src/objects/ordered-hash-table.h"
2123
#include "src/profiler/allocation-tracker.h"
2224
#include "src/profiler/heap-snapshot-generator-inl.h"
2325
#include "src/profiler/sampling-heap-profiler.h"
@@ -405,4 +407,27 @@ void HeapProfiler::QueryObjects(DirectHandle<Context> context,
405407
});
406408
}
407409

410+
#ifdef V8_HEAP_PROFILER_SAMPLE_LABELS
411+
v8::MaybeLocal<v8::Value> HeapProfiler::LookupAlsValue(
412+
v8::Local<v8::Value> cped) {
413+
if (sample_labels_als_key_.IsEmpty() || cped.IsEmpty()) {
414+
return v8::MaybeLocal<v8::Value>();
415+
}
416+
Tagged<Object> cped_obj = *Utils::OpenDirectHandle(*cped);
417+
if (!IsJSMap(cped_obj)) return v8::MaybeLocal<v8::Value>();
418+
419+
Tagged<JSMap> js_map = Cast<JSMap>(cped_obj);
420+
Tagged<OrderedHashMap> table = Cast<OrderedHashMap>(js_map->table());
421+
422+
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate());
423+
v8::Local<v8::Value> als_key_local = sample_labels_als_key_.Get(v8_isolate);
424+
Tagged<Object> key_obj = *Utils::OpenDirectHandle(*als_key_local);
425+
InternalIndex entry = table->FindEntry(isolate(), key_obj);
426+
if (!entry.is_found()) return v8::MaybeLocal<v8::Value>();
427+
428+
Tagged<Object> value = table->ValueAt(entry);
429+
return Utils::ToLocal(direct_handle(value, isolate()));
430+
}
431+
#endif // V8_HEAP_PROFILER_SAMPLE_LABELS
432+
408433
} // namespace v8::internal

deps/v8/src/profiler/heap-profiler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ class HeapProfiler : public HeapObjectAllocationTracker {
105105
const v8::Global<v8::Value>& sample_labels_als_key() const {
106106
return sample_labels_als_key_;
107107
}
108+
109+
// Extracts the ALS value from a CPED Map using the stored ALS key.
110+
// Uses OrderedHashMap::FindEntry — zero-allocation, GC-safe.
111+
v8::MaybeLocal<v8::Value> LookupAlsValue(v8::Local<v8::Value> cped);
108112
#endif // V8_HEAP_PROFILER_SAMPLE_LABELS
109113

110114
void StartHeapObjectsTracking(bool track_allocations);

0 commit comments

Comments
 (0)