Skip to content

html: avoid copying tag names and attributes without NUL bytes - #272

Open
thevilledev wants to merge 1 commit into
golang:masterfrom
thevilledev:fix/html-tag-nul-copy
Open

thevilledev wants to merge 1 commit into
golang:masterfrom
thevilledev:fix/html-tag-nul-copy

Conversation

@thevilledev

Copy link
Copy Markdown

CL 781883 made TagName and TagAttr replace NUL bytes in tag names and
attribute keys and values using bytes.ReplaceAll. ReplaceAll returns a
copy even if there is nothing to replace, so since then every tag name,
attribute key and attribute value is allocated, although they rarely
contain a NUL byte.

Only copy them if they contain a NUL byte, as Text already does.
Otherwise return slices of the tokenizer's buffer, as before CL 781883,
which the documentation of Raw allows.

goos: darwin
goarch: arm64
pkg: golang.org/x/net/html
cpu: Apple M1 Max
│ master.txt │ new.txt │
│ sec/op │ sec/op vs base │
Parser-10 820.1µ ± 0% 761.2µ ± 0% -7.19% (p=0.000 n=10)
RawLevelTokenizer-10 276.3µ ± 0% 277.0µ ± 1% ~ (p=0.190 n=10)
LowLevelTokenizer-10 437.9µ ± 0% 371.0µ ± 1% -15.27% (p=0.000 n=10)
HighLevelTokenizer-10 516.0µ ± 1% 451.1µ ± 2% -12.59% (p=0.000 n=10)
geomean 475.7µ 433.4µ -8.89%

                  │  master.txt  │               new.txt                │
                  │     B/s      │     B/s       vs base                │

Parser-10 90.89Mi ± 0% 97.92Mi ± 0% +7.74% (p=0.000 n=10)
RawLevelTokenizer-10 269.8Mi ± 0% 269.1Mi ± 1% ~ (p=0.190 n=10)
LowLevelTokenizer-10 170.2Mi ± 0% 200.9Mi ± 1% +18.03% (p=0.000 n=10)
HighLevelTokenizer-10 144.4Mi ± 1% 165.2Mi ± 2% +14.40% (p=0.000 n=10)
geomean 156.7Mi 172.0Mi +9.75%

                  │  master.txt   │                new.txt                 │
                  │     B/op      │     B/op      vs base                  │

Parser-10 575.4Ki ± 0% 539.6Ki ± 0% -6.22% (p=0.000 n=10)
RawLevelTokenizer-10 10.23Ki ± 0% 10.23Ki ± 0% ~ (p=1.000 n=10) ¹
LowLevelTokenizer-10 42.29Ki ± 0% 10.23Ki ± 0% -75.80% (p=0.000 n=10)
HighLevelTokenizer-10 128.15Ki ± 0% 92.36Ki ± 0% -27.93% (p=0.000 n=10)
geomean 75.16Ki 47.80Ki -36.40%
¹ all samples are equal

                  │  master.txt  │                new.txt                │
                  │  allocs/op   │  allocs/op   vs base                  │

Parser-10 11.189k ± 0% 7.470k ± 0% -33.24% (p=0.000 n=10)
RawLevelTokenizer-10 678.0 ± 0% 678.0 ± 0% ~ (p=1.000 n=10) ¹
LowLevelTokenizer-10 4397.0 ± 0% 678.0 ± 0% -84.58% (p=0.000 n=10)
HighLevelTokenizer-10 7.094k ± 0% 3.375k ± 0% -52.42% (p=0.000 n=10)
geomean 3.922k 1.845k -52.96%
¹ all samples are equal

Fixes golang/go#82128

CL 781883 made TagName and TagAttr replace NUL bytes in tag names and
attribute keys and values using bytes.ReplaceAll. ReplaceAll returns a
copy even if there is nothing to replace, so since then every tag name,
attribute key and attribute value is allocated, although they rarely
contain a NUL byte.

Only copy them if they contain a NUL byte, as Text already does.
Otherwise return slices of the tokenizer's buffer, as before CL 781883,
which the documentation of Raw allows.

goos: darwin
goarch: arm64
pkg: golang.org/x/net/html
cpu: Apple M1 Max
                      │ master.txt  │               new.txt               │
                      │   sec/op    │   sec/op     vs base                │
Parser-10               820.1µ ± 0%   761.2µ ± 0%   -7.19% (p=0.000 n=10)
RawLevelTokenizer-10    276.3µ ± 0%   277.0µ ± 1%        ~ (p=0.190 n=10)
LowLevelTokenizer-10    437.9µ ± 0%   371.0µ ± 1%  -15.27% (p=0.000 n=10)
HighLevelTokenizer-10   516.0µ ± 1%   451.1µ ± 2%  -12.59% (p=0.000 n=10)
geomean                 475.7µ        433.4µ        -8.89%

                      │  master.txt  │               new.txt                │
                      │     B/s      │     B/s       vs base                │
Parser-10               90.89Mi ± 0%   97.92Mi ± 0%   +7.74% (p=0.000 n=10)
RawLevelTokenizer-10    269.8Mi ± 0%   269.1Mi ± 1%        ~ (p=0.190 n=10)
LowLevelTokenizer-10    170.2Mi ± 0%   200.9Mi ± 1%  +18.03% (p=0.000 n=10)
HighLevelTokenizer-10   144.4Mi ± 1%   165.2Mi ± 2%  +14.40% (p=0.000 n=10)
geomean                 156.7Mi        172.0Mi        +9.75%

                      │  master.txt   │                new.txt                 │
                      │     B/op      │     B/op      vs base                  │
Parser-10                575.4Ki ± 0%   539.6Ki ± 0%   -6.22% (p=0.000 n=10)
RawLevelTokenizer-10     10.23Ki ± 0%   10.23Ki ± 0%        ~ (p=1.000 n=10) ¹
LowLevelTokenizer-10     42.29Ki ± 0%   10.23Ki ± 0%  -75.80% (p=0.000 n=10)
HighLevelTokenizer-10   128.15Ki ± 0%   92.36Ki ± 0%  -27.93% (p=0.000 n=10)
geomean                  75.16Ki        47.80Ki       -36.40%
¹ all samples are equal

                      │  master.txt  │                new.txt                │
                      │  allocs/op   │  allocs/op   vs base                  │
Parser-10               11.189k ± 0%   7.470k ± 0%  -33.24% (p=0.000 n=10)
RawLevelTokenizer-10      678.0 ± 0%    678.0 ± 0%        ~ (p=1.000 n=10) ¹
LowLevelTokenizer-10     4397.0 ± 0%    678.0 ± 0%  -84.58% (p=0.000 n=10)
HighLevelTokenizer-10    7.094k ± 0%   3.375k ± 0%  -52.42% (p=0.000 n=10)
geomean                  3.922k        1.845k       -52.96%
¹ all samples are equal

Fixes golang/go#82128
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: c0fead6) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/net/+/848445.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Jean-Baptiste Lernout:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/848445.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Ville Vesilehto:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/848445.
After addressing review feedback, remember to publish your drafts!

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

Successfully merging this pull request may close these issues.

x/net/html: Tokenizer.TagName and TagAttr allocate for every tag since v0.56.0

2 participants