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

Support for relative paths introduces cache performance bug #378

Closed
boutell opened this issue Feb 21, 2015 · 18 comments
Closed

Support for relative paths introduces cache performance bug #378

boutell opened this issue Feb 21, 2015 · 18 comments

Comments

@boutell
Copy link

boutell commented Feb 21, 2015

See #349 cc: @SamyPesse

With the new support for relative paths in folder names, the same "name" can refer to two completely different templates.

The cache storage code does in fact address this, storing the cached template under its resolved name.

However the cache fetch code runs before the resolve call.

The correct resolution would be to put the resolve call before the cache fetch code. However there is a wrinkle: nunjucks permits multiple loaders at once. And the "resolve" call currently is not responsible for saying "this is the correct resolution and I'm sure this file actually exists and I'm taking responsibility for it, so stop looking for other loaders."

I think the "resolve" API needs to be tried for each configured loader, and must become responsible for not returning a happy result unless the file at the resolved path actually exists.

@SamyPesse
Copy link
Contributor

I'm taking a look at the issue, I'll add some unit tests and propose a fix.

@boutell
Copy link
Author

boutell commented Feb 21, 2015

I'm working on unit tests now, if I can get my install --dev to complete...

On Sat, Feb 21, 2015 at 11:01 AM, Samy Pessé notifications@github.com
wrote:

I'm taking a look at the issue, I'll add some unit tests and propose a fix.


Reply to this email directly or view it on GitHub
#378 (comment).

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

@boutell
Copy link
Author

boutell commented Feb 21, 2015

Hmm, my test passed. Is the cache always active?

On Sat, Feb 21, 2015 at 11:04 AM, Tom Boutell tom@punkave.com wrote:

I'm working on unit tests now, if I can get my install --dev to complete...

On Sat, Feb 21, 2015 at 11:01 AM, Samy Pessé notifications@github.com
wrote:

I'm taking a look at the issue, I'll add some unit tests and propose a
fix.


Reply to this email directly or view it on GitHub
#378 (comment).

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

@SamyPesse
Copy link
Contributor

I added a test but it's passing: https://github.com/SamyPesse/nunjucks/commit/af62c97713e6d81628a4c3db581f041116d26ea4

Let me know if the test is correctly covering the issue.

@SamyPesse
Copy link
Contributor

Same for me ^^ I'm taking a look at why.

@boutell
Copy link
Author

boutell commented Feb 21, 2015

Very interesting. I'm going to try logging a bit.

On Sat, Feb 21, 2015 at 11:08 AM, Samy Pessé notifications@github.com
wrote:

Same for me ^^ I'm taking a look at why.


Reply to this email directly or view it on GitHub
#378 (comment).

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

@boutell
Copy link
Author

boutell commented Feb 21, 2015

(We wrote almost identical tests.)

On Sat, Feb 21, 2015 at 11:08 AM, Tom Boutell tom@punkave.com wrote:

Very interesting. I'm going to try logging a bit.

On Sat, Feb 21, 2015 at 11:08 AM, Samy Pessé notifications@github.com
wrote:

Same for me ^^ I'm taking a look at why.


Reply to this email directly or view it on GitHub
#378 (comment).

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

@SamyPesse
Copy link
Contributor

It looks like the cache key is the correct absolue path (like it should be):

screen shot 2015-02-21 at 17 10 30

@boutell
Copy link
Author

boutell commented Feb 21, 2015

You're right, it is, however the test for a cache match is done before the
resolve call. So it will never succeed for a template included via a
relative path.

So this is actually a cache performance bug, not a wrong-result bug.

I will retitle the bug.

On Sat, Feb 21, 2015 at 11:11 AM, Samy Pessé notifications@github.com
wrote:

It looks like the cache key is the correct absolue path (like it should
be):

[image: screen shot 2015-02-21 at 17 10 30]
https://cloud.githubusercontent.com/assets/845425/6314784/98427208-b9ec-11e4-82b3-0f0171950a31.png


Reply to this email directly or view it on GitHub
#378 (comment).

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

@boutell boutell changed the title Support for relative paths has caching bug, does not work properly Support for relative paths introduces cache performance bug Feb 21, 2015
@boutell
Copy link
Author

boutell commented Feb 21, 2015

I have updated the issue's description to cover the cache performance issue correctly and suggested a solution that involves invoking resolve for each loader and stopping if one of the loaders claims responsibility. A loader should not do so unless the template actually exists.

I think this requires making resolve async-capable (but not async-mandatory, since synchronous template support is still important to many, including my coworkers and I (: )

@SamyPesse
Copy link
Contributor

@boutell
Copy link
Author

boutell commented Feb 21, 2015

Hmm.

That would certainly work for us. I think it would be a little safer in the
general case if we knew parentLoader as well as parentName, removing any
ambiguity as to who should be doing the resolving. But that's a big ask.

On Sat, Feb 21, 2015 at 11:12 AM, Tom Boutell tom@punkave.com wrote:

You're right, it is, however the test for a cache match is done before the
resolve call. So it will never succeed for a template included via a
relative path.

So this is actually a cache performance bug, not a wrong-result bug.

I will retitle the bug.

On Sat, Feb 21, 2015 at 11:11 AM, Samy Pessé notifications@github.com
wrote:

It looks like the cache key is the correct absolue path (like it should
be):

[image: screen shot 2015-02-21 at 17 10 30]
https://cloud.githubusercontent.com/assets/845425/6314784/98427208-b9ec-11e4-82b3-0f0171950a31.png


Reply to this email directly or view it on GitHub
#378 (comment).

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

@boutell
Copy link
Author

boutell commented Feb 21, 2015

Just to clarify, my concern is that right now, loaders are a straightforward filter pattern: the first loader to claim a template by actually returning a happy result wins. There's no ambiguity.

When we call "resolve" on all the loaders prior to that point, we introduce an ambiguity. Any loader might think it knows how to resolve that. They may have different ideas of how to resolve it. And since it's synchronous, they can't really check to be sure it's really going to work when you ask them for the source code (at least not in the general case).

So you could get a resolve() result from loader 1 when in reality only loader 2 was capable of loading the template.

@SamyPesse
Copy link
Contributor

One solution could be to prepend an id of the loader to the cache key.

For example, the cache key become: indexOfTheLoaderInTheList + ":" + resolvedName

@boutell
Copy link
Author

boutell commented Feb 21, 2015

Oh right, we can do that, can't we. It's the loader that issues "update"
though, and it doesn't know its index, not yet anyway.

On Sat, Feb 21, 2015 at 11:51 AM, Samy Pessé notifications@github.com
wrote:

One solution could be to prepend an id of the loader to the cache key.

For examle, the cache key become: indexOfTheLoaderInTheList + ":" + name


Reply to this email directly or view it on GitHub
#378 (comment).

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

@SamyPesse
Copy link
Contributor

Here is what I believe is a better solution: https://github.com/SamyPesse/nunjucks/commit/17bc3f0b55241636a33faba7767f45a94fa02275

I just moved the cache to the loader. Let me know what you think.

@boutell
Copy link
Author

boutell commented Feb 21, 2015

Perfect! I was going to suggest moving responsibility for caching into the
loaders, but this is nicer because it doesn't clutter up the loader
implementation.

On Sat, Feb 21, 2015 at 12:13 PM, Samy Pessé notifications@github.com
wrote:

Here is what I believe is a better solution: SamyPesse@17bc3f0
https://github.com/SamyPesse/nunjucks/commit/17bc3f0b55241636a33faba7767f45a94fa02275

I just moved the cache to the loader. Let me know what you think.


Reply to this email directly or view it on GitHub
#378 (comment).

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

@boutell
Copy link
Author

boutell commented Feb 21, 2015

(Really appreciate all your work on this!)

On Sat, Feb 21, 2015 at 12:35 PM, Tom Boutell tom@punkave.com wrote:

Perfect! I was going to suggest moving responsibility for caching into the
loaders, but this is nicer because it doesn't clutter up the loader
implementation.

On Sat, Feb 21, 2015 at 12:13 PM, Samy Pessé notifications@github.com
wrote:

Here is what I believe is a better solution: SamyPesse@17bc3f0
https://github.com/SamyPesse/nunjucks/commit/17bc3f0b55241636a33faba7767f45a94fa02275

I just moved the cache to the loader. Let me know what you think.


Reply to this email directly or view it on GitHub
#378 (comment).

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

*THOMAS BOUTELL, *DEV & OPS
P'UNK AVENUE | (215) 755-1330 | punkave.com

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

No branches or pull requests

2 participants