Skip to content

http: optimize checkIsHttpToken() and checkInvalidHeaderChar()

Checklist
  • tests and code linting passes
  • the commit message follows commit guidelines
Affected core subsystem(s)
  • http
Description of change

This PR optimizes checkIsHttpToken() by:

  • Reducing the source code size to allow it to be inlinable with default v8 settings
  • Tweaking the order of the conditionals a bit to optimize for common cases.
  • Moving the character checking logic to a separate inlinable function to allow the main function to permit some loop unrolling.

checkInvalidHeaderChar() is also optimized by moving the character checking logic to a separate inlinable function to allow the main function to permit some loop unrolling.

The results from the existing check_is_http_token benchmark are (with the iterations bumped up to 5e8):

http/check_is_http_token.js key=TCN n=500000000: ./node: 80661000 ./node-master: 76596000 .................... 5.31%
http/check_is_http_token.js key=ETag n=500000000: ./node: 68376000 ./node-master: 59987000 .................. 13.98%
http/check_is_http_token.js key=date n=500000000: ./node: 77423000 ./node-master: 59011000 .................. 31.20%
http/check_is_http_token.js key=Vary n=500000000: ./node: 71596000 ./node-master: 59517000 .................. 20.30%
http/check_is_http_token.js key=server n=500000000: ./node: 86029000 ./node-master: 41288000 ............... 108.36%
http/check_is_http_token.js key=Server n=500000000: ./node: 82373000 ./node-master: 43516000 ................ 89.29%
http/check_is_http_token.js key=status n=500000000: ./node: 86021000 ./node-master: 43569000 ................ 97.43%
http/check_is_http_token.js key=version n=500000000: ./node: 66875000 ./node-master: 38341000 ............... 74.42%
http/check_is_http_token.js key=Expires n=500000000: ./node: 68358000 ./node-master: 36367000 ............... 87.97%
http/check_is_http_token.js key=alt-svc n=500000000: ./node: 60369000 ./node-master: 35545000 ............... 69.84%
http/check_is_http_token.js key=location n=500000000: ./node: 54662000 ./node-master: 34359000 .............. 59.09%
http/check_is_http_token.js key=Connection n=500000000: ./node: 41642000 ./node-master: 25207000 ............ 65.20%
http/check_is_http_token.js key=Keep-Alive n=500000000: ./node: 36692000 ./node-master: 27106000 ............ 35.36%
http/check_is_http_token.js key=content-type n=500000000: ./node: 29480000 ./node-master: 20660000 .......... 42.69%
http/check_is_http_token.js key=Content-Type n=500000000: ./node: 28407000 ./node-master: 22059000 .......... 28.77%
http/check_is_http_token.js key=Cache-Control n=500000000: ./node: 26861000 ./node-master: 19907000 ......... 34.93%
http/check_is_http_token.js key=Last-Modified n=500000000: ./node: 25668000 ./node-master: 19906000 ......... 28.95%
http/check_is_http_token.js key=Accept-Ranges n=500000000: ./node: 26276000 ./node-master: 18999000 ......... 38.30%
http/check_is_http_token.js key=content-length n=500000000: ./node: 22521000 ./node-master: 18320000 ........ 22.93%
http/check_is_http_token.js key=x-frame-options n=500000000: ./node: 19494000 ./node-master: 16385000 ....... 18.97%
http/check_is_http_token.js key=x-xss-protection n=500000000: ./node: 18531000 ./node-master: 14178000 ...... 30.71%
http/check_is_http_token.js key=Content-Encoding n=500000000: ./node: 21053000 ./node-master: 15840000 ...... 32.91%
http/check_is_http_token.js key=Content-Location n=500000000: ./node: 21052000 ./node-master: 15485000 ...... 35.95%
http/check_is_http_token.js key=Transfer-Encoding n=500000000: ./node: 19648000 ./node-master: 14687000 ..... 33.78%
http/check_is_http_token.js key=alternate-protocol n=500000000: ./node: 16859000 ./node-master: 13820000 .... 21.98%
http/check_is_http_token.js key=: n=500000000: ./node: 201910000 ./node-master: 105210000 ................... 91.92%
http/check_is_http_token.js key=@@ n=500000000: ./node: 201910000 ./node-master: 104490000 .................. 93.23%
http/check_is_http_token.js key=中文呢 n=500000000: ./node: 185510000 ./node-master: 101830000 ............... 82.18%
http/check_is_http_token.js key=((((()))) n=500000000: ./node: 316610000 ./node-master: 103700000 .......... 205.32%
http/check_is_http_token.js key=:alternate-protocol n=500000000: ./node: 202710000 ./node-master: 103840000 . 95.21%
http/check_is_http_token.js key=alternate-protocol: n=500000000: ./node: 16225000 ./node-master: 12317000 ... 31.73%

The results from the new check_invalid_header_char benchmark are:

http/check_invalid_header_char.js key="" n="500000000": ./node: 589640000 ./node-master: 605710000 ............................................................................................................ -2.65%
http/check_invalid_header_char.js key="1" n="500000000": ./node: 159060000 ./node-master: 156190000 ............................................................................................................ 1.83%
http/check_invalid_header_char.js key="\t\t\t\t\t\t\t\t\t\tFoo bar baz" n="500000000": ./node: 14964000 ./node-master: 11644000 ............................................................................... 28.51%
http/check_invalid_header_char.js key="keep-alive" n="500000000": ./node: 41767000 ./node-master: 24943000 .................................................................................................... 67.45%
http/check_invalid_header_char.js key="close" n="500000000": ./node: 108590000 ./node-master: 54515000 ........................................................................................................ 99.18%
http/check_invalid_header_char.js key="gzip" n="500000000": ./node: 83603000 ./node-master: 66625000 .......................................................................................................... 25.48%
http/check_invalid_header_char.js key="20091" n="500000000": ./node: 58258000 ./node-master: 56457000 .......................................................................................................... 3.19%
http/check_invalid_header_char.js key="private" n="500000000": ./node: 68037000 ./node-master: 39502000 ....................................................................................................... 72.24%
http/check_invalid_header_char.js key="text/html; charset=utf-8" n="500000000": ./node: 12537000 ./node-master: 10960000 ...................................................................................... 14.39%
http/check_invalid_header_char.js key="text/plain" n="500000000": ./node: 41925000 ./node-master: 24940000 .................................................................................................... 68.10%
http/check_invalid_header_char.js key="Sat, 07 May 2016 16:54:48 GMT" n="500000000": ./node: 10075000 ./node-master: 9243500 ................................................................................... 9.00%
http/check_invalid_header_char.js key="SAMEORIGIN" n="500000000": ./node: 40381000 ./node-master: 24941000 .................................................................................................... 61.91%
http/check_invalid_header_char.js key="en-US" n="500000000": ./node: 110410000 ./node-master: 54398000 ....................................................................................................... 102.97%
http/check_invalid_header_char.js key="Here is a value that is really a folded header value\r\n  this should be      supported, but it is not currently" n="500000000": ./node: 5479200 ./node-master: 5359200 . 2.24%
http/check_invalid_header_char.js key="中文呢" n="500000000": ./node: 533970000 ./node-master: 280280000 ....................................................................................................... 90.51%
http/check_invalid_header_char.js key="foo\nbar" n="500000000": ./node: 182810000 ./node-master: 70721000 .................................................................................................... 158.49%
http/check_invalid_header_char.js key="" n="500000000": ./node: 468570000 ./node-master: 294120000 ........................................................................................................... 59.31%

Merge request reports

Loading