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

proposal: html/template: serialize parsed templates #70580

Closed
theaino opened this issue Nov 26, 2024 · 5 comments
Closed

proposal: html/template: serialize parsed templates #70580

theaino opened this issue Nov 26, 2024 · 5 comments
Labels
Proposal WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@theaino
Copy link

theaino commented Nov 26, 2024

Proposal Details

I was trying to serialize already parsed templates. Apparently, this is extremely hard; you have to do some reflection magic because half of the fields are unexported. I think this should be supported natively.

If this already works, please tell me.

@gopherbot gopherbot added this to the Proposal milestone Nov 26, 2024
@seankhliao
Copy link
Member

What's the use case for doing this?

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Nov 26, 2024
@theaino
Copy link
Author

theaino commented Nov 26, 2024

I used wasm to render templates client-side. It would be faster if I already have the templates parsed on the server and just send the parsed tree to the client to execute the template.

@seankhliao
Copy link
Member

use text/template/parse and just call Template.AddParseTree on the client?

@apparentlymart
Copy link

apparentlymart commented Nov 26, 2024

Do you have a particular serialization format in mind for this?

One approach I could imagine is to support a JSON serialization of all of the types in text/template/parse, and then send the JSON form of the parse.Tree to the client. But the client-side code would still need to do substantial work to evaluate that AST and produce a rendered result, so I assume then you'd want to deserialize it back into a parse.Tree and then use template.AddParseTree to reuse the existing template renderer.

The main question this raises to me is whether this "optimization" would actually be productive in practice...

I would expect that a JSON serialization of an entire parse.Tree data structure would be significantly larger than the template source code it was created from, because the template source code is likely already the most compact possible serialization of the Go template language, or at least is pretty close to being that.

So this would be productive only if the increased size of the JSON serialization vs. the template source code is smaller than the cost of sending the template parser to the client as part of your wasm bundle, and if the extra data transfer time it implies is smaller than the time it would take to run the wasm-compiled version of the tree.Parse function client-side.

Have you done some measurements that convinced you that this optimization would be productive in practice?

(I have not measured anything, so I don't intend to claim it definitely isn't productive, but an argument in terms of performance is typically best motivated by timing data or other similar analysis.)

@seankhliao
Copy link
Member

I'm going to close this as already possible with AddParseTree, which only does a few checks before assigning to the Template's internal mapping. https://cs.opensource.google/go/go/+/master:src/text/template/template.go;l=126?q=AddParseTr&ss=go%2Fgo

Whether this is suitable for your use case is up for you to investigate.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Proposal WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants