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

Performance improvements #204

Closed
wants to merge 2 commits into from
Closed

Conversation

iwsfg
Copy link
Contributor

@iwsfg iwsfg commented Nov 28, 2016

Hello.

I noticed that some methods waste time by doing unnecessary work like clonning newly created buffers or allocating buffers larger than needed and decided to fix that. So I changed that, added some shortcuts here and there and changed simpleRotate in a way so in doesnt have to iterate over every pixel more than once in for any angle supported.

Here are some numbers:

Jimp#rotate (simpleRotate)

  • Saves some time by not creating extra buffer.
  • Added special handling for 180 degree turn, rotation by 90/270 degrees always performed by the shortest route
// Rotate by 90 degrees
Jimp#rotate old x 230 ops/sec ±0.83% (189 runs sampled)
Jimp#rotate new x 257 ops/sec ±0.32% (196 runs sampled)

// Rotate by 180 degrees
Jimp#rotate old x 115 ops/sec ±0.48% (190 runs sampled)
Jimp#rotate new x 402 ops/sec ±0.17% (201 runs sampled)

// Rotate by 270 degrees
Jimp#rotate old x 77.28 ops/sec ±0.31% (176 runs sampled)
Jimp#rotate new x 253 ops/sec ±0.38% (196 runs sampled)

Jimp#flip

  • Saves some time by not creating extra buffer.
  • When flipping in both directions - uses simple rotation by 180 degree from above.
// Flip horizontally
Jimp#rotate old x 31.34 ops/sec ±1.27% (169 runs sampled)
Jimp#rotate new x 92.31 ops/sec ±0.39% (181 runs sampled)

// Flip vertically
Jimp#rotate old x 31.10 ops/sec ±1.34% (168 runs sampled)
Jimp#rotate new x 102 ops/sec ±0.74% (186 runs sampled)

// Flip horizontally and vertically
Jimp#rotate old x 23.95 ops/sec ±1.11% (158 runs sampled)
Jimp#rotate new x 396 ops/sec ±0.49% (198 runs sampled)

Jimp#crop

  • Allocates buffer of correct length.
  • When possible - uses Buffer#slice, so cropped image would uses same memory as old buffer, just with different offset,

(note: following numbers also include time of clonning test image in every run)

// Copy image and crop
Jimp#crop old x 252 ops/sec ±0.35% (193 runs sampled)
Jimp#crop new x 324 ops/sec ±0.76% (193 runs sampled)

// Copy image and crop without changing width
Jimp#crop old x 166 ops/sec ±0.63% (192 runs sampled)
Jimp#crop new x 1,199 ops/sec ±4.58% (179 runs sampled)

@iwsfg
Copy link
Contributor Author

iwsfg commented Dec 4, 2016

You may notice that code performing rotation by 90 degrees moved to a separate function. I had to do that, otherwise performance degraded seriously in benchmark after rotation by 180 degrees. Something is wrong with V8's optimizator, it seems. It was becoming even slower than old implementation, that performed 2 extra rotations

@edi9999
Copy link
Contributor

edi9999 commented Dec 25, 2017

That's really great work !

@edi9999 edi9999 changed the title Performance improvenets Performance improvements Jan 9, 2018
@hipstersmoothie hipstersmoothie added the bug there is a bug in the way jimp behaves label Aug 4, 2018
@hipstersmoothie
Copy link
Collaborator

closing in favor of #512

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug there is a bug in the way jimp behaves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants