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

Skip running color match regex, if not required #186

Merged
merged 4 commits into from
May 26, 2023

Conversation

pramilk
Copy link
Contributor

@pramilk pramilk commented May 22, 2023

Color Parsing is in a critial hot path and is executed multiple times while style parsing. This PR brings in a small optimization.

Before running a expensive color match regex run a cheap validation. Tests shows a 98% improvement if color is specified in HSL format. There will be some improvement when color is specified in rgb format as well.
Test: https://jsbench.me/4blhzc953l/1

image

Launch Checklist

  • Confirm your changes do not include backports from Mapbox projects (unless with compliant license) - if you are not sure about this, please ask!
  • Briefly describe the changes in this PR.
  • Link to related issues.
  • Include before/after visuals or gifs if this PR includes visual changes.
  • Write tests for all new functionality.
  • Document any changes to public APIs.
  • Post benchmark scores.
  • Add an entry to CHANGELOG.md under the ## main section.

@wipfli
Copy link
Contributor

wipfli commented May 23, 2023

Shouldn't CI run automatically for people who are not first-time contributors?
image

@HarelM
Copy link
Collaborator

HarelM commented May 23, 2023

Probably a first time in this repo.
But I think the setting are too strict for my taste (both in this repo, and in gl-js).
I would prefer that if the github user is not a new user the CI will run, and not only if the user had contributed in the past...

@HarelM
Copy link
Collaborator

HarelM commented May 23, 2023

I think package-lock.json needs to be updated as well.

@pramilk
Copy link
Contributor Author

pramilk commented May 23, 2023

I think package-lock.json needs to be updated as well.

Sorry forgot about it. Updated.

@pramilk
Copy link
Contributor Author

pramilk commented May 26, 2023

@HarelM, Can you please take a look.

@HarelM HarelM merged commit f1a5661 into maplibre:main May 26, 2023
@kajkal
Copy link
Contributor

kajkal commented May 28, 2023

I stumbled across this change and it intrigued me.
I decided to make my own benchmark because I think the one attached to this PR is misleading by showing only hsl().

https://jsbench.me/mdli7ehoen/1
jsbench me_mdli7ehoen_1
From my tests, it appears that, assuming hsl() is not the only format, and # and rgb are also used in similar proportions, this change degrades color parsing performance (I checked in chrome and firefox).

It seems that better performance boost would be if you simply put the regexp declarations in a higher scope. But in any case, I don't think the current color parsing performance is a big problem that critically needs to be optimized.

@HarelM
Copy link
Collaborator

HarelM commented May 28, 2023

Ahh interesting! :-)
Would be interesting to see what happens when most of the input is rgb or hex as I think they are more widely used, maybe along color names, IDK...
@pramilk any comments on this new findings?

@pramilk
Copy link
Contributor Author

pramilk commented May 28, 2023

I had tested moving regex to higher scope and it did no showed much difference before making this change.

Here is updated test with Regex moved to setup

Testing for Hex and RGB
https://jsbench.me/4blhzc953l/2
image

Testing for just Hex
https://jsbench.me/4blhzc953l/3
image

Intent of the original test(https://jsbench.me/4blhzc953l/1) was to see if adding a cheap startswith validation will be much faster then a full regex match without any additional code. In Bing maps road style we have 158 #hex and 201 HSL. Maplibre default style has 94 #hex, 79 RGB and 11 HSL.

Testing with mixed color format, dilutes the test and does not test the change that was intended. Like having hex color will force test to also run its regex. Intend was to simply have a cheap way to test the color before running regex.

@kajkal
Copy link
Contributor

kajkal commented May 29, 2023

I compared the entire parsing functions (had to minify code as it is at the jsbench limits):

https://jsbench.me/mdli7ehoen/2
jsbench me_mdli7ehoen_2

The results made me think why my earlier test would suggest the opposite. And so I noticed that I added a for of loop for code that was intended to be in the body of a function and the early return spoiled the test completely 😩

So yes, I was wrong and this change indeed improves color parsing performance. Sorry for claiming otherwise 😔
And according to the test above regexps in a higher scope only slightly improves performance.

As a final note, I would like to add that I disagree with the statement that testing with mixed formats dilutes the test, in my opinion it is more valuable because it verifies the additional cost for the hex/rgb() paths, instead of focusing only on the benefits for the hsl() path.

@pramilk pramilk deleted the skip_rgbColor_regex branch May 30, 2023 17:26
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.

5 participants