Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
cscherrer authored May 24, 2021
1 parent c623693 commit 267be6f
Showing 1 changed file with 2 additions and 68 deletions.
70 changes: 2 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,72 +94,6 @@ Foo{(:μ, :σ), Tuple{Int64, Int64}}((μ = 4, σ = 2))

In [MeasureTheory.jl](https://github.com/cscherrer/MeasureTheory.jl), we use this approach to allow multiple parameterizations of a given distribution.

## Related Packages


## Limitations

KeywordCalls tries to push as much of the work as possible to the compiler, to make repeated run-time calls fast. But there's no free lunch, you either pay now or pay later.

If you'd rather avoid the compilation time (at the cost of some runtime overhead), you might try [KeywordDispatch.jl](https://github.com/simonbyrne/KeywordDispatch.jl).

## Benchmarks

Let's define a method for each "alphabet prefix":
```julia
letters = Symbol.('a':'z')

for n in 1:26
fkeys = Tuple(letters[1:n])

@eval begin
f(nt::NamedTuple{$fkeys}) = sum(values(nt))
$(KeywordCalls._kwcall(:(f($(fkeys...)))))
end
end
```

So now `f`'s methods look like this:
```julia
julia> methods(f)
# 28 methods for generic function "f":
[1] f(; kwargs...) in Main at /home/chad/git/KeywordCalls/src/KeywordCalls.jl:52
[2] f(nt::NamedTuple{(:a, :b, :c, :d, :e, :f, :g), T} where T<:Tuple) in Main at REPL[3]:5
[3] f(nt::NamedTuple{(:a, :b, :c, :d, :e, :f), T} where T<:Tuple) in Main at REPL[3]:5
[4] f(nt::NamedTuple{(:a, :b, :c, :d, :e), T} where T<:Tuple) in Main at REPL[3]:5
[5] f(nt::NamedTuple{(:a, :b, :c, :d), T} where T<:Tuple) in Main at REPL[3]:5
[6] f(nt::NamedTuple{(:a, :b, :c), T} where T<:Tuple) in Main at REPL[3]:5
[7] f(nt::NamedTuple{(:a, :b), T} where T<:Tuple) in Main at REPL[3]:5
[8] f(nt::NamedTuple{(:a,), T} where T<:Tuple) in Main at REPL[3]:5
[9] f(nt::NamedTuple{(:a, :b, :c, :d, :e, :f, :g, :h), T} where T<:Tuple) in Main at REPL[3]:5
[10] f(nt::NamedTuple{(:a, :b, :c, :d, :e, :f, :g, :h, :i), T} where T<:Tuple) in Main at REPL[3]:5
[26] f(nt::NamedTuple{(:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o, :p, :q, :r, :s, :t, :u, :v, :w, :x, :y), T} where T<:Tuple) in Main at REPL[3]:5
[27] f(nt::NamedTuple{(:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o, :p, :q, :r, :s, :t, :u, :v, :w, :x, :y, :z), T} where T<:Tuple) in Main at REPL[3]:5
[28] f(nt::NamedTuple) in Main at /home/chad/git/KeywordCalls/src/KeywordCalls.jl:50
```

That method 28 is the dispatch that requires permutation; it's called for any named tuple without an explicit method.

Now we can benchmark:
```julia
function runbenchmark()
times = Matrix{Float64}(undef, 26,2)
for n in 1:26
fkeys = Tuple(letters[1:n])
rkeys = reverse(fkeys)

nt = NamedTuple{fkeys}(1:n)
rnt = NamedTuple{rkeys}(n:-1:1)

times[n,1] = @belapsed($f($nt))
times[n,2] = @belapsed($f($rnt))
end
return times
end

times = runbenchmark()
```

Here's the result:

![benchmarks](https://user-images.githubusercontent.com/1184449/116616679-d2abef00-a8f1-11eb-9507-0af267fa37cb.png)
[KeywordDispatch.jl](https://github.com/simonbyrne/KeywordDispatch.jl) is very similar. When we started KeywordCalls, it seemed we would need lots of extra dependencies to make the idea work. This motivated creating a new package instead of making a PR for KeywordDispatch. @simeonschaub helped us get away from this and simplify the implementation; it's now very light-weight, and very fast.

2 comments on commit 267be6f

@cscherrer
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/37379

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.6 -m "<description of version>" 267be6fd52bf4e3026b6212a6628623ca8ae14c1
git push origin v0.1.6

Please sign in to comment.