Skip to content

Conversation

calvinmetcalf
Copy link
Contributor

pull for #1968 which adds compiled templates

@mourner
Copy link
Member

mourner commented Aug 13, 2013

Is this really a bottleneck to justify adding this type of complexity to the code?

@calvinmetcalf
Copy link
Contributor Author

threw together a perf it does seem to offer a pretty good speed up, might make sense to make it a bit more readable or remove the surrounding closure.

@mourner
Copy link
Member

mourner commented Aug 13, 2013

I'm sure it offers good relative speed up, but is the original performance a bottleneck in the first place?

Lets take for example full screen Macbook 15 Retina with 2880 x 1800 resolution and divide that by 256x256 — we're getting about 80 tiles per one screen. Now we look at the benchmark above — Leaflet template gets 23,631 operations (20 template calls) per second, that means that templating one full screen load of 80 tiles is performed in 80 * 1000 ms / (23,631 * 20) = 0.16 milliseconds.

0.16 ms per full Retina screen. Or 100 full screens (8000 tiles) per one frame of 16 ms. Is this slow enough to even bother optimizing? Now we see why people say "Premature optimization is the root of all evil".

@calvinmetcalf
Copy link
Contributor Author

so my phone can do 300 to 600 operations per second, we'll round down to 300 (we can assume i have otherfor things going on) and my phone has 45 tiles displayed at once (5 across and 7 down, counted ). That's 7.5 ms or 1 full frame if i have an overlay tileset too.

@calvinmetcalf
Copy link
Contributor Author

i did the math wrong thats only 35 tiles so overlay and scrolling

@calvinmetcalf
Copy link
Contributor Author

I think the main difference this would make would be in situation with slow processors not necessarily situation with high resolution

@mourner
Copy link
Member

mourner commented Aug 14, 2013

OK, I'll think about this. Thanks Calvin!

@mourner
Copy link
Member

mourner commented Aug 16, 2013

Some more thoughts:

  1. Since the template function is mostly used for URLs, we can assume that there are no line breaks in the passed string, and remove all that monstrous code with line break symbols escaping.
  2. L.Util.template signature should be left the same so that existing plugins that rely on it don't break. We can create a cash with template strings as keys so that we don't have to change the signature while getting all the benefits.

@calvinmetcalf
Copy link
Contributor Author

cleaned up the regex also changed the expression that searched for the {variables} to be the same as the old one, switched that method name to be compileTemplate from the real awkward makeInterpolater. I set TileLayer.js to use this method. I then set template to look in L.Util.templateCache for a template before making a new one.

@mourner
Copy link
Member

mourner commented Aug 25, 2013

This looks much better. Lets also cover this with some tests, and I'll merge once I'm back from vacation.

@calvinmetcalf
Copy link
Contributor Author

I just saw you were back from vacation, I can throw some tests together latter today.

@mourner
Copy link
Member

mourner commented Aug 27, 2013

Yep!

@calvinmetcalf
Copy link
Contributor Author

ok added tests for the template cache and the template function and made the current template test sneakier, also rebased into one commit

@mourner
Copy link
Member

mourner commented Aug 27, 2013

Awesome! One last bit: I think that as L.Util.tempalte signature remains the same, we can ditch all TileLayer.js changes since it will still hit the cache and benefit from performance gains (and cache lookup certainly doesn't have a noticeable performance hit), while the code gets a bit simpler.

@ghost ghost assigned mourner Aug 27, 2013
@@ -104,17 +104,24 @@ L.Util = {
}
return ((!existingUrl || existingUrl.indexOf('?') === -1) ? '?' : '&') + params.join('&');
},

Copy link
Member

Choose a reason for hiding this comment

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

don't remove empty lines, they're good for readability :)

@calvinmetcalf
Copy link
Contributor Author

ok switched it to give the data to the compile function

mourner added a commit that referenced this pull request Aug 27, 2013
@mourner mourner merged commit b2b25c8 into Leaflet:master Aug 27, 2013
@mourner
Copy link
Member

mourner commented Aug 27, 2013

Thanks Calvin, merged and clean up a bit to be simpler and more consistent with other Leaflet code. Notice how the huge initial pull (before the discussion) got shrinked to just a few simple lines of code. :)

@calvinmetcalf calvinmetcalf deleted the interpolation branch August 27, 2013 19:00
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.

2 participants