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

Add specialized matrix transform for Vector3 and Matrix3x3 #585

Merged
merged 5 commits into from
Aug 23, 2024

Conversation

lloydk
Copy link
Collaborator

@lloydk lloydk commented Aug 13, 2024

Adds a specialized matrix multiplication function (transform) that transforms an array of length 3 with a 3x3 matrix. The specialized function can also transform the array in place which can eliminate one array allocation.

The https://github.com/texel-org/color color library has a benchmark comparing itself to Color.js.

Profiling the conversion benchmark showed that matrix multiplication was one of the areas that was taking the most amount of time.

This PR speeds up the conversion benchmark by more than 3x and some of the other benchmarks by 2-3x.

Benchmark numbers before this PR:

conversion (Colorjs.io procedural API) --
Colorjs.io: 6213.74 ms
Ours: 733.75 ms
Speedup: 8.5x faster

conversion (Colorjs.io main API) --
Colorjs.io: 25444.47 ms
Ours: 703.50 ms
Speedup: 36.2x faster

gamut mapping OKLCH - sRGB (Colorjs.io procedural API) -- Colorjs.io: 13302.80 ms
Ours: 72.97 ms
Speedup: 182.3x faster

gamut mapping OKLCH - sRGB (Colorjs.io main API) -- Colorjs.io: 14900.11 ms
Ours: 67.78 ms
Speedup: 219.8x faster

gamut mapping all spaces to P3 (Colorjs.io procedural API) -- Colorjs.io: 10878.26 ms
Ours: 201.85 ms
Speedup: 53.9x faster

gamut mapping all spaces to P3 (Colorjs.io main API) -- Colorjs.io: 13731.21 ms
Ours: 201.76 ms
Speedup: 68.1x faster

After this PR:

conversion (Colorjs.io procedural API) --
Colorjs.io: 1793.97 ms
Ours: 713.03 ms
Speedup: 2.5x faster

conversion (Colorjs.io main API) --
Colorjs.io: 19884.18 ms
Ours: 704.59 ms
Speedup: 28.2x faster

gamut mapping OKLCH - sRGB (Colorjs.io procedural API) -- Colorjs.io: 4981.04 ms
Ours: 74.01 ms
Speedup: 67.3x faster

gamut mapping OKLCH - sRGB (Colorjs.io main API) -- Colorjs.io: 6477.12 ms
Ours: 68.02 ms
Speedup: 95.2x faster

gamut mapping all spaces to P3 (Colorjs.io procedural API) -- Colorjs.io: 4205.36 ms
Ours: 202.27 ms
Speedup: 20.8x faster

gamut mapping all spaces to P3 (Colorjs.io main API) -- Colorjs.io: 6137.99 ms
Ours: 199.91 ms
Speedup: 30.7x faster

There are still some color spaces that haven't been switched over to use the transform function. Assuming we want to proceed with this PR I'll create a separate PR to deal with the remaining spaces.

Adds a specialized matrix multiplication function that
transforms an array of length 3 with a 3x3 matrix. The
specialized function can also transform the array in place
which can eliminate one array allocation.

The https://github.com/texel-org/color color library
has a benchmark comparing itself to Color.js.

Profiling the conversion benchmark showed that matrix
multiplication was one of the areas that was taking
the most amount of time.

This PR speeds up the conversion benchmark by more
than 3x and some of the other benchmarks by 2-3x

Benchmark numbers before this PR:
---------------------------------

conversion (Colorjs.io procedural API) --
Colorjs.io: 6213.74 ms
Ours: 733.75 ms
Speedup: 8.5x faster

conversion (Colorjs.io main API) --
Colorjs.io: 25444.47 ms
Ours: 703.50 ms
Speedup: 36.2x faster

gamut mapping OKLCH - sRGB (Colorjs.io procedural API) --
Colorjs.io: 13302.80 ms
Ours: 72.97 ms
Speedup: 182.3x faster

gamut mapping OKLCH - sRGB (Colorjs.io main API) --
Colorjs.io: 14900.11 ms
Ours: 67.78 ms
Speedup: 219.8x faster

gamut mapping all spaces to P3 (Colorjs.io procedural API) --
Colorjs.io: 10878.26 ms
Ours: 201.85 ms
Speedup: 53.9x faster

gamut mapping all spaces to P3 (Colorjs.io main API) --
Colorjs.io: 13731.21 ms
Ours: 201.76 ms
Speedup: 68.1x faster

After this PR:
--------------

conversion (Colorjs.io procedural API) --
Colorjs.io: 1793.97 ms
Ours: 713.03 ms
Speedup: 2.5x faster

conversion (Colorjs.io main API) --
Colorjs.io: 19884.18 ms
Ours: 704.59 ms
Speedup: 28.2x faster

gamut mapping OKLCH - sRGB (Colorjs.io procedural API) --
Colorjs.io: 4981.04 ms
Ours: 74.01 ms
Speedup: 67.3x faster

gamut mapping OKLCH - sRGB (Colorjs.io main API) --
Colorjs.io: 6477.12 ms
Ours: 68.02 ms
Speedup: 95.2x faster

gamut mapping all spaces to P3 (Colorjs.io procedural API) --
Colorjs.io: 4205.36 ms
Ours: 202.27 ms
Speedup: 20.8x faster

gamut mapping all spaces to P3 (Colorjs.io main API) --
Colorjs.io: 6137.99 ms
Ours: 199.91 ms
Speedup: 30.7x faster
Copy link

netlify bot commented Aug 13, 2024

Deploy Preview for colorjs ready!

Name Link
🔨 Latest commit 8e39fdf
🔍 Latest deploy log https://app.netlify.com/sites/colorjs/deploys/66c8a46f08104d0007d6da83
😎 Deploy Preview https://deploy-preview-585--colorjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Collaborator

@facelessuser facelessuser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Copy link
Member

@LeaVerou LeaVerou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! LGTM, one nit is that the name transform() seems a bit too generic.

@lloydk
Copy link
Collaborator Author

lloydk commented Aug 22, 2024

Thanks for working on this! LGTM, one nit is that the name transform() seems a bit too generic.

I agree but wasn't sure what to call it, I was hoping someone would suggest a better name 😃

@LeaVerou
Copy link
Member

LeaVerou commented Aug 22, 2024

Thanks for working on this! LGTM, one nit is that the name transform() seems a bit too generic.

I agree but wasn't sure what to call it, I was hoping someone would suggest a better name 😃

It multiplies a vector of length 3 with a 3x3 matrix, right? What about multiply3() or multiply_3x3() or multiply_v3_m3x3() or multiply_3_3x3()?

@lloydk lloydk merged commit 6332f3a into color-js:main Aug 23, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants