Skip to content

Commit

Permalink
perf(flamegraph): Only repaint hovered rectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
bric3 committed Mar 16, 2022
1 parent 2c386ad commit ed326d9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public void setData(List<FrameBox<T>> frames,
canvas.setFlameGraphPainter(flameGraphPainter);
canvas.setToolTipTextFunction(tooltipTextFunction);
canvas.invalidate();
canvas.repaint();
}

/**
Expand Down Expand Up @@ -287,9 +288,9 @@ public void mouseClicked(MouseEvent e) {
.ifPresent(fgp -> {
fgp.toggleSelectedFrameAt(
(Graphics2D) viewPort.getView().getGraphics(),
point
point,
(frame, r) -> canvas.repaint()
);
scrollPane.repaint();
});
}
}
Expand All @@ -308,7 +309,7 @@ public void mouseExited(MouseEvent e) {
var scrollPane = (JScrollPane) e.getComponent();
canvas.getFlameGraphPainter()
.ifPresent(FlameGraphPainter::stopHover);
scrollPane.repaint();
canvas.repaint();
}
}

Expand All @@ -328,9 +329,9 @@ public void mouseMoved(MouseEvent e) {
.ifPresent(fgp -> fgp.hoverFrameAt(
(Graphics2D) view.getGraphics(),
point,
frame -> {
(frame, r) -> {
canvas.setToolTipText(frame);
scrollPane.repaint();
canvas.repaint(r);
}
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.awt.*;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -222,14 +223,14 @@ private void paint(Graphics2D g2, Rectangle visibleRect, boolean minimapMode) {
var nowWidth = g2d.getFontMetrics().stringWidth(drawTimeMs);
g2d.setColor(Color.DARK_GRAY);
g2d.fillRect(visibleRect.x + visibleRect.width - nowWidth - textBorder * 2,
visibleRect.y + visibleRect.height - frameBoxHeight,
nowWidth + textBorder * 2,
frameBoxHeight);
visibleRect.y + visibleRect.height - frameBoxHeight,
nowWidth + textBorder * 2,
frameBoxHeight);

g2d.setColor(Color.YELLOW);
g2d.drawString(drawTimeMs,
visibleRect.x + visibleRect.width - nowWidth - textBorder,
visibleRect.y + visibleRect.height - textBorder);
visibleRect.x + visibleRect.width - nowWidth - textBorder,
visibleRect.y + visibleRect.height - textBorder);
}

g2d.dispose();
Expand Down Expand Up @@ -328,6 +329,17 @@ private Rectangle paintFrameRectangle(Graphics2D g2, Rectangle frameRect, Color
return frameRect;
}

public Rectangle getFrameRectangle(Graphics2D g2, FrameBox<T> frame) {
var frameBoxHeight = getFrameBoxHeight(g2);

var rect = new Rectangle();
rect.x = (int) (flameGraphWidth * frame.startX); // + internalPadding;
rect.width = (int) (flameGraphWidth * frame.endX) - rect.x; // - internalPadding;
rect.y = frameBoxHeight * frame.stackDepth;
rect.height = frameBoxHeight;
return rect;
}

public Optional<FrameBox<T>> getFrameAt(Graphics2D g2, Point point) {
int depth = point.y / getFrameBoxHeight(g2);
double xLocation = ((double) point.x) / flameGraphWidth;
Expand All @@ -337,21 +349,28 @@ public Optional<FrameBox<T>> getFrameAt(Graphics2D g2, Point point) {
.findFirst();
}

public void toggleSelectedFrameAt(Graphics2D g2, Point point) {
public void toggleSelectedFrameAt(Graphics2D g2, Point point, BiConsumer<FrameBox<T>, Rectangle> toggleConsumer) {
getFrameAt(
g2,
point
).ifPresent(frame -> selectedFrame = selectedFrame == frame ? null : frame);
).ifPresent(frame -> {
selectedFrame = selectedFrame == frame ? null : frame;
toggleConsumer.accept(frame, getFrameRectangle(g2, frame));
});
}

public void hoverFrameAt(Graphics2D g2, Point point, Consumer<FrameBox<T>> hoverConsumer) {
public void hoverFrameAt(Graphics2D g2, Point point, BiConsumer<FrameBox<T>, Rectangle> hoverConsumer) {
getFrameAt(
g2,
point
).ifPresentOrElse(frame -> {
var oldHoveredFrame = hoveredFrame;
hoveredFrame = frame;
if (hoverConsumer != null) {
hoverConsumer.accept(frame);
hoverConsumer.accept(frame, getFrameRectangle(g2, frame));
if (oldHoveredFrame != null) {
hoverConsumer.accept(oldHoveredFrame, getFrameRectangle(g2, oldHoveredFrame));
}
}
},
this::stopHover);
Expand Down

0 comments on commit ed326d9

Please sign in to comment.