Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
text/template: optimize Clone+Funcs+Execute sequence #38114
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.
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.
Are you considering as an alternative to add a
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?
@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?