Giter Club home page Giter Club logo

dispatchedtuples.jl's Introduction

DispatchedTuples.jl

Docs Build docs build
Documentation dev
GHA CI gha ci
Code Coverage codecov
Bors enabled bors

What are dispatched tuples?

DispatchedTuples are like immutable dictionaries (so, they're technically more like NamedTuples) except that the keys are instances of types. Also, because DispatchedTuples are backed by tuples, they are GPU-friendly.

There are two kinds of DispatchedTuples with different behavior:

┌────────────────────────────────────┬───────────────────────────┬────────────────────┐
│                       Return value │           DispatchedTuple │ DispatchedSet      │
│                                    │ (non-unique keys allowed) │ (unique keys only) │
├────────────────────────────────────┼───────────────────────────┼────────────────────┤
│                               Type │                     Tuple │              Value │
│ Unregistered key (without default) │                        () │              error │
│    Unregistered key (with default) │                (default,) │            default │
│                    Duplicative key │     all registered values │          one value │
└────────────────────────────────────┴───────────────────────────┴────────────────────┘

Creating and using dispatched tuples

DispatchedTuple and DispatchedSets have three constructors:

  1. A variable number of (vararg) Pairs + keyword default
  2. A Tuple of Pairs + positional default
  3. A Tuple of 2-element Tuples (the first element being the "key", and the second the "value") + positional default

The first field of the Pair (the "key") is an instance of the type you want to dispatch on. The second field of the Pair is the quantity (the "value", which can be anything) returned by dtup[key].

A default value, if passed to DispatchedTuple and DispatchedSet, is returned for any unrecognized keys as shown in the table above.

Example

Here is an example in action

julia> using DispatchedTuples

julia> struct Foo end;

julia> struct Bar end;

julia> struct Baz end;

julia> dtup = DispatchedTuple((
                Pair(Foo(), 1),
                Pair(Foo(), 2),
                Pair(Bar(), 3),
              ))
DispatchedTuple with 3 entries:
  Foo() => 1
  Foo() => 2
  Bar() => 3
  default => ()


julia> dtup[Foo()]
(1, 2)

julia> dtup[Bar()]
(3,)

julia> dtup[Baz()]
()

dispatchedtuples.jl's People

Contributors

ali-ramadhan avatar bors[bot] avatar charleskawczynski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ali-ramadhan

dispatchedtuples.jl's Issues

Alternative constructor that accepts multiple pairs as varargs?

The first thing I tried (because I didn't read the README carefully enough) was

dt = DispatchedTuple(Center() => "c", Face() => "f")

which didn't work because the input needs to be a tuple of pairs.

Would it helpful to users to define a constructor like

julia> DispatchedTuple(multiple_pairs...) = DispatchedTuple(Tuple(multiple_pairs))

so that the initial example works:

julia> dt = DispatchedTuple(Center() => "c", Face() => "f")
DispatchedTuple{Tuple{Tuple{Center, String}, Tuple{Face, String}}, DispatchedTuples.NoDefaults}(((Center(), "c"), (Face(), "f")), DispatchedTuples.NoDefaults())

Show conciseness of generated code in README/docs?

Seems like other packages like to show off how short the generated LLVM or assembly code is to show that the code generated is performant. Is it worth adding something similar for DispatchedTuples.jl?

I'm not too familiar with @generated so I played around to see if the generated code was indeed concise. Looks like it is!

DispatchedSet

julia> ds = DispatchedSet((Center() => "c", Face() => :f))
DispatchedSet{Tuple{Tuple{Center, String}, Tuple{Face, Symbol}}, DispatchedTuples.NoDefaults}(((Center(), "c"), (Face(), :f)), DispatchedTuples.NoDefaults())

julia> @code_native dispatch(ds, Face())
        .text
; ┌ @ DispatchedTuples.jl:147 within `dispatch'
; │┌ @ DispatchedTuples.jl within `macro expansion'
; ││┌ @ tuple.jl:29 within `getindex'
        movq    8(%rdi), %rax
; ││└
        retq
        nopw    %cs:(%rax,%rax)
; └└

julia> @code_native dispatch(ds, Center())
        .text
; ┌ @ DispatchedTuples.jl:147 within `dispatch'
; │┌ @ DispatchedTuples.jl within `macro expansion'
; ││┌ @ tuple.jl:29 within `getindex'
        movq    (%rdi), %rax
; ││└
        retq
        nopw    %cs:(%rax,%rax)
; └└

DispatchedTuple

julia> dt = DispatchedTuple((Center() => "c", Face() => "f", Center() => :c, Face() => :f))
DispatchedTuple{Tuple{Tuple{Center, String}, Tuple{Face, String}, Tuple{Center, Symbol}, Tuple{Face, Symbol}}, DispatchedTuples.NoDefaults}(((Center(), "c"), (Face(), "f"), (Center(), :c), (Face(), :f)), DispatchedTuples.NoDefaults())

julia> @code_native dispatch(dt, Center())
        .text
; ┌ @ DispatchedTuples.jl:107 within `dispatch'
        movq    %rdi, %rax
; │┌ @ DispatchedTuples.jl:24 within `macro expansion'
; ││┌ @ tuple.jl:29 within `getindex'
        movq    (%rsi), %rcx
        movq    16(%rsi), %rdx
; ││└
        movq    %rcx, (%rdi)
        movq    %rdx, 8(%rdi)
        retq
        nopw    %cs:(%rax,%rax)
; └└

julia> @code_native dispatch(dt, Face())
        .text
; ┌ @ DispatchedTuples.jl:107 within `dispatch'
        movq    %rdi, %rax
; │┌ @ DispatchedTuples.jl:24 within `macro expansion'
; ││┌ @ tuple.jl:29 within `getindex'
        movq    8(%rsi), %rcx
        movq    24(%rsi), %rdx
; ││└
        movq    %rcx, (%rdi)
        movq    %rdx, 8(%rdi)
        retq
        nopw    %cs:(%rax,%rax)
; └└

Improve storage

Right now, when passing a tuple to DispatchedTuple/DispatchedSet, we store the entire tuple, but since DispatchedTuple kind of compresses the values (into a single tuple), we could compress the storage of the keys.

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

Why does indexing a `DispatchedTuple` always return a tuple?

I noticed that indexing a DispatchedTuple always returns a tuple:

julia> dispatch(dt, Center())
("c",)

which I thought might introduce lots of [1] boilerplate if I used it since I usually want the contents of the 1-tuple

julia> dispatch(dt, Center())[1]
"c"

but maybe this is because I'm focusing on the case where each object maps to just one value.

Is it always returning a tuple because you want the type of the output to always be <:Tuple? I guess returning [1] in the case of a 1-tuple might introduce type instability?

Define `getindex` for `DispatchedTuple`?

I guess ignoring the README I was kind of hoping getindex would just work.

julia> dt = DispatchedTuple((Center() => "c", Face() => "f"))
DispatchedTuple{Tuple{Tuple{Center, String}, Tuple{Face, String}}, DispatchedTuples.NoDefaults}(((Center(), "c"), (Face(), "f")), DispatchedTuples.NoDefaults())

julia> dt[Center()]
ERROR: MethodError: no method matching getindex(::DispatchedTuple{Tuple{Tuple{Center, String}, Tuple{Face, String}}, DispatchedTuples.NoDefaults}, ::Center)
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

Turns out it's just a one-liner!

julia> import Base: getindex

julia> getindex(dt::DispatchedTuple, e) = dispatch(dt, e)
getindex (generic function with 372 methods)

julia> dt[Center()]
("c",)

julia> dt[Face()]
("f",)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.