https://github.com/JuliaLang/julia
Revision d88d5cddacdc96c1eb13f12c9c3541e5f2908e02 authored by Petr Vana on 10 November 2023, 14:30:10 UTC, committed by GitHub on 10 November 2023, 14:30:10 UTC
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>
1 parent 42c088b
History
Tip revision: d88d5cddacdc96c1eb13f12c9c3541e5f2908e02 authored by Petr Vana on 10 November 2023, 14:30:10 UTC
Optimize `pop!(s::Set, x, default)` (#52023)
Tip revision: d88d5cd
File Mode Size
.devcontainer
.github
base
cli
contrib
deps
doc
etc
src
stdlib
test
.buildkite-external-version -rw-r--r-- 5 bytes
.clang-format -rw-r--r-- 3.3 KB
.clangd -rw-r--r-- 114 bytes
.codecov.yml -rw-r--r-- 52 bytes
.git-blame-ignore-revs -rw-r--r-- 371 bytes
.gitattributes -rw-r--r-- 65 bytes
.gitignore -rw-r--r-- 523 bytes
.mailmap -rw-r--r-- 12.7 KB
CITATION.bib -rw-r--r-- 513 bytes
CITATION.cff -rw-r--r-- 940 bytes
CONTRIBUTING.md -rw-r--r-- 23.4 KB
HISTORY.md -rw-r--r-- 372.8 KB
LICENSE.md -rw-r--r-- 1.3 KB
Make.inc -rw-r--r-- 55.9 KB
Makefile -rw-r--r-- 30.3 KB
NEWS.md -rw-r--r-- 5.1 KB
README.md -rw-r--r-- 7.3 KB
THIRDPARTY.md -rw-r--r-- 3.8 KB
VERSION -rw-r--r-- 11 bytes
julia.spdx.json -rw-r--r-- 35.8 KB
pkgimage.mk -rw-r--r-- 6.1 KB
sysimage.mk -rw-r--r-- 4.2 KB
typos.toml -rw-r--r-- 78 bytes

README.md

back to top