golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.65k stars 17.61k forks source link

slices: low performance from common misuse of Delete #58818

Closed earthboundkid closed 1 year ago

earthboundkid commented 1 year ago

Problem:

A quick search for slices.Delete on SourceGraph shows that many calls to slices.Delete are preceded by a call to slices.Index. This is often done in a loop, leading to needlessly terrible performance.

Possible solutions:

dmitshur commented 1 year ago

CC @ianlancetaylor, @eliben.

ianlancetaylor commented 1 year ago

Can you link to a couple of places? Thanks.

I'm fine with adding DeleteFunc but I don't see how it makes sense to remove Delete. The function's doc comment is even careful to explain its performance.

earthboundkid commented 1 year ago

https://sourcegraph.com/search?q=context:global+slices.Delete+lang:go+&patternType=standard&sm=1&groupBy=repo

I'm just going to do cursory reviews of the first handful of examples:

Around a quarter of examples seem to be misuses, and many examples are some variant of i := slices.Index(s, something); if i != -1 { s = slices.Delete(s, i, i+1) }.

rsc commented 1 year ago

Closing as duplicate of #54768.