Seems reasonable (although I must add that if you have a situation where
the overhead of lexing your templates is significant, you must be doing
something odd). Feel free to send a CL.

-rob


On Mon, Jul 11, 2016 at 1:28 PM, 'Paul Borman' via golang-nuts <
golang-nuts@googlegroups.com> wrote:

> Here are a set of results using benchcmp.  I also included a new
> benchmark, the code from example_test.go that executes a template 3 times
> with different input.  The template is:
>
> Dear {{.Name}},
> {{if .Attended}}
> It was a pleasure to see you at the wedding.
> {{- else}}
> It is a shame you couldn't make it to the wedding.
> {{- end}}
> {{with .Gift -}}
> Thank you for the lovely {{.}}.
> {{end}}
> Best wishes,
> Josie
>
>
> The density of {{'s is higher than in the T tests so it has a lesser
> gain.  Over 5 runs it shows overall improvement in all test cases (with the
> single exception of a 0.02% slowdown in 1 of the 5 "Many" runs).
>
> benchmark              old ns/op     new ns/op     delta
> BenchmarkR512-4        12018         4416          -63.26%
> BenchmarkT512-4        21456         14179         -33.92%
> BenchmarkR8k-4         135954        15203         -88.82%
> BenchmarkT8k-4         282175        137039        -51.43%
> BenchmarkMany-4        5948637       5576714       -6.25%
> BenchmarkExample-4     33824         30026         -11.23%
>
> benchmark              old ns/op     new ns/op     delta
> BenchmarkR512-4        12414         4436          -64.27%
> BenchmarkT512-4        21550         14282         -33.73%
> BenchmarkR8k-4         132062        14138         -89.29%
> BenchmarkT8k-4         267345        137823        -48.45%
> BenchmarkMany-4        5602299       5528760       -1.31%
> BenchmarkExample-4     32567         30108         -7.55%
>
> benchmark              old ns/op     new ns/op     delta
> BenchmarkR512-4        12251         4703          -61.61%
> BenchmarkT512-4        21788         14719         -32.44%
> BenchmarkR8k-4         133698        15989         -88.04%
> BenchmarkT8k-4         271711        141895        -47.78%
> BenchmarkMany-4        5595565       5596960       +0.02%
> BenchmarkExample-4     32677         30909         -5.41%
>
> benchmark              old ns/op     new ns/op     delta
> BenchmarkR512-4        12327         4532          -63.24%
> BenchmarkT512-4        22281         14112         -36.66%
> BenchmarkR8k-4         137433        14473         -89.47%
> BenchmarkT8k-4         272089        137517        -49.46%
> BenchmarkMany-4        5632862       5539014       -1.67%
> BenchmarkExample-4     32660         29871         -8.54%
>
> benchmark              old ns/op     new ns/op     delta
> BenchmarkR512-4        12369         4394          -64.48%
> BenchmarkT512-4        21671         14261         -34.19%
> BenchmarkR8k-4         133809        14251         -89.35%
> BenchmarkT8k-4         269672        142183        -47.28%
> BenchmarkMany-4        5579276       5576453       -0.05%
> BenchmarkExample-4     32522         29956         -7.89%
>
>
>
> On Mon, Jul 11, 2016 at 1:02 PM, Caleb Spare <cesp...@gmail.com> wrote:
>
>> Hey Paul, nice results. The benchmark results would be easier to
>> interpret if you provided benchcmp output instead.
>>
>>
>> On Mon, Jul 11, 2016 at 12:27 PM, 'Paul Borman' via golang-nuts
>> <golang-nuts@googlegroups.com> wrote:
>> > I was looking at text/template/parse/lex.go and noticed it seemed to be
>> very
>> > inefficient in how it searched for {{ in its input.  I rewrote lexText
>> to us
>> > strings.Index rather than calling l.next() for every single rune in the
>> > string.  The results were up to a 9x speed improvement, depending on the
>> > density of {{'s in the text.
>> >
>> > My results were:
>> >
>> > BenchmarkOldR512-4  100000     12300 ns/op
>> > BenchmarkNewR512-4  300000      4513 ns/op
>> > BenchmarkOldT512-4  100000     23600 ns/op
>> > BenchmarkNewT512-4  100000     13898 ns/op
>> > BenchmarkOldR8k-4   10000    133261 ns/op
>> > BenchmarkNewR8k-4  100000     14852 ns/op
>> > BenchmarkOldT8k-4    5000    264654 ns/op
>> > BenchmarkNewT8k-4   10000    135056 ns/op
>> > BenchmarkOldMany-4     300   5577909 ns/op
>> > BenchmarkNewMany-4     300   5632914 ns/op
>> >
>> > BenchmarkOldR512-4  100000     12100 ns/op
>> > BenchmarkNewR512-4  300000      4507 ns/op
>> > BenchmarkOldT512-4  100000     23297 ns/op
>> > BenchmarkNewT512-4  100000     14699 ns/op
>> > BenchmarkOldR8k-4   10000    136474 ns/op
>> > BenchmarkNewR8k-4  100000     16566 ns/op
>> > BenchmarkOldT8k-4    5000    266256 ns/op
>> > BenchmarkNewT8k-4   10000    137201 ns/op
>> > BenchmarkOldMany-4     300   5606573 ns/op
>> > BenchmarkNewMany-4     300   5596660 ns/op
>> >
>> > BenchmarkOldR512-4  100000     12150 ns/op
>> > BenchmarkNewR512-4  300000      4405 ns/op
>> > BenchmarkOldT512-4  100000     21539 ns/op
>> > BenchmarkNewT512-4  100000     13735 ns/op
>> > BenchmarkOldR8k-4   10000    133586 ns/op
>> > BenchmarkNewR8k-4  100000     14076 ns/op
>> > BenchmarkOldT8k-4    5000    264374 ns/op
>> > BenchmarkNewT8k-4   10000    132618 ns/op
>> > BenchmarkOldMany-4     300   5622040 ns/op
>> > BenchmarkNewMany-4     300   5638461 ns/op
>> >
>> >
>> > R512 was 512 bytes of Lorem Ipsum text with no {{}}'s
>> > R8K was 8,192 bytes of Lorem Ipsum text with no {{}}'s
>> >
>> > The T versions are the same, but with all occurrences of ipsum changed
>> to
>> > {{.}}
>> > (3 times in T512 and 13 times in T8K).
>> >
>> > The Many version was the string "{{.}}" repeated 1000 times.
>> >
>> > The old code:
>> >
>> >         for {
>> >                 delim, trimSpace := l.atLeftDelim()
>> >                 if delim {
>> >                         trimLength := Pos(0)
>> >                         if trimSpace {
>> >                                 trimLength =
>> > rightTrimLength(l.input[l.start:l.pos])
>> >                         }
>> >                         l.pos -= trimLength
>> >                         if l.pos > l.start {
>> >                                 l.emit(itemText)
>> >                         }
>> >                         l.pos += trimLength
>> >                         l.ignore()
>> >                         return lexLeftDelim
>> >                 }
>> >                 if l.next() == eof {
>> >                         break
>> >                 }
>> >         }
>> >
>> >
>> > The new code:
>> >
>> >         l.width = 0
>> >         if x := strings.Index(l.input[l.pos:], l.leftDelim); x >= 0 {
>> >                 ldn := Pos(len(l.leftDelim))
>> >                 l.pos += Pos(x)
>> >                 trimLength := Pos(0)
>> >                 if strings.HasPrefix(l.input[l.pos+ldn:],
>> leftTrimMarker) {
>> >                         trimLength =
>> rightTrimLength(l.input[l.start:l.pos])
>> >                 }
>> >                 l.pos -= trimLength
>> >                 if l.pos > l.start {
>> >                         l.emit(itemText)
>> >                 }
>> >                 l.pos += trimLength
>> >                 l.ignore()
>> >                 return lexLeftDelim
>> >         } else {
>> >                 l.pos = Pos(len(l.input))
>> >         }
>> >
>> > Is this something that would be accepted into the standard library?
>> >
>> > Thanks,
>> >
>> >     -Paul
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> Groups
>> > "golang-nuts" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> an
>> > email to golang-nuts+unsubscr...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to