1
0
mirror of https://github.com/distribution/distribution synced 2024-11-12 05:45:51 +01:00
distribution/registry/handlers
Sebastiaan van Stijn 3b391d3290
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:

```go
func BenchmarkSplit(b *testing.B) {
	b.ReportAllocs()
	data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
	for i := 0; i < b.N; i++ {
		for _, s := range data {
			_ = strings.SplitN(s, "=", 2)[0]
		}
	}
}

func BenchmarkCut(b *testing.B) {
	b.ReportAllocs()
	data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
	for i := 0; i < b.N; i++ {
		for _, s := range data {
			_, _, _ = strings.Cut(s, "=")
		}
	}
}
```

    BenchmarkSplit
    BenchmarkSplit-10    	 8244206	       128.0 ns/op	     128 B/op	       4 allocs/op
    BenchmarkCut
    BenchmarkCut-10      	54411998	        21.80 ns/op	       0 B/op	       0 allocs/op

While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;

- for cases where an specific number of items is expected, I used `strings.SplitN()`
  with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
  a prefix; for those I replaced the code to just match (and/or strip) the prefix.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-10 22:38:12 +01:00
..
api_test.go replace deprecated io/ioutil 2022-11-04 23:47:52 +01:00
app_test.go Merge pull request #3766 from thaJeztah/gofumpt 2022-11-04 12:19:53 +01:00
app.go Merge pull request #3766 from thaJeztah/gofumpt 2022-11-04 12:19:53 +01:00
basicauth_prego14.go bump up golang v1.17 2021-10-27 15:51:30 +08:00
basicauth.go bump up golang v1.17 2021-10-27 15:51:30 +08:00
blob.go use http consts for request methods 2022-11-02 23:31:47 +01:00
blobupload.go Merge pull request #3766 from thaJeztah/gofumpt 2022-11-04 12:19:53 +01:00
catalog.go Merge pull request #3766 from thaJeztah/gofumpt 2022-11-04 12:19:53 +01:00
context.go go.mod: change imports to github.com/distribution/distribution/v3 2021-02-08 18:30:46 +01:00
health_test.go replace deprecated io/ioutil 2022-11-04 23:47:52 +01:00
helpers.go replace strings.Split(N) for strings.Cut() or alternatives 2022-11-10 22:38:12 +01:00
hmac_test.go Refactor Blob Service API 2015-05-15 17:05:18 -07:00
hmac.go handlers: provide better log message on mismatched secret 2016-09-02 15:24:35 -07:00
hooks.go replace strings.Split(N) for strings.Cut() or alternatives 2022-11-10 22:38:12 +01:00
mail.go Fix gometalint errors 2019-02-04 16:01:04 -08:00
manifests.go Merge pull request #3766 from thaJeztah/gofumpt 2022-11-04 12:19:53 +01:00
tags.go use http consts for request methods 2022-11-02 23:31:47 +01:00