Skip to content

refactor: using slices.Contains to simplify the code#6234

Merged
gmalouf merged 1 commit intoalgorand:masterfrom
damuzhi0810:master
Feb 12, 2025
Merged

refactor: using slices.Contains to simplify the code#6234
gmalouf merged 1 commit intoalgorand:masterfrom
damuzhi0810:master

Conversation

@damuzhi0810
Copy link
Copy Markdown
Contributor

Summary

This is a new function added in the go1.21 standard library, which can make the code more concise and easy to read.

Test Plan

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jan 23, 2025

CLA assistant check
All committers have signed the CLA.

@jannotti
Copy link
Copy Markdown
Contributor

Thanks, makes sense. I wonder if there's a tool to find the old code pattern. We've switched to slices.Contains in many places, but missed this one.

@damuzhi0810
Copy link
Copy Markdown
Contributor Author

Thanks, makes sense. I wonder if there's a tool to find the old code pattern. We've switched to slices.Contains in many places, but missed this one.

I also search by keywords, and there will be a lot of omissions. But I think I can write such a tool

@jannotti
Copy link
Copy Markdown
Contributor

Thanks, makes sense. I wonder if there's a tool to find the old code pattern. We've switched to slices.Contains in many places, but missed this one.

I also search by keywords, and there will be a lot of omissions. But I think I can write such a tool

I did something with semgrep and found these:

┌──────────────────┐
│ 11 Code Findings │
└──────────────────┘

    cmd/algons/dnsCmd.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

          443┆ for _, known := range append(recordTypes, "") {
          444┆   if recordType == known {
          445┆           isKnown = true
          446┆           break
          447┆   }
          448┆ }

    cmd/goal/accountsList.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

          104┆ for _, name := range accountList.Accounts {
          105┆   if name == accountName {
          106┆           return true
          107┆   }
          108┆ }

    data/transactions/logic/eval.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

          5301┆ for _, assetID := range cx.txn.Txn.ForeignAssets {
          5302┆  if assetID == aid {
          5303┆          return true
          5304┆  }
          5305┆ }
            ⋮┆----------------------------------------
          5345┆ for _, appID := range cx.txn.Txn.ForeignApps {
          5346┆  if appID == aid {
          5347┆          return true
          5348┆  }
          5349┆ }

    network/requestTracker.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

          522┆ for _, v := range []string{"localhost", "127.0.0.1",
               "[::1]", "::1", "[::]"} {
          523┆   if host == v {
          524┆           return true
          525┆   }
          526┆ }

    network/wsNetwork.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

          928┆ for _, supportedProtocolVersion := range
               wn.supportedProtocolVersions {
          929┆   if supportedProtocolVersion == otherVersion {
          930┆           return supportedProtocolVersion, otherVersion
          931┆   }
          932┆ }

    network/wsPeer.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

           56┆ for _, version := range SupportedProtocolVersions {
           57┆   if version == versionPeerFeatures {
           58┆           matched = true
           59┆   }
           60┆ }

    shared/pingpong/accounts.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

          1006┆ for _, v := range they {
          1007┆  if v == x {
          1008┆          return they
          1009┆  }
          1010┆ }

    shared/pingpong/config.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

          196┆ for _, v := range accountSampleMethods {
          197┆   if v == cfg.GeneratedAccountSampleMethod {
          198┆           sampleOk = true
          199┆           break
          200┆   }
          201┆ }

    tools/block-generator/generator/generator_ledger.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

          337┆ for _, boxOptin := range ledgerBoxEvidence[appId] {
          338┆   if boxOptin == optin {
          339┆           missing = false
          340┆           break
          341┆   }
          342┆ }

    util/codecs/json.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

          187┆ for _, s := range set {
          188┆   if item == s {
          189┆           return true
          190┆   }
          191┆ }

@jannotti
Copy link
Copy Markdown
Contributor

jannotti commented Jan 23, 2025

Oh, the one you found can't be changed. That accountsList is a map, not a slice.

My semgrep rule had the same issue. I fixed that, so there are only 5 detected now, instead of 11.

@jannotti jannotti self-requested a review January 23, 2025 17:40
Copy link
Copy Markdown
Contributor

@jannotti jannotti left a comment

Choose a reason for hiding this comment

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

To remove approval, I have to comment. Issue is the map vs slice thing.

@damuzhi0810
Copy link
Copy Markdown
Contributor Author

To remove approval, I have to comment. Issue is the map vs slice thing.

Great job. I'll modify the other items you found.

@damuzhi0810
Copy link
Copy Markdown
Contributor Author

To remove approval, I have to comment. Issue is the map vs slice thing.

Everything else has been modified, I will keep an eye on ci.

But I feel like these are

    data/transactions/logic/eval.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

          5301for _, assetID := range cx.txn.Txn.ForeignAssets {
          5302if assetID == aid {
          5303return true
          5304┆  }
          5305┆ }
            ⋮┆----------------------------------------
          5345for _, appID := range cx.txn.Txn.ForeignApps {
          5346if appID == aid {
          5347return true
          5348┆  }
          5349┆ }

Since there is no return false operation, it may not be possible to use slice.Contains optimization.

@jannotti
Copy link
Copy Markdown
Contributor

But I feel like these are

    data/transactions/logic/eval.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

          5301for _, assetID := range cx.txn.Txn.ForeignAssets {
          5302if assetID == aid {
          5303return true
          5304┆  }
          5305┆ }

Since there is no return false operation, it may not be possible to use slice.Contains optimization.

	if slices.Contains(cx.txn.Txn.ForeignAssets, aid) {
		return true
	}

Comment thread cmd/algons/dnsCmd.go Outdated
Comment thread network/wsPeer.go Outdated
Comment thread shared/pingpong/accounts.go Outdated
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 5, 2025

Codecov Report

Attention: Patch coverage is 66.66667% with 5 lines in your changes missing coverage. Please review.

Project coverage is 51.82%. Comparing base (a81d54f) to head (7014fc1).
Report is 14 commits behind head on master.

Files with missing lines Patch % Lines
shared/pingpong/accounts.go 0.00% 2 Missing ⚠️
cmd/algons/dnsCmd.go 0.00% 1 Missing ⚠️
network/wsPeer.go 0.00% 0 Missing and 1 partial ⚠️
shared/pingpong/config.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6234      +/-   ##
==========================================
- Coverage   51.83%   51.82%   -0.02%     
==========================================
  Files         643      643              
  Lines       86382    86355      -27     
==========================================
- Hits        44780    44754      -26     
+ Misses      38735    38730       -5     
- Partials     2867     2871       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@damuzhi0810 damuzhi0810 force-pushed the master branch 2 times, most recently from eea4137 to 00e5073 Compare February 5, 2025 09:11
@damuzhi0810
Copy link
Copy Markdown
Contributor Author

damuzhi0810 commented Feb 5, 2025

But I feel like these are

    data/transactions/logic/eval.go
   ❯❯❱ Users.jj.no-reimplement-slices.Contains
          Don't write a loop for that, use slices.Contains instead!

          5301for _, assetID := range cx.txn.Txn.ForeignAssets {
          5302if assetID == aid {
          5303return true
          5304┆  }
          5305┆ }

Since there is no return false operation, it may not be possible to use slice.Contains optimization.

	if slices.Contains(cx.txn.Txn.ForeignAssets, aid) {
		return true
	}

That makes sense!

Modified. Please review again. Thank you!

@damuzhi0810
Copy link
Copy Markdown
Contributor Author

All CI checks have passed, please review again.

Comment thread network/wsNetwork.go Outdated
@jannotti jannotti requested a review from cce February 6, 2025 19:02
Signed-off-by: damuzhi0810 <rust@before.tech>
@gmalouf gmalouf merged commit 2f07e60 into algorand:master Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants