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

Add WeakKeyIdDict #1419

Merged
merged 3 commits into from
Sep 12, 2023
Merged

Add WeakKeyIdDict #1419

merged 3 commits into from
Sep 12, 2023

Conversation

fingolfin
Copy link
Member

  • Added data-structure WeakKeyIdDict
  • Sync WeakKeyIdDict with latest Julia WeakKeyDict implementation
  • Fix some import issues in WeakValueDict

See oscar-system/Oscar.jl#1872

CC @mauro3

@codecov
Copy link

codecov bot commented Sep 8, 2023

Codecov Report

Merging #1419 (f33f5de) into master (a655aee) will decrease coverage by 1.05%.
Report is 3 commits behind head on master.
The diff coverage is 98.52%.

@@            Coverage Diff             @@
##           master    #1419      +/-   ##
==========================================
- Coverage   85.62%   84.58%   -1.05%     
==========================================
  Files         107      109       +2     
  Lines       28760    29345     +585     
==========================================
+ Hits        24627    24822     +195     
- Misses       4133     4523     +390     
Files Changed Coverage Δ
src/AbstractAlgebra.jl 56.25% <ø> (ø)
src/WeakKeyIdDict.jl 98.52% <98.52%> (ø)

... and 21 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@thofma
Copy link
Member

thofma commented Sep 11, 2023

Looks good. Do you want to bump up that coverage (at the moment it is at 50%)?

@fingolfin
Copy link
Member Author

Good point -- I only copied the existing tests, but of course syncing with master.

So I've added some tests, and of course this uncovered various bugs 😂 . I've also tried to add a test for the GC collecting the weak refs, but I can't get it to reliably work, so it is for now disabled.

I'll wait for the updated coverage report to see where else I can add some more.

@fingolfin fingolfin force-pushed the mh/WeakKeyIdDict branch 3 times, most recently from 8b4146d to 5656ea9 Compare September 12, 2023 10:11
Comment on lines 133 to 157
# The following tests involve the garbage collector and
# don't work if they are inside a @testset
wkd = WeakKeyIdDict([42]=>2, [43]=>3, [44]=>4)
for k in keys(wkd)
delete!(wkd, k)
end
@test isempty(wkd)
GC.gc() # try to get it to evict some weak references
@test isempty(wkd)

# verify that garbage collection takes care of our weak references
A = [1]
wkd = WeakKeyIdDict(A => 1)
let tmp = [ 42 ]
@test length(wkd) == 1
wkd[tmp] = 2
@test length(wkd) == 2
end
# at this point there is no strong reference left to the vector [42]
# previously reachable via tmp
GC.gc(true)

@test length(wkd) == 1
@test length(keys(wkd)) == 1
@test WeakKeyIdDict(A => 1) == wkd
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming the CI tests pass now, I am fine with how high the coverage now is (> 90%). Note this ugliness: I could not find a way to get those tests which interact with the GC to work "right" while inside a @testset. But I am not aware of a serious drawback of the above, other than it not being reported in the output of Pkg.test("AbstractAlgbera") ... ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work if you put it into a let scope? Currently, CI fails as there are two global objects both named A in the tests.

@fingolfin fingolfin merged commit c390a51 into master Sep 12, 2023
13 of 15 checks passed
@fingolfin fingolfin deleted the mh/WeakKeyIdDict branch September 12, 2023 22:19
@ericphanson
Copy link

I wonder if WeakKeyIdDict could do in a separate WeakKeyIdDicts.jl package, so it could be reused elsewhere easier?

@fingolfin
Copy link
Member Author

Feel free to create such a package, and also to copy the code from here (it is open source after all, and heavily based on the Julia stdlib code anyway, as well as code written by others).

@ericphanson
Copy link

BTW @haberdashPI has created this package here: https://github.com/beacon-biosignals/WeakKeyIdDicts.jl

@fingolfin fingolfin mentioned this pull request Apr 12, 2024
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.

5 participants