Skip to content

Commit 3ec314d

Browse files
committed
Simplify
1 parent b1e1a0e commit 3ec314d

File tree

2 files changed

+9
-47
lines changed

2 files changed

+9
-47
lines changed

Lib/profiling/sampling/flamegraph.js

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -403,26 +403,14 @@ function populateStats(data) {
403403
const functionMap = new Map();
404404

405405
function collectFunctions(node) {
406-
// Debug to understand the node structure
407406
if (!node) return;
408407

409-
// Try multiple ways to get the filename and function name
410-
let filename = node.filename;
411-
let funcname = node.funcname || node.name;
408+
let filename = typeof node.filename === 'number' ? resolveString(node.filename) : node.filename;
409+
let funcname = typeof node.funcname === 'number' ? resolveString(node.funcname) : node.funcname;
412410

413-
// If they're numbers (string indices), resolve them
414-
if (typeof filename === 'number') {
415-
filename = resolveString(filename);
416-
}
417-
if (typeof funcname === 'number') {
418-
funcname = resolveString(funcname);
419-
}
420-
421-
// If they're still undefined or null, try extracting from the name field
422-
if (!filename && node.name) {
411+
if (!filename || !funcname) {
423412
const nameStr = typeof node.name === 'number' ? resolveString(node.name) : node.name;
424-
if (nameStr && nameStr.includes('(')) {
425-
// Parse format: "funcname (filename:lineno)"
413+
if (nameStr?.includes('(')) {
426414
const match = nameStr.match(/^(.+?)\s*\((.+?):(\d+)\)$/);
427415
if (match) {
428416
funcname = funcname || match[1];
@@ -431,7 +419,6 @@ function populateStats(data) {
431419
}
432420
}
433421

434-
// Final fallback
435422
filename = filename || 'unknown';
436423
funcname = funcname || 'unknown';
437424

@@ -486,10 +473,9 @@ function populateStats(data) {
486473
const num = i + 1;
487474
if (i < hotSpots.length && hotSpots[i]) {
488475
const hotspot = hotSpots[i];
489-
// Safe extraction with fallbacks
490476
const filename = hotspot.filename || 'unknown';
491477
const basename = filename !== 'unknown' ? filename.split('/').pop() : 'unknown';
492-
const lineno = hotspot.lineno !== undefined && hotspot.lineno !== null ? hotspot.lineno : '?';
478+
const lineno = hotspot.lineno ?? '?';
493479
let funcDisplay = hotspot.funcname || 'unknown';
494480
if (funcDisplay.length > 35) {
495481
funcDisplay = funcDisplay.substring(0, 32) + '...';
@@ -591,7 +577,6 @@ function filterByThread() {
591577
const threadId = parseInt(selectedThread);
592578
filteredData = filterDataByThread(originalData, threadId);
593579

594-
// Ensure string indices are resolved for the filtered data
595580
if (filteredData.strings) {
596581
stringTable = filteredData.strings;
597582
filteredData = resolveStringIndices(filteredData);
@@ -605,33 +590,16 @@ function filterByThread() {
605590
}
606591

607592
function filterDataByThread(data, threadId) {
608-
// Deep clone the data structure and filter by thread
609593
function filterNode(node) {
610-
// Check if this node contains the thread
611594
if (!node.threads || !node.threads.includes(threadId)) {
612595
return null;
613596
}
614597

615-
// Create a filtered copy of the node, preserving all fields
616598
const filteredNode = {
617-
name: node.name,
618-
value: node.value,
619-
filename: node.filename,
620-
funcname: node.funcname,
621-
lineno: node.lineno,
622-
threads: node.threads,
623-
source: node.source,
599+
...node,
624600
children: []
625601
};
626602

627-
// Copy any other properties that might exist
628-
Object.keys(node).forEach(key => {
629-
if (!(key in filteredNode)) {
630-
filteredNode[key] = node[key];
631-
}
632-
});
633-
634-
// Recursively filter children
635603
if (node.children && Array.isArray(node.children)) {
636604
filteredNode.children = node.children
637605
.map(child => filterNode(child))
@@ -641,28 +609,22 @@ function filterDataByThread(data, threadId) {
641609
return filteredNode;
642610
}
643611

644-
// Create filtered root, preserving all metadata
645612
const filteredRoot = {
646613
...data,
647-
children: [],
648-
strings: data.strings // Preserve string table
614+
children: []
649615
};
650616

651-
// Filter children
652617
if (data.children && Array.isArray(data.children)) {
653618
filteredRoot.children = data.children
654619
.map(child => filterNode(child))
655620
.filter(child => child !== null);
656621
}
657622

658-
// Recalculate total value based on filtered children
659623
function recalculateValue(node) {
660624
if (!node.children || node.children.length === 0) {
661625
return node.value || 0;
662626
}
663-
const childrenValue = node.children.reduce((sum, child) => {
664-
return sum + recalculateValue(child);
665-
}, 0);
627+
const childrenValue = node.children.reduce((sum, child) => sum + recalculateValue(child), 0);
666628
node.value = Math.max(node.value || 0, childrenValue);
667629
return node.value;
668630
}

Lib/profiling/sampling/stack_collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def convert_children(children, min_samples):
135135
"filename": filename_idx,
136136
"lineno": func[1],
137137
"funcname": funcname_idx,
138-
"threads": list(node.get("threads", set())),
138+
"threads": sorted(list(node.get("threads", set()))),
139139
}
140140

141141
source = self._get_source_lines(func)

0 commit comments

Comments
 (0)