-
Notifications
You must be signed in to change notification settings - Fork 28
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 setwith!
#117
Add setwith!
#117
Conversation
Ping @andyferris :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@theogf very sorry I let this linger - life is too full!
I like the idea here, and the name is good, however one thing (and we see this sometimes in Base
functions too) is that setwith!
doesn't work well with dictionaries that contain nothing
values. Note that the tokensetwith!
and setwith!
actually behave differently when the existing value is nothing
.
I suspect that the tokensetwith!
implementation needs @inbounds
around the settokenvalue!
statements to improve speed. Also the condtional could be moved inside the function call so only the value differs. Also we can make a second method for safe_convert
in the case no conversion is necessary, to make it a no-op as it currently wastes time with isequal
etc. (In any case it's a massive speed improvement on the mergewith!
method, right?)
One more thing I'm thinking of here, is what to do in the case you want to insert a new mutable object in the "default" case, like an empty vector that you will For |
So I guess I wonder if something like this would work ok? function setwith!(f, d::AbstractDictionary{I}, i, value) where {I}
i2 = safe_convert(I, i)
(had_token, token) = gettoken!(d, i2)
settoken!(d, token, had_token ? f(gettokenvalue(d, token), value) : f(value))
return d
end (Also, note the usage of |
Hmm, I see now that the original So probably the remaining work is to use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay - now we are using tokens, and I think this function makes sense the way you have it.
I'll note here for posterity that at some point in the future Base
might have a function like update!
or modify
with similar behavior, and we might need to adjust the name to suit. But the current name works extremely well with set!
so its OK to roll with it.
8747880
to
7f7c65b
Compare
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #117 +/- ##
==========================================
+ Coverage 80.12% 80.40% +0.28%
==========================================
Files 20 20
Lines 2360 2358 -2
==========================================
+ Hits 1891 1896 +5
+ Misses 469 462 -7 ☔ View full report in Codecov by Sentry. |
And just to check the benchmark at the top:
|
Thanks again @theogf - only took me a year... |
This PR proposes a new feature which is an equivalent of
mergewith!
with aDictionary
containing only one element.This is much faster than building a
Dictionary
every time.I did not use
token
as theget
approach was faster.Here are some benchmarks:
Additionaly I started to use
safe_convert
instead of converting and equality checking to avoid code duplication,