-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Optimize _clipImageData #4200
Optimize _clipImageData #4200
Conversation
This makes clip image data around twice as fast, it's a little tricky to measure it though. Changes: - Work on Uint32Array instead of Uint8 so it's 1 assignment per pixel instead of 1 per channel. - Perform the clipping in-place using the original image data to avoid allocation a new buffer. Fixes xtermjs#4197
👍 Nice. The GC pressure should also be much lower, but note that most of the runtime in your original post came from GC itself, prolly meaning that the GC books already were pretty full. Thus there is a high chance, that the additional objs from that method only "brought the barrel to overflow" (german saying) forcing the GC into serious cleanup. |
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.
A few more notes on code...
clippedData[newOffset + 1] = imageData.data[oldOffset + 1]; | ||
clippedData[newOffset + 2] = imageData.data[oldOffset + 2]; | ||
clippedData[newOffset + 3] = imageData.data[oldOffset + 3]; | ||
const clippedData = new Uint32Array(imageData.data.buffer, 0, width * height); |
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.
Oh well - inplace overwriting is abit dangerous, if the overlapping from rewrites is not safe (guess its ok here?)
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.
It's safe as the data is read from low to high and the writing is always the same as writing (rare) or lower
} | ||
} | ||
return new ImageData(clippedData, width, height); | ||
return new ImageData(new Uint8ClampedArray(clippedData.buffer, clippedData.byteOffset, clippedData.byteLength), width, height); |
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.
Not sure if it is worth the trouble - since you already do inplace overwriting above, the new ImageData maybe can also be avoided?
Which raises the question - is the _clipImageData
method needed at all? As far as I can see it just transfers a rectangular subarea over. Isnt it faster to simply take the orginal ImageData and apply the box offsets during drawImage
? But maybe I am overlooking something...
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.
You're right! We can remove this outright and just use boundingBox in drawImage. I'm guessing the reason it's as it is currently is because I just overlooked that or wasn't thinking drawImage had a this signature source+dest signature.
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.
Was just doing this, we can't actually use drawImage as it doesn't accept ImageData, looks like we can still use putImageData though.
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.
🎉 #4201
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.
Oh right, was mixing the methods up (I use putImageData myself, lol) 😊
Though the question remains, if thats actually faster, or just creates runtime noise at different ends...
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.
Surely it's faster, it's essentially doing what was being done manually before natively
This makes clip image data around twice as fast, it's a little tricky to measure it though. Changes:
Fixes #4197