-
Notifications
You must be signed in to change notification settings - Fork 81
Remove vertex_index(::Integer), make special case for SimpleGraph #135
Conversation
Thanks @sbromberger Fixes #131 Fixes #127 Closes #133
Implements the suggestion by @lindahua, so will merge soon unless there are any objections. |
@@ -37,7 +35,10 @@ typealias ProvidedVertexType Union(Integer, KeyVertex, ExVertex) | |||
|
|||
function vertex_index{V}(v::V, g::AbstractGraph{V}) | |||
@graph_requires g vertex_list | |||
return vertex_index(v, vertices(g)) | |||
if applicable(vertex_index, v) | |||
return vertex_index(v) |
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.
Since applicable
makes for more complex generated code and there's only one case where it's true, perhaps just writing out two separate methods instead? (I think it's clearer and no increase in lines of code, too.)
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.
This would make it impossible to extend KeyVertex
. Is the code generation actually bad for this?
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.
Double checked that it did not specialize how you'd hope (let me know if I did something wrong):
immutable A
int::Int
end
immutable B
int::Int
end
g(x) = x*x
f(::A, x::Int) = 2x
f(b::B) = b.int
a_only(a::A, x::Int) = f(a, g(x))
b_only(b::B, x::Int) = f(b)
function a_or_b{T}(v::T, x::Int)
if applicable(f, v)
return f(v)
end
return f(v, g(x))
end
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.
I don't know what constitutes bad -- it (and method_exists
) just doesn't specialize like I expected the first time I used it:
Using that sample code above:
code_native(a_or_b2, (A, Int))
.section __TEXT,__text,regular,pure_instructions
Filename: none
Source line: 2
push RBP
mov RBP, RSP
push R15
push R14
push R12
push RBX
sub RSP, 32
mov RBX, RSI
mov R14, RDI
mov QWORD PTR [RBP - 64], 4
Source line: 2
movabs R12, jl_pgcstack
mov RAX, QWORD PTR [R12]
mov QWORD PTR [RBP - 56], RAX
lea RAX, QWORD PTR [RBP - 64]
mov QWORD PTR [R12], RAX
vxorps XMM0, XMM0, XMM0
vmovups XMMWORD PTR [RBP - 48], XMM0
movabs RAX, 140405752947744
Source line: 2
mov RDI, QWORD PTR [RAX]
mov RAX, QWORD PTR [RDI + 8]
movabs RCX, 140405770133600
mov RCX, QWORD PTR [RCX]
Source line: 2
lea R15, QWORD PTR [RBP - 48]
movabs RDX, 140405903671888
Source line: 2
mov QWORD PTR [RBP - 48], RCX
mov QWORD PTR [RBP - 40], RDX
mov RSI, R15
mov EDX, 2
call RAX
movabs RCX, 140405753013296
cmp RAX, RCX
je L218
Source line: 3
movabs RAX, allocobj
mov EDI, 16
call RAX
movabs RCX, 140405864207744
mov QWORD PTR [RAX], RCX
mov QWORD PTR [RAX + 8], R14
mov QWORD PTR [RBP - 48], RAX
movabs RAX, jl_apply_generic
movabs RDI, 140405793447904
mov RSI, R15
mov EDX, 1
call RAX
jmpq L240
Source line: 5
L218: imul RBX, RBX
add RBX, RBX
movabs RAX, jl_box_int64
mov RDI, RBX
call RAX
L240: mov RCX, QWORD PTR [RBP - 56]
mov QWORD PTR [R12], RCX
add RSP, 32
pop RBX
pop R12
pop R14
pop R15
pop RBP
ret
julia> code_native(a_only, (A, Int))
.section __TEXT,__text,regular,pure_instructions
Filename: none
Source line: 1
push RBP
mov RBP, RSP
Source line: 1
imul RSI, RSI
lea RAX, QWORD PTR [RSI + RSI]
pop RBP
ret
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.
I'll add a specialization for KeyVertex
and leave this as it is for extensibility. The slow case where we have to do a linear search on indices is already slow and shouldn't be affected by this.
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.
Cool, makes sense to me.
Remove vertex_index(::Integer), make special case for SimpleGraph
Thanks @sbromberger
Fixes #131
Fixes #127
Closes #133