generated from napi-rs/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: adjust Node.js external memory while creating Canvas
- Loading branch information
1 parent
2a4c900
commit 77ecc52
Showing
12 changed files
with
175 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { join } from 'node:path' | ||
import { createRequire } from 'node:module' | ||
import { setTimeout } from 'node:timers/promises' | ||
|
||
import { whiteBright, red, green, gray } from 'colorette' | ||
import prettyBytes from 'pretty-bytes' | ||
import { table } from 'table' | ||
|
||
import { createCanvas, Path2D, clearAllCache } from '../index.js' | ||
|
||
function paint() { | ||
const require = createRequire(import.meta.url) | ||
const tiger = require('../example/tiger.json') | ||
const canvas = createCanvas(6016, 3384) | ||
const ctx = canvas.getContext('2d') | ||
for (const pathObject of tiger) { | ||
const p = new Path2D(pathObject.d) | ||
ctx.fillStyle = pathObject.fillStyle | ||
ctx.strokeStyle = pathObject.strokeStyle | ||
if (pathObject.lineWidth) { | ||
ctx.lineWidth = parseInt(pathObject.lineWidth, 10) | ||
} | ||
ctx.stroke(p) | ||
ctx.fill(p) | ||
} | ||
} | ||
|
||
const initial = process.memoryUsage() | ||
|
||
async function main() { | ||
for (const [index, _] of Array.from({ length: 100 }).entries()) { | ||
displayMemoryUsageFromNode(initial) | ||
await setTimeout(100) | ||
global?.gc?.() | ||
await paint() | ||
} | ||
} | ||
|
||
main().then(() => { | ||
displayMemoryUsageFromNode(initial) | ||
clearAllCache() | ||
global?.gc?.() | ||
setInterval(() => { | ||
displayMemoryUsageFromNode(initial) | ||
}, 2000) | ||
}) | ||
|
||
function displayMemoryUsageFromNode(initialMemoryUsage) { | ||
const finalMemoryUsage = process.memoryUsage() | ||
const titles = Object.keys(initialMemoryUsage).map((k) => whiteBright(k)) | ||
const tableData = [titles] | ||
const diffColumn = [] | ||
for (const [key, value] of Object.entries(initialMemoryUsage)) { | ||
const diff = finalMemoryUsage[key] - value | ||
const prettyDiff = prettyBytes(diff, { signed: true }) | ||
if (diff > 0) { | ||
diffColumn.push(red(prettyDiff)) | ||
} else if (diff < 0) { | ||
diffColumn.push(green(prettyDiff)) | ||
} else { | ||
diffColumn.push(gray(prettyDiff)) | ||
} | ||
} | ||
tableData.push(diffColumn) | ||
console.info(table(tableData)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -417,3 +417,8 @@ impl SVGCanvas { | |
} | ||
} | ||
} | ||
|
||
#[napi] | ||
pub fn clear_all_cache() { | ||
unsafe { sk::ffi::skiac_clear_all_cache() }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77ecc52
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
Draw house#skia-canvas
20.1
ops/sec (±0.55%
)21
ops/sec (±1.67%
)1.04
Draw house#node-canvas
17.8
ops/sec (±0.2%
)17
ops/sec (±1.86%
)0.96
Draw house#@napi-rs/skia
17.5
ops/sec (±1.85%
)18
ops/sec (±0.53%
)1.03
Draw gradient#skia-canvas
19.3
ops/sec (±0.48%
)21
ops/sec (±0.41%
)1.09
Draw gradient#node-canvas
17
ops/sec (±0.24%
)17
ops/sec (±0.24%
)1
Draw gradient#@napi-rs/skia
16.7
ops/sec (±1.07%
)18
ops/sec (±0.2%
)1.08
This comment was automatically generated by workflow using github-action-benchmark.