Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix maximum call stack size exceeded in some case #438

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/lib/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,15 @@ export class Profile {
// as immutable.
protected sortGroupedCallTree() {
function visit(node: CallTreeNode) {
node.children.sort((a, b) => -(a.getTotalWeight() - b.getTotalWeight()))
node.children.forEach(visit)
const stack:Array<CallTreeNode> = [node]
while (stack.length > 0) {
const currentNode = stack.pop()

if (currentNode) {
currentNode.children.sort((a, b) => -(a.getTotalWeight() - b.getTotalWeight()))
stack.push(...currentNode.children)
}
}
}
visit(this.groupedCalltreeRoot)
}
Expand Down
163 changes: 85 additions & 78 deletions src/views/flamechart-pan-zoom-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,96 +196,103 @@ export class FlamechartPanZoomView extends Component<FlamechartPanZoomViewProps,
const indirectlySelectedOutlineBatch = new BatchCanvasRectRenderer()
const matchedFrameBatch = new BatchCanvasRectRenderer()

const renderFrameLabelAndChildren = (frame: FlamechartFrame, depth = 0) => {
const width = frame.end - frame.start
const y = this.props.renderInverted ? this.configSpaceSize().y - 1 - depth : depth
const configSpaceBounds = new Rect(new Vec2(frame.start, y), new Vec2(width, 1))
const renderFrameLabelAndChildren = (frameInfo: {frame: FlamechartFrame; depth: number}) => {
const stack: Array<{frame: FlamechartFrame; depth: number}> = [frameInfo]
while (stack.length > 0) {
const info = stack.pop()
if (!info) {
continue
}

if (width < minConfigSpaceWidthToRender) return
if (configSpaceBounds.left() > this.props.configSpaceViewportRect.right()) return
if (configSpaceBounds.right() < this.props.configSpaceViewportRect.left()) return
const frame = info.frame
const depth = info.depth
const width = frame.end - frame.start
const y = this.props.renderInverted ? this.configSpaceSize().y - 1 - depth : depth
const configSpaceBounds = new Rect(new Vec2(frame.start, y), new Vec2(width, 1))

if (this.props.renderInverted) {
if (configSpaceBounds.bottom() < this.props.configSpaceViewportRect.top()) return
} else {
if (configSpaceBounds.top() > this.props.configSpaceViewportRect.bottom()) return
}
if (width < minConfigSpaceWidthToRender) return
if (configSpaceBounds.left() > this.props.configSpaceViewportRect.right()) return
if (configSpaceBounds.right() < this.props.configSpaceViewportRect.left()) return

if (configSpaceBounds.hasIntersectionWith(this.props.configSpaceViewportRect)) {
let physicalLabelBounds = configToPhysical.transformRect(configSpaceBounds)

if (physicalLabelBounds.left() < 0) {
physicalLabelBounds = physicalLabelBounds
.withOrigin(physicalLabelBounds.origin.withX(0))
.withSize(
physicalLabelBounds.size.withX(
physicalLabelBounds.size.x + physicalLabelBounds.left(),
),
)
}
if (physicalLabelBounds.right() > physicalViewSize.x) {
physicalLabelBounds = physicalLabelBounds.withSize(
physicalLabelBounds.size.withX(physicalViewSize.x - physicalLabelBounds.left()),
)
if (this.props.renderInverted) {
if (configSpaceBounds.bottom() < this.props.configSpaceViewportRect.top()) return
} else {
if (configSpaceBounds.top() > this.props.configSpaceViewportRect.bottom()) return
}

if (physicalLabelBounds.width() > minWidthToRender) {
const match = this.props.searchResults?.getMatchForFrame(frame.node.frame)

const trimmedText = trimTextMid(
ctx,
frame.node.frame.name,
physicalLabelBounds.width() - 2 * LABEL_PADDING_PX,
)
if (configSpaceBounds.hasIntersectionWith(this.props.configSpaceViewportRect)) {
let physicalLabelBounds = configToPhysical.transformRect(configSpaceBounds)

if (match) {
const rangesToHighlightInTrimmedText = remapRangesToTrimmedText(
trimmedText,
match
if (physicalLabelBounds.left() < 0) {
physicalLabelBounds = physicalLabelBounds
.withOrigin(physicalLabelBounds.origin.withX(0))
.withSize(
physicalLabelBounds.size.withX(
physicalLabelBounds.size.x + physicalLabelBounds.left(),
),
)
}
if (physicalLabelBounds.right() > physicalViewSize.x) {
physicalLabelBounds = physicalLabelBounds.withSize(
physicalLabelBounds.size.withX(physicalViewSize.x - physicalLabelBounds.left()),
)
}

// Once we have the character ranges to highlight, we need to
// actually do the highlighting.
let lastEndIndex = 0
let left = physicalLabelBounds.left() + LABEL_PADDING_PX
if (physicalLabelBounds.width() > minWidthToRender) {
const match = this.props.searchResults?.getMatchForFrame(frame.node.frame)

const padding = (physicalViewSpaceFrameHeight - physicalViewSpaceFontSize) / 2 - 2
for (let [startIndex, endIndex] of rangesToHighlightInTrimmedText) {
left += cachedMeasureTextWidth(
ctx,
trimmedText.trimmedString.substring(lastEndIndex, startIndex),
)
const highlightWidth = cachedMeasureTextWidth(
ctx,
trimmedText.trimmedString.substring(startIndex, endIndex),
)
matchedTextHighlightBatch.rect({
x: left,
y: physicalLabelBounds.top() + padding,
w: highlightWidth,
h: physicalViewSpaceFrameHeight - 2 * padding,
})

left += highlightWidth
lastEndIndex = endIndex
const trimmedText = trimTextMid(
ctx,
frame.node.frame.name,
physicalLabelBounds.width() - 2 * LABEL_PADDING_PX,
)

if (match) {
const rangesToHighlightInTrimmedText = remapRangesToTrimmedText(trimmedText, match)

// Once we have the character ranges to highlight, we need to
// actually do the highlighting.
let lastEndIndex = 0
let left = physicalLabelBounds.left() + LABEL_PADDING_PX

const padding = (physicalViewSpaceFrameHeight - physicalViewSpaceFontSize) / 2 - 2
for (let [startIndex, endIndex] of rangesToHighlightInTrimmedText) {
left += cachedMeasureTextWidth(
ctx,
trimmedText.trimmedString.substring(lastEndIndex, startIndex),
)
const highlightWidth = cachedMeasureTextWidth(
ctx,
trimmedText.trimmedString.substring(startIndex, endIndex),
)
matchedTextHighlightBatch.rect({
x: left,
y: physicalLabelBounds.top() + padding,
w: highlightWidth,
h: physicalViewSpaceFrameHeight - 2 * padding,
})

left += highlightWidth
lastEndIndex = endIndex
}
}
}

const batch = this.props.searchResults != null && !match ? fadedLabelBatch : labelBatch
batch.text({
text: trimmedText.trimmedString,
const batch = this.props.searchResults != null && !match ? fadedLabelBatch : labelBatch
batch.text({
text: trimmedText.trimmedString,

// This is specifying the position of the starting text baseline.
x: physicalLabelBounds.left() + LABEL_PADDING_PX,
y: Math.round(
physicalLabelBounds.bottom() -
(physicalViewSpaceFrameHeight - physicalViewSpaceFontSize) / 2,
),
})
// This is specifying the position of the starting text baseline.
x: physicalLabelBounds.left() + LABEL_PADDING_PX,
y: Math.round(
physicalLabelBounds.bottom() -
(physicalViewSpaceFrameHeight - physicalViewSpaceFontSize) / 2,
),
})
}
}
for (let child of frame.children) {
stack.push({frame: child, depth: depth + 1})
}
}
for (let child of frame.children) {
renderFrameLabelAndChildren(child, depth + 1)
}
}

Expand Down Expand Up @@ -342,7 +349,7 @@ export class FlamechartPanZoomView extends Component<FlamechartPanZoomViewProps,
}

for (let frame of this.props.flamechart.getLayers()[0] || []) {
renderFrameLabelAndChildren(frame)
renderFrameLabelAndChildren({frame, depth: 0})
}

const theme = this.props.theme
Expand Down