-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize accessing PNG dimensions (#600)
I've noticed that a lot of the parsing is spent figuring out the dimensions of our icons, which very often are PNG images. Apparently to access the full header of a PNG file a lot of decoding needs to happen, which is apparently quite expensive. However we don't really need to access the full header and instead really only the width and height. And here the PNG specification comes in handy: https://www.w3.org/TR/2003/REC-PNG-20031110/ It says the following: > A valid PNG datastream shall begin with a PNG signature, immediately > followed by an IHDR chunk Each chunk is encoded as a length and type and then its chunk encoding. An IHDR chunk immediately starts with the width and height. This means we can model the beginning of a PNG file as follows: ```rust // 5.2 PNG signature png_signature: [u8; 8], // 5.3 Chunk layout chunk_len: [u8; 4], // 11.2.2 IHDR Image header chunk_type: [u8; 4], width: U32, height: U32, ``` And with a little bit of bytemuck magic we essentially can access the width and height with a single read. This improves parsing speed of entire splits files by up to 30%. Yes, the old code was that slow.
- Loading branch information
Showing
2 changed files
with
68 additions
and
30 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