]> Cypherpunks repositories - gostls13.git/commit
html/template: fix quadratic performance with special tags
authorDidier Spezia <didier.06@gmail.com>
Wed, 29 Apr 2015 18:59:04 +0000 (18:59 +0000)
committerRob Pike <r@golang.org>
Thu, 30 Apr 2015 16:22:29 +0000 (16:22 +0000)
commitf4e3e5eaf0833ef21ae477ad888f6bccfd93d1bc
treed7dc77ff18e38824f49c4a3da24ff5307d641abf
parent79a990b845f358d11f725a574bdc2c80339282ef
html/template: fix quadratic performance with special tags

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/9502
Reviewed-by: Rob Pike <r@golang.org>
src/html/template/transition.go
src/html/template/transition_test.go [new file with mode: 0644]