Skip to content

Add option to preserve capacity when empty! is called on IdDict - #51102

Open
Zentrik wants to merge 1 commit into
JuliaLang:masterfrom
Zentrik:empty!-iddict
Open

Zentrik wants to merge 1 commit into
JuliaLang:masterfrom
Zentrik:empty!-iddict

Conversation

@Zentrik

@Zentrik Zentrik commented Aug 29, 2023

Copy link
Copy Markdown
Member

I couldn't find a function for just emptying an IdDict without resizing it so I just added that as an option.

@KristofferC

Copy link
Copy Markdown
Member

Unless this is part of the generic empty! function and is only applicable to IdDict I think it is better for this to be a separate function.

@Tokazama

Copy link
Copy Markdown
Contributor

This should also apply to Dict

@Zentrik

Zentrik commented Aug 30, 2023

Copy link
Copy Markdown
Member Author

Interestingly, it seems empty!(a::Vector) doesn't resize

julia> b = collect(1:10^4);

julia> empty!(b)
Int64[]

julia> unsafe_load(convert(Ptr{Csize_t}, pointer_from_objref(b)+8*2+2+2+4+8)) |> Int
10000

Not sure why Dict and IdDict do in contrast.

Do you have any suggestions for a name, I was thinking zero!.

@Tokazama

Copy link
Copy Markdown
Contributor

I assumed differences are due to how dictionaries resize using different approaches. I can't think of any collection that uses 'empty!' that wouldn't benefit from this extra argument. Not sure why it needs different name.

@vtjnash

vtjnash commented Aug 30, 2023 •

Copy link
Copy Markdown
Member

I think the observation was that an oversized Dict performs worse in real-world conditions than one that is resize! to fit the current contents. That doesn't apply to an oversized Vector.

@vtjnash

vtjnash commented Aug 30, 2023

Copy link
Copy Markdown
Member

c.f. e62a7db for measurements

@Tokazama

Copy link
Copy Markdown
Contributor

I'm always for improving memory management tools but I'm curious what scenario one deletes all keys and values only to replace them with the same number of unique key value pairs

@Zentrik

Zentrik commented Aug 31, 2023

Copy link
Copy Markdown
Member Author

It's useful in #51120, essentially I have an IdDict which has a known max size and needs to be reset on every iteration.
So I want it to be preallocated for the max size to prevent rehashing on every iteration. So I want to empty it whilst persevering the size.

@brenhinkeller brenhinkeller added the collections Data structures holding multiple items, e.g. sets label Sep 1, 2023

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

collections Data structures holding multiple items, e.g. sets

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants