Type seperation & code bloat: dispatch vs compile type.

134 views
Skip to first unread message

ma.laf...@gmail.com

unread,
Jun 14, 2016, 9:54:16 AM6/14/16
to julia-dev
It would be very nice to be able to seperate the dispatch function signature from the compiled type, for example:

#myfunction(x::DispatchType::CompileType) = ...
f
(x::Real::Float64) = x^50

Which would basically translate to the following:
f(x::Float64) = x^50 #Main function
f
(x::Real) = f(convert(Float64, x)) #Dispatch wrapper

Why?
  1. Using concrete types reduces the number of possible "run paths" (**see below) for a given algorihm (reduces required # of tests to guarantee correctness).
  2. Potentially reduces code bloat from compiling many specializations (vs only making the dispatch signature generic).
  3. Less code bloat (potentially) = fewer cache misses + more efficiency.
  4. Less verbose than manual implementation of (function + dispatch wrapper).

** The following is an example of a having unexpected "run paths":
function runalgorithm(x::Real)
   
...
    y50
= x^50 #Oops... Int(5)^50 = -6776596920136667815
   
...
    y50_again
= x^50.0 #Computed correctly: 8.881784197001253e34
end

A string example

The proposed mechanism would also be good with another source of annoyance with file system strings:

You would think specifying file paths with `UTF8String` would be ideal for your function signatures:
typealias FileSystemString UTF8String

function dosomething(path::FileSystemString)
   
...
end

...but then users can not make direct calls with `ASCIIString`s:
#Will not work - not UTF8String:
dosomething
("/home/user/myfile.txt")

So instead of going through the trouble of creating a generic dispatch wrapper function, I usually just end up making an overly generic implementation (succeptible to code bloat):
#Julia will create specializations for each concrete subtype:
function dosomething(path::AbstractString)
   
...
end

Yichao Yu

unread,
Jun 14, 2016, 10:25:18 AM6/14/16
to Julia Dev

ma.laf...@gmail.com

unread,
Jun 14, 2016, 12:27:36 PM6/14/16
to julia-dev
Excellent.  Thanks for the heads up!
Reply all
Reply to author
Forward
0 new messages