Skip to content

proposal: builtin: delete returns bool #41130

Closed
@pjebs

Description

@pjebs

It would be good if the delete function returns a bool indicating if the key was found or not. if it was found, it returns true and the key is obviously deleted.

In my article: https://medium.com/swlh/ordered-maps-for-go-using-generics-875ef3816c71#4102, you can see that after I attempt to delete a key from the map, I have to do an expensive operation (optimising the data struct using linked list is besides the point). The operation only needs to be done if the key exists.

I can't check existence for key first and then attempt to delete because it's not atomic.

Activity

seankhliao

seankhliao commented on Aug 29, 2020

@seankhliao
Member

You mention check-then-delete is not atomic, which indicates you want them for concurrent use, but maps are not safe for concurrent use without synchronization, so the correct solution would be to introduce a lock or some other method of synchronization which would also cover check-then-delete

pjebs

pjebs commented on Aug 29, 2020

@pjebs
ContributorAuthor

Using a synchronisation mechanism is overkill in my situation. I just need to be informed if the delete() actually deleted a key or not. Internally, the function has that information. It just needs to release it.

pjebs

pjebs commented on Aug 29, 2020

@pjebs
ContributorAuthor

On second thoughts, you are correct but I still think this proposal provides benefits with no cost and is backward compatible

changed the title [-]builtin: delete returns bool (atomic)[/-] [+]proposal: builtin: delete returns bool (atomic)[/+] on Aug 30, 2020
added this to the Proposal milestone on Aug 30, 2020
changed the title [-]proposal: builtin: delete returns bool (atomic)[/-] [+]proposal: builtin: delete returns bool[/+] on Sep 2, 2020
rsc

rsc commented on Sep 2, 2020

@rsc
Contributor

In the non-atomic case, this boils down to:

if _, ok := m[key]; ok {
    delete(m, key) // you know key was deleted
}

This seems like pretty clear code.
The only real argument against it is that it might be inefficient to do the hash twice.
We could fix that in the compiler (#5147) if needed.

It doesn't seem worth a language change.

But you wrote:

I can't check existence for key first and then attempt to delete because it's not atomic.

If there are other map changes happening at the same time, your code is unsafe and will crash, either from the race detector or from the map implementation's own race detector. Adding an 'atomic' delete would require synchronizing every write and delete just in case there was a racing write, which would slow down all accesses. That's why maps aren't atomic in the first place.

Given that maps aren't atomic and you need an atomic operation, I think it's safe to say this is infeasible.

mewmew

mewmew commented on Sep 2, 2020

@mewmew
locked and limited conversation to collaborators on Sep 16, 2021
moved this to Declined in Proposalson Aug 10, 2022
removed this from Proposalson Oct 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @rsc@mewmew@ianlancetaylor@pjebs@gopherbot

        Issue actions

          proposal: builtin: delete returns bool · Issue #41130 · golang/go