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

templ generate takes a long time for unformatted files #857

Closed
MilkeeyCat opened this issue Jul 22, 2024 · 6 comments
Closed

templ generate takes a long time for unformatted files #857

MilkeeyCat opened this issue Jul 22, 2024 · 6 comments

Comments

@MilkeeyCat
Copy link
Contributor

Try to generate code for this code

package test

templ TestThingy(){
    <div>
        <h3>Dummy text</h3>
        <p>
            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>

            <span>Dummy text</span>
            <br>
        </p>
    </div>
}

For me it takes 6.3s when it's formatted but after I run templ fmt on the file, it takes 4.2ms. It doesn't seem like a big difference but I have a while which takes 1min to generate xd but after I formatted it, it takes only 20ms.

templ info output
usage: templ [...]

templ - build HTML UIs with Go

See docs at https://templ.guide

commands:
generate Generates Go code from templ files
fmt Formats templ files
lsp Starts a language server for templ files
version Prints the version

Desktop (please complete the following information):

  • OS: Linux
  • templ CLI version v0.2.747
  • Go version 1.22.3 linux/amd64
@a-h
Copy link
Owner

a-h commented Jul 22, 2024

I'd guess it's taking a long time to parse the br tag. The parser currently reads to the end of the content to check if it ever gets closed.

If it gets closed and there's content in it, IIRC, the formatter leaves it alone. But if it's a void tag like br, and it's got no children, templ fmt converts it to a self closing tag which is much faster to parse.

But several seconds to do either seems like a long time!

@MilkeeyCat
Copy link
Contributor Author

MilkeeyCat commented Jul 22, 2024

Yep, when i change it to <br/> it works MUCH faster, it was my skill issue but thanks for the answer!

@joerdav
Copy link
Collaborator

joerdav commented Jul 23, 2024

There's possibly a way to speed this up by avoiding the rewinding and re-parsing of tags after unclosed tags.

Though it may be one of those that adds more parsing complexity than it's worth.

@a-h
Copy link
Owner

a-h commented Jul 23, 2024

It could be that as soon as we see a void open tag inside a void tag, eg br inside br, we should quit looking for the close for the first br.

a-h added a commit that referenced this issue Aug 17, 2024

Verified

This commit was signed with the committer’s verified signature.
a-h Adrian Hesketh
@a-h
Copy link
Owner

a-h commented Aug 17, 2024

See #887

a-h added a commit that referenced this issue Aug 18, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@MilkeeyCat
Copy link
Contributor Author

Thanks a lot!

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

3 participants