a-h / templ

A language for writing HTML user interfaces in Go.
https://templ.guide/
MIT License
8.04k stars 264 forks source link

`templ generate` takes a long time for unformatted files #857

Closed MilkeeyCat closed 2 months ago

MilkeeyCat commented 2 months ago

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):

a-h commented 2 months ago

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 commented 2 months ago

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

joerdav commented 2 months ago

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 commented 2 months ago

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 commented 1 month ago

See #887

MilkeeyCat commented 1 month ago

Thanks a lot!