net/netip: optimize parseIPv4 and refactor IPv6 embedded IPv4 parsing
This change refactors the parseIPv4 function to extract a new helper
function, parseIPv4Fields, which is now used by both parseIPv4 and
parseIPv6 functions. The extraction of this logic into a separate
helper function removes code duplication and improves the performance
of parsing IPv6 addresses that contain an embedded IPv4 address.
Additionally, the error handling within the IP address parsing logic
has been streamlined to provide clearer messages when encountering
incorrect formats or values in IPv4 fields.
Benchmark:
```
benchstat old.out new.out
goos: darwin
goarch: amd64
pkg: net/netip
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
│ old.out │ new.out │
│ sec/op │ sec/op vs base │
ParseAddr/v4-12 22.23n ± 2% 21.86n ± 2% ~ (p=0.127 n=10)
ParseAddr/v6-12 69.67n ± 7% 70.31n ± 1% ~ (p=0.128 n=10)
ParseAddr/v6_ellipsis-12 48.22n ± 17% 48.58n ± 1% ~ (p=0.739 n=10)
ParseAddr/v6_v4-12 60.73n ± 36% 51.54n ± 1% -15.14% (p=0.000 n=10)
ParseAddr/v6_zone-12 102.50n ± 22% 93.50n ± 0% -8.79% (p=0.000 n=10)
ParseAddrPort/v4-12 38.07n ± 1% 36.84n ± 2% -3.22% (p=0.000 n=10)
ParseAddrPort/v6-12 84.61n ± 1% 87.21n ± 1% +3.07% (p=0.000 n=10)
ParseAddrPort/v6_ellipsis-12 69.65n ± 8% 64.56n ± 2% -7.31% (p=0.023 n=10)
ParseAddrPort/v6_v4-12 71.88n ± 1% 70.61n ± 1% -1.76% (p=0.000 n=10)
ParseAddrPort/v6_zone-12 119.0n ± 2% 118.0n ± 2% ~ (p=0.108 n=10)
geomean 62.38n 60.17n -3.54%
│ old.out │ new.out │
│ B/op │ B/op vs base │
ParseAddr/v4-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddr/v6-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddr/v6_ellipsis-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddr/v6_v4-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddr/v6_zone-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddrPort/v4-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddrPort/v6-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddrPort/v6_ellipsis-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddrPort/v6_v4-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddrPort/v6_zone-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
geomean ² +0.00% ²
¹ all samples are equal
² summaries must be >0 to compute geomean
│ old.out │ new.out │
│ allocs/op │ allocs/op vs base │
ParseAddr/v4-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddr/v6-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddr/v6_ellipsis-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddr/v6_v4-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddr/v6_zone-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddrPort/v4-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddrPort/v6-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddrPort/v6_ellipsis-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddrPort/v6_v4-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
ParseAddrPort/v6_zone-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹
geomean ² +0.00% ²
¹ all samples are equal
² summaries must be >0 to compute geomean
```
Change-Id: I403cb76f449a0bf203a821294df25d3c9031df4c
GitHub-Last-Rev:
917f78ce4ef2a4156d0291c36047689de1764c3f
GitHub-Pull-Request: golang/go#64219
Reviewed-on: https://go-review.googlesource.com/c/go/+/543179
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@golang.org>