Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sizehint! when creating Dict from Generator #36334

Closed
nacnudus opened this issue Jun 17, 2020 · 4 comments · Fixed by #35254
Closed

Use sizehint! when creating Dict from Generator #36334

nacnudus opened this issue Jun 17, 2020 · 4 comments · Fixed by #35254
Labels
domain:collections Data structures holding multiple items, e.g. sets performance Must go faster

Comments

@nacnudus
Copy link

Dictionaries can be created from generator expressions, but don't seem to use sizehint!. This could give better performance.

I couldn't confirm this in the source code, so I asked on discourse where a benchmark showed fewer allocations when manually calling sizehint! than when using a generator expression.

using BenchmarkTools

println("Implicit:")
@btime begin
    dict = Dict(string(i) => sind(i) for i = 0:5:360)
end

println("Explicit:")
@btime begin
    dict = Dict{String, Int}()
    sizehint!(dict, 360÷5)
    for i in 0:5:360
        dict[string(i)] = i
    end
end

Results in

Implicit:
  8.669 μs (156 allocations: 13.42 KiB)
Explicit:
  5.737 μs (153 allocations: 9.70 KiB)
@KristofferC
Copy link
Sponsor Member

KristofferC commented Jun 17, 2020

If you want to compare the effect of the sizehint! shouldn't you just measure your explicit one but without the sizehint! call? Otherwise, other differences in the constructor for dictionaires from generators will pollute the result.

@harryscholes
Copy link
Contributor

Dup of #35254?

@rfourquet rfourquet added the domain:collections Data structures holding multiple items, e.g. sets label Jun 18, 2020
@KristofferC
Copy link
Sponsor Member

Seems so, yes.

@rfourquet
Copy link
Member

I mean, not really, as this is an issue, which that PR can close! it's always nice for a PR author to close an issue, so I suggest to keep this open, now that it has been created :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:collections Data structures holding multiple items, e.g. sets performance Must go faster
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants