Skip to content

Conversation

@jakobnissen
Copy link
Member

@jakobnissen jakobnissen commented Nov 3, 2023

Previously, pop!(::Set, x) returned x, not the element in the set.This matters if multiple different elements are equal and hash to the same.

I believe this might have been an oversight. It was not tested for, and it makes most sense to me that pop! returns the element actually in the set.

Before:

julia> pop!(Set([1]), 0x01)
0x01

Now:

julia> pop!(Set([1]), 0x01)
1

Previously, `pop!(::Set, x)` returned `x`, not the element in the set.
This matters if multiple different elements are equal and hash to the same.
Before:
```julia
julia> pop!(Set([1]), 0x01)
0x01
```
Now:
```julia
julia> pop!(Set([1]), 0x01)
1
```
@jakobnissen
Copy link
Member Author

CI failures appear unrelated.

@petvana
Copy link
Member

petvana commented Nov 5, 2023

Would pkgeval worth it?

@vtjnash vtjnash added the merge me PR is reviewed. Merge when all tests are passing label Nov 5, 2023
@vtjnash vtjnash merged commit 6f6419c into JuliaLang:master Nov 8, 2023
@jakobnissen jakobnissen deleted the setpop branch November 8, 2023 16:01
@giordano giordano removed the merge me PR is reviewed. Merge when all tests are passing label Nov 8, 2023
oscardssmith pushed a commit that referenced this pull request Nov 10, 2023
Minor optimization to compute index in `Dict` only once.

This PR should not be merged before #52017.

**Master**
``` julia
  126.417 μs (1 allocation: 16 bytes)
  147.812 μs (1 allocation: 16 bytes)
```

**PR**
``` julia
  86.494 μs (1 allocation: 16 bytes)
  156.912 μs (1 allocation: 16 bytes)
```

<details>
<summary><b><u>Testing code</u></b></summary>

``` julia
using BenchmarkTools

function PR_pop!(s::Set, x, default)
    dict = s.dict
    index = Base.ht_keyindex(dict, x)
    if index > 0
        @inbounds key = dict.keys[index]
        Base._delete!(dict, index)
        return key
    else
        return default
    end
end

N = 10000
x = collect(1:N)
x_negative = collect(-N:-1)

function pop_all(s, x)
    for v in x
        pop!(s, v, -1)
    end
end

function pop_all_PR(s, x)
    for v in x
        PR_pop!(s, v, -1)
    end
end

# Master
@Btime pop_all(s, x) setup=(s=Set(x))
@Btime pop_all(s, x_negative) setup=(s=Set(x))

# PR
@Btime pop_all_PR(s, x) setup=(s=Set(x))
@Btime pop_all_PR(s, x_negative) setup=(s=Set(x))
```
</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants