• Didier Spezia's avatar
    html/template: fix quadratic performance with special tags · f4e3e5ea
    Didier Spezia authored
    The current implementation of the tSpecialTagEnd function
    is inefficient since it generates plenty of memory allocations
    and converts the whole buffer to lowercase at each call.
    
    If the number of special tags increases linearly with the
    template size, the complexity becomes quadratic.
    
    This CL provides an alternative implementation.
    While the algorithm is probably still not optimal, it avoids
    the quadratic behavior and the memory allocations.
    
    benchmark                          old ns/op     new ns/op     delta
    BenchmarkTemplateSpecialTags-4     19326431      532190        -97.25%
    
    benchmark                          old allocs    new allocs    delta
    BenchmarkTemplateSpecialTags-4     2650          190           -92.83%
    
    benchmark                          old bytes     new bytes     delta
    BenchmarkTemplateSpecialTags-4     4106460       46568         -98.87%
    
    While we are there, make sure we respect the HTML tokenization algorithm.
    An end tag needs to be followed by a space, tab, CR, FF, /, or > as described
    in https://html.spec.whatwg.org/multipage/syntax.html#tokenization
    Explicitly add this check.
    
    Fixes #10605
    
    Change-Id: Ia33ddee164ab608a69ac4183e16ec506bbeaa54c
    Reviewed-on: https://go-review.googlesource.com/9502Reviewed-by: 's avatarRob Pike <r@golang.org>
    f4e3e5ea
transition_test.go 1.71 KB