From 3777c46dad9dcb1ed99a82d2ac2773547cb207c0 Mon Sep 17 00:00:00 2001 From: WofWca Date: Sun, 24 Oct 2021 13:33:41 +0800 Subject: [PATCH] perf: Revert "perf: remove unnecessary `context.save()`& `context.restore()`" This reverts commit f7b488eb545cfb307968bf9fbe20cfd1eb031f18. For some reason in Chromium 95.0.4638.54 (and maybe others) that change caused `render()` CPU time to grow over time. For me on the `examples/example1.html` page it would grow from 0.5ms to 50ms over 5 minutes --- smoothie.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/smoothie.js b/smoothie.js index 82e4469..071609b 100644 --- a/smoothie.js +++ b/smoothie.js @@ -859,6 +859,10 @@ context.font = chartOptions.labels.fontSize + 'px ' + chartOptions.labels.fontFamily; + // Save the state of the canvas context, any transformations applied in this method + // will get removed from the stack at the end of this method when .restore() is called. + context.save(); + // Move the origin. context.translate(dimensions.left, dimensions.top); @@ -870,11 +874,14 @@ context.clip(); // Clear the working area. + context.save(); context.fillStyle = chartOptions.grid.fillStyle; context.clearRect(0, 0, dimensions.width, dimensions.height); context.fillRect(0, 0, dimensions.width, dimensions.height); + context.restore(); // Grid lines... + context.save(); context.lineWidth = chartOptions.grid.lineWidth; context.strokeStyle = chartOptions.grid.strokeStyle; // Vertical (time) dividers. @@ -912,6 +919,7 @@ context.strokeRect(0, 0, dimensions.width, dimensions.height); context.closePath(); } + context.restore(); // Draw any horizontal lines... if (chartOptions.horizontalLines && chartOptions.horizontalLines.length) { @@ -938,6 +946,7 @@ if (dataSet.length <= 1 || timeSeries.disabled) { continue; } + context.save(); var seriesOptions = this.seriesSet[d].options; @@ -1019,6 +1028,8 @@ context.strokeStyle = seriesOptions.strokeStyle; context.stroke(); } + + context.restore(); } if (chartOptions.tooltip && this.mouseX >= 0) { @@ -1116,6 +1127,8 @@ context.fillStyle = chartOptions.title.fillStyle; context.fillText(chartOptions.title.text, titleXPos, titleYPos); } + + context.restore(); // See .save() above. }; // Sample timestamp formatting function