Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@juejinyuxitu
Copy link
Contributor

Inspired by https://go-review.googlesource.com/c/go/+/639536

After using strings.FieldsFuncSeq, the number of memory allocations has been reduced from 2 to 0.

The following is the complete benchamark code and results:

package main

import (
	"strings"
	"testing"
)

func isSlashRune(r rune) bool { return r == '/' || r == '\\' }

func containsDotDotLoop(v string) bool {
	if !strings.Contains(v, "..") {
		return false
	}
	for _, ent := range strings.FieldsFunc(v, isSlashRune) {
		if ent == ".." {
			return true
		}
	}
	return false
}

func containsDotDotSeq(v string) bool {
	if !strings.Contains(v, "..") {
		return false
	}
	for ent := range strings.FieldsFuncSeq(v, isSlashRune) {
		if ent == ".." {
			return true
		}
	}
	return false
}

func BenchmarkDotDot(b *testing.B) {
	testCases := []string{
		"/path/to/somewhere",
		"/path/../to/somewhere",
		"/really/long/path/with/many/segments",
		"../../../deep/path",
	}

	b.Run("Loop", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			for _, tc := range testCases {
				containsDotDotLoop(tc)
			}
		}
	})

	b.Run("Seq", func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			for _, tc := range testCases {
				containsDotDotSeq(tc)
			}
		}
	})
}
go test -bench=. -benchmem

goos: darwin
goarch: arm64
pkg: bc
cpu: Apple M1
BenchmarkDotDot/Loop-8         	 6133270	       193.7 ns/op	     144 B/op	       2 allocs/op
BenchmarkDotDot/Seq-8          	23172360	        51.19 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	bc	2.633s

@juejinyuxitu juejinyuxitu requested a review from a team as a code owner September 24, 2025 15:12
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Sep 24, 2025
@cliAutomation
Copy link
Collaborator

Hi! Thanks for the pull request. Please ensure that this change is linked to an issue by mentioning an issue number in the description of the pull request. If this pull request would close the issue, please put the word 'Fixes' before the issue number somewhere in the pull request body. If this is a tiny change like fixing a typo, feel free to ignore this message.

@juejinyuxitu
Copy link
Contributor Author

@babakks Hi, Could you please review this PR at your convenience? Thank you very much.

Copy link
Member

@babakks babakks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for the refactor, @juejinyuxitu!

@babakks babakks merged commit 52ba836 into cli:trunk Sep 30, 2025
15 checks passed
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Oct 10, 2025
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [cli/cli](https://github.com/cli/cli) | minor | `v2.80.0` -> `v2.81.0` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>cli/cli (cli/cli)</summary>

### [`v2.81.0`](https://github.com/cli/cli/releases/tag/v2.81.0): GitHub CLI 2.81.0

[Compare Source](cli/cli@v2.80.0...v2.81.0)

#### Support for GitHub Release Attestations

This release introduces the `release verify` and `release verify-asset` commands for verifying GitHub Release attestations. Part of the Immutable Releases initiative, a release attestation provides a signed, timestamped binding between a release, its git tag, and any associated assets. These new commands provide a convenient way to verify the integrity of an immutable release against its attestation.

- Verify the latest release has a valid attestation: `gh release verify`
- Verify a specific release by tag: `gh release verify v1.2.3`
- Verify an asset from the latest release: `gh release verify-asset my-asset.zip`
- Verify a local asset file originated from a specific release: `gh release verify-asset v1.2.3 my-asset.zip`

These commands help ensure that releases and their assets are authentic and haven’t been tampered with, providing an additional layer of security for your software supply chain.

#### `gh auth status` Supports JSON Output

This release adds support for the `--json` flag in `gh auth status`. Run `gh auth status --help` for more information and usage examples.

#### What's Changed

##### ✨ Features

- Add alias `co` for `pr checkout` by [@&#8203;babakks](https://github.com/babakks) in [#&#8203;11804](cli/cli#11804)
- Add JSON output to `gh auth status` by [@&#8203;benjlevesque](https://github.com/benjlevesque) in [#&#8203;11544](cli/cli#11544)
- `release verify`  and `release verify-asset` are now visible by [@&#8203;ejahnGithub](https://github.com/ejahnGithub) in [#&#8203;11801](cli/cli#11801)

##### 🐛 Fixes

- Fix no tagname logic for  release verify-asset  by [@&#8203;ejahnGithub](https://github.com/ejahnGithub) in [#&#8203;11798](cli/cli#11798)

##### 📚 Docs & Chores

- refactor: use strings.FieldsFuncSeq to reduce memory allocations by [@&#8203;juejinyuxitu](https://github.com/juejinyuxitu) in [#&#8203;11805](cli/cli#11805)

#### New Contributors

- [@&#8203;juejinyuxitu](https://github.com/juejinyuxitu) made their first contribution in [#&#8203;11805](cli/cli#11805)

**Full Changelog**: <cli/cli@v2.80.0...v2.81.0>

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xMzIuNSIsInVwZGF0ZWRJblZlciI6IjQxLjEzMi41IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants