Skip to content

Commit

Permalink
Show a size estimate for the png export button (#4505)
Browse files Browse the repository at this point in the history
* Show an estimate for PNG export size

* Prettier ignore a line

* Puggify

Co-authored-by: Matt Godbolt <matt@godbolt.org>

Co-authored-by: Matt Godbolt <matt@godbolt.org>
  • Loading branch information
jeremy-rifkin and mattgodbolt committed Jan 4, 2023
1 parent 124a9d8 commit 742ee2f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
35 changes: 35 additions & 0 deletions static/panes/cfg-view.ts
Expand Up @@ -61,6 +61,8 @@ type Coordinate = {
const DZOOM = 0.1;
const MINZOOM = 0.1;

const EST_COMPRESSION_RATIO = 0.022;

// https://stackoverflow.com/questions/6234773/can-i-escape-html-special-chars-in-javascript
function escapeSVG(text: string) {
return text
Expand All @@ -77,6 +79,33 @@ function attrs(attributes: Record<string, string | number | null>) {
.join(' ');
}

function special_round(x: number) {
assert(x >= 0);
if (x === 0) {
return 0;
}
const p = Math.pow(10, Math.floor(Math.log10(x)));
// prettier-ignore
const candidates = [
Math.round(x / p) * p - p / 2,
Math.round(x / p) * p,
Math.round(x / p) * p + p / 2,
];
return Math.trunc(candidates.sort((a, b) => Math.abs(x - a) - Math.abs(x - b))[0]);
}

function size_to_human(bytes: number) {
if (bytes < 1000) {
return special_round(bytes) + ' B';
} else if (bytes < 1_000_000) {
return special_round(bytes / 1_000) + ' KB';
} else if (bytes < 1_000_000_000) {
return special_round(bytes / 1_000_000) + ' MB';
} else {
return special_round(bytes / 1_000_000_000) + ' GB';
}
}

export class Cfg extends Pane<CfgState> {
graphDiv: HTMLElement;
svg: SVGElement;
Expand All @@ -85,6 +114,7 @@ export class Cfg extends Pane<CfgState> {
graphElement: HTMLElement;
infoElement: HTMLElement;
exportPNGButton: JQuery;
estimatedPNGSize: Element;
exportSVGButton: JQuery;
currentPosition: Coordinate = {x: 0, y: 0};
dragging = false;
Expand Down Expand Up @@ -157,6 +187,7 @@ export class Cfg extends Pane<CfgState> {
this.graphElement = this.domRoot.find('.graph')[0];
this.infoElement = this.domRoot.find('.cfg-info')[0];
this.exportPNGButton = this.domRoot.find('.export-png').first();
this.estimatedPNGSize = unwrap(this.exportPNGButton[0].querySelector('.estimated-export-size'));
this.exportSVGButton = this.domRoot.find('.export-svg').first();
}

Expand Down Expand Up @@ -362,6 +393,7 @@ export class Cfg extends Pane<CfgState> {
async selectFunction(name: string | null) {
this.blockContainer.innerHTML = '';
this.svg.innerHTML = '';
this.estimatedPNGSize.innerHTML = '';
if (!name || !(name in this.results)) {
return;
}
Expand All @@ -374,6 +406,9 @@ export class Cfg extends Pane<CfgState> {
this.infoElement.innerHTML = `Layout time: ${Math.round(this.layout.layoutTime)}ms<br/>Basic blocks: ${
fn.nodes.length
}`;
this.estimatedPNGSize.innerHTML = `(~${size_to_human(
this.layout.getWidth() * this.layout.getHeight() * 4 * EST_COMPRESSION_RATIO
)})`;
}

zoom(zoom: number) {
Expand Down
7 changes: 7 additions & 0 deletions static/styles/explorer.scss
Expand Up @@ -423,6 +423,13 @@ pre.content.wrap * {
}
}

.cfg-toolbar {
.estimated-export-size {
font-size: x-small;
font-style: italic;
}
}

.clear-cache {
position: absolute;
right: 0;
Expand Down
1 change: 1 addition & 0 deletions views/templates/panes/cfg.pug
Expand Up @@ -10,6 +10,7 @@
button.btn.btn-sm.btn-light.dropdown-item.export-png(title="Export graph" aria-label="Export graph as an image")
span.fas.fa-image
span.hideable Export PNG
span.estimated-export-size
button.btn.btn-sm.btn-light.dropdown-item.export-svg(title="Export graph" aria-label="Export graph as an image")
span.fas.fa-vector-square
span.hideable Export SVG
Expand Down

0 comments on commit 742ee2f

Please sign in to comment.