-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: enhance performance #2135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ReneWerner87
wants to merge
12
commits into
valyala:master
Choose a base branch
from
ReneWerner87:performance
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+409
−43
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
65f770b
feat: enhance performance
ReneWerner87 1a7fc0f
fix: improve request URI parsing condition
ReneWerner87 9b038c0
feat: validate HTTP date parsing and optimize status code length calc…
ReneWerner87 17b7732
Address parsing and lint issues
ReneWerner87 5e2913f
chore: update Go version to 1.24.x in CI configuration
ReneWerner87 2d2fc57
feat: enhance HTTP date parsing and request URI handling
ReneWerner87 219a75b
refactor: optimize month and day name parsing using bitwise operations
ReneWerner87 d69765b
refactor: replace cookie token comparison with case insensitive funct…
ReneWerner87 8ec303b
refactor: streamline request body handling and simplify request URI a…
ReneWerner87 58ae4a6
chore: update Go version to 1.25.x in CI configuration
ReneWerner87 3231343
feat: add fuzz testing for HTTP date parsing to improve robustness
ReneWerner87 edca233
refactor: avoid unused return values in HTTP date parsing benchmarks
ReneWerner87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,8 @@ func AppendIPv4(dst []byte, ip net.IP) []byte { | |
|
|
||
| var errEmptyIPStr = errors.New("empty ip address string") | ||
|
|
||
| var httpDateGMT = time.FixedZone("GMT", 0) | ||
|
|
||
| // ParseIPv4 parses ip address from ipStr into dst and returns the extended dst. | ||
| func ParseIPv4(dst net.IP, ipStr []byte) (net.IP, error) { | ||
| if len(ipStr) == 0 { | ||
|
|
@@ -117,9 +119,133 @@ func AppendHTTPDate(dst []byte, date time.Time) []byte { | |
|
|
||
| // ParseHTTPDate parses HTTP-compliant (RFC1123) date. | ||
| func ParseHTTPDate(date []byte) (time.Time, error) { | ||
| if t, ok := parseRFC1123DateGMT(date); ok { | ||
| return t, nil | ||
| } | ||
| return time.Parse(time.RFC1123, b2s(date)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change this to
|
||
| } | ||
ReneWerner87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func parseRFC1123DateGMT(b []byte) (time.Time, bool) { | ||
erikdubbelboer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Expects "Mon, 02 Jan 2006 15:04:05 GMT". | ||
ReneWerner87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if len(b) != 29 { | ||
| return time.Time{}, false | ||
| } | ||
| if !isWeekday3(b[0], b[1], b[2]) { | ||
| return time.Time{}, false | ||
| } | ||
| if b[3] != ',' || b[4] != ' ' || b[7] != ' ' || b[11] != ' ' || | ||
| b[16] != ' ' || b[19] != ':' || b[22] != ':' || b[25] != ' ' { | ||
| return time.Time{}, false | ||
| } | ||
| if (b[26]|0x20) != 'g' || (b[27]|0x20) != 'm' || (b[28]|0x20) != 't' { | ||
| return time.Time{}, false | ||
| } | ||
|
|
||
| day, ok := parse2Digits(b[5], b[6]) | ||
| if !ok || day < 1 || day > 31 { | ||
| return time.Time{}, false | ||
| } | ||
| month, ok := parseMonth3(b[8], b[9], b[10]) | ||
| if !ok { | ||
ReneWerner87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return time.Time{}, false | ||
| } | ||
| year, ok := parse4Digits(b[12], b[13], b[14], b[15]) | ||
| if !ok { | ||
| return time.Time{}, false | ||
| } | ||
| hour, ok := parse2Digits(b[17], b[18]) | ||
| if !ok || hour > 23 { | ||
| return time.Time{}, false | ||
| } | ||
| minute, ok := parse2Digits(b[20], b[21]) | ||
| if !ok || minute > 59 { | ||
| return time.Time{}, false | ||
| } | ||
| second, ok := parse2Digits(b[23], b[24]) | ||
| if !ok || second > 59 { | ||
| return time.Time{}, false | ||
| } | ||
|
|
||
| t := time.Date(year, month, day, hour, minute, second, 0, httpDateGMT) | ||
| // Reject calendar-invalid dates like "31 Feb", which time.Date normalizes. | ||
| if t.Year() != year || t.Month() != month || t.Day() != day { | ||
| return time.Time{}, false | ||
| } | ||
| return t, true | ||
| } | ||
|
|
||
| func isWeekday3(a, b, c byte) bool { | ||
| a |= 0x20 | ||
| b |= 0x20 | ||
| c |= 0x20 | ||
| k := uint32(a)<<16 | uint32(b)<<8 | uint32(c) | ||
| switch k { | ||
| case uint32('m')<<16 | uint32('o')<<8 | uint32('n'), | ||
| uint32('t')<<16 | uint32('u')<<8 | uint32('e'), | ||
| uint32('w')<<16 | uint32('e')<<8 | uint32('d'), | ||
| uint32('t')<<16 | uint32('h')<<8 | uint32('u'), | ||
| uint32('f')<<16 | uint32('r')<<8 | uint32('i'), | ||
| uint32('s')<<16 | uint32('a')<<8 | uint32('t'), | ||
| uint32('s')<<16 | uint32('u')<<8 | uint32('n'): | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func parse2Digits(a, b byte) (int, bool) { | ||
| if a < '0' || a > '9' || b < '0' || b > '9' { | ||
| return 0, false | ||
| } | ||
| return int(a-'0')*10 + int(b-'0'), true | ||
| } | ||
|
|
||
| func parse4Digits(a, b, c, d byte) (int, bool) { | ||
| v1, ok := parse2Digits(a, b) | ||
| if !ok { | ||
| return 0, false | ||
| } | ||
| v2, ok := parse2Digits(c, d) | ||
| if !ok { | ||
| return 0, false | ||
| } | ||
| return v1*100 + v2, true | ||
| } | ||
|
|
||
| func parseMonth3(a, b, c byte) (time.Month, bool) { | ||
| a |= 0x20 | ||
| b |= 0x20 | ||
| c |= 0x20 | ||
| k := uint32(a)<<16 | uint32(b)<<8 | uint32(c) | ||
| switch k { | ||
| case uint32('j')<<16 | uint32('a')<<8 | uint32('n'): | ||
| return time.January, true | ||
| case uint32('f')<<16 | uint32('e')<<8 | uint32('b'): | ||
| return time.February, true | ||
| case uint32('m')<<16 | uint32('a')<<8 | uint32('r'): | ||
| return time.March, true | ||
| case uint32('a')<<16 | uint32('p')<<8 | uint32('r'): | ||
| return time.April, true | ||
| case uint32('m')<<16 | uint32('a')<<8 | uint32('y'): | ||
| return time.May, true | ||
| case uint32('j')<<16 | uint32('u')<<8 | uint32('n'): | ||
| return time.June, true | ||
| case uint32('j')<<16 | uint32('u')<<8 | uint32('l'): | ||
| return time.July, true | ||
| case uint32('a')<<16 | uint32('u')<<8 | uint32('g'): | ||
| return time.August, true | ||
| case uint32('s')<<16 | uint32('e')<<8 | uint32('p'): | ||
| return time.September, true | ||
| case uint32('o')<<16 | uint32('c')<<8 | uint32('t'): | ||
| return time.October, true | ||
| case uint32('n')<<16 | uint32('o')<<8 | uint32('v'): | ||
| return time.November, true | ||
| case uint32('d')<<16 | uint32('e')<<8 | uint32('c'): | ||
| return time.December, true | ||
| } | ||
| return 0, false | ||
| } | ||
|
|
||
| // AppendUint appends n to dst and returns the extended dst. | ||
| func AppendUint(dst []byte, n int) []byte { | ||
| if n < 0 { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.