sharkdp / pastel

A command-line tool to generate, analyze, convert and manipulate colors
Apache License 2.0
4.98k stars 97 forks source link

Performance issue with the redundancy operations #163

Closed yyzdtccjdtc closed 2 years ago

yyzdtccjdtc commented 2 years ago

https://github.com/sharkdp/pastel/blob/master/src/distinct.rs#L329-L324

For line 329 here, the &colors[color] will keep loading the same value from the same memory location in this for loop, because the index color will not change. If we declare a new variable at_color outside of this for loop and reference it into to the self.distance() function then it can save a lot of memory operations because this at_color will be put into register by the compiler. According to my test with generate 4000 distinguished colors, it will get a 5.4% speedup.

https://github.com/sharkdp/pastel/blob/master/src/distinct.rs#L168

For line 168 here, every element in the new_colors vector is a tuple (Color, Lab), but for the result.update() function, it will only use the Lab elements, so there's some waste memory to copy and update the color. And if it only pass a vector of Lab, then there will be a 13.4% speed up with the previous optimization together.

Here is the modified code:

distinct-modified.txt

Hope these information helps!

sharkdp commented 2 years ago

Awesome, thank you very much! If you are okay with using your code under pastels LICENSE, it would be great if someone could open a PR with the necessary changes.