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

text/template: optimize Clone+Funcs+Execute sequence #38114

Open
rsc opened this issue Mar 27, 2020 · 4 comments
Open

text/template: optimize Clone+Funcs+Execute sequence #38114

rsc opened this issue Mar 27, 2020 · 4 comments

Comments

@rsc
Copy link
Contributor

@rsc rsc commented Mar 27, 2020

In #31107 it came up that maybe most of the requested new functionality would be doable outside the package if it were possible to install new functions on a per-execution basis. One option is to add a new Execute that takes a second FuncMap, but I think we should consider not adding new API and instead make Clone+Funcs+Execute run faster.

It might be that all that's needed is making Clone make the actual template backing data copy-on-write. Then Clone is cheap. Then Funcs would copy only the map, or maybe even start a second map, also cheap. And Execute would be unchanged.

This issue is to think about whether this is possible to do without visible semantic changes, and if so, to do it.

/cc @bep @robpike @empijei

@empijei
Copy link
Contributor

@empijei empijei commented Mar 27, 2020

Thanks for opening this. It would indeed address my need to seamlessly provide a per-execution nonce on the framework side.

Both adding other functions and cloning would work.

@andybons andybons added this to the Unplanned milestone Mar 27, 2020
@bep
Copy link
Contributor

@bep bep commented Mar 27, 2020

I assume you still would need to synchronize the clone? What is the cost compared to doing it the proper way (adding the funcs to the execution struct where they belong)?

@empijei
Copy link
Contributor

@empijei empijei commented Mar 29, 2020

What is the cost

This could be a lazy copy so potentially very little. It would just mark the template as being in use by more than one user and thus turning on a copy-on-write flag. I am similarly worried about this but Russ said this could be done with very little overhead.

doing it the proper way

Are you considering as an alternative to add a FuncMap to the Execute call? I would personally prefer it too but it would require an API change.

If your concern is about performance, would you be ok with expressing this semantic as Clone+Funcs+Execute (which has the perk of not changing the API) if it had a similar runtime cost?

@empijei
Copy link
Contributor

@empijei empijei commented May 2, 2020

@rsc I am still trying to wrap my head around this.

How could this be acceptably efficient to clone the templates every time we need to render them?

Most of our servers require to render the same template tens of times with different data and different nonces at the same time.

Can you please share a bit of technical details on what you had in mind to implement this so I can maybe start taking a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.