Skip to content
This repository was archived by the owner on May 4, 2019. It is now read-only.

Commit 5dcbb52

Browse files
committed
Improve efficiency of null checking for generic case
In my benchmarks, this approach is just as fast as manually repeating isnull(x) | isnull(y)...
1 parent ebca424 commit 5dcbb52

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/broadcast.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ eltypes() = Tuple{}
6262
eltypes(x) = Tuple{eltype(x)}
6363
eltypes(x, xs...) = Tuple{eltype(x), eltypes(xs...).parameters...}
6464

65+
hasnulls(x) = isnull(x)
66+
hasnulls(x, xs...) = hasnulls(x) | hasnulls(xs...)
67+
6568
"""
6669
broadcast_lift(f, xs...)
6770
@@ -71,14 +74,10 @@ return `f` applied to values of `xs`.
7174
"""
7275
@inline function broadcast_lift(f, xs...)
7376
if null_safe_op(f, eltypes(xs).parameters...)
74-
# TODO: find a more efficient approach than mapreduce
75-
# (i.e. one which gets lowered to just isnull(x1) | isnull(x2) | ...)
76-
return @compat Nullable(f(unsafe_get.(xs)...), !mapreduce(isnull, |, xs))
77+
return @compat Nullable(f(unsafe_get.(xs)...), !hasnulls(xs...))
7778
else
78-
U = Core.Inference.return_type(f, eltypes(xs...))
79-
# TODO: find a more efficient approach than mapreduce
80-
# (i.e. one which gets lowered to just isnull(x1) | isnull(x2) | ...)
81-
if mapreduce(isnull, |, xs)
79+
U = Core.Inference.return_type(f, eltypes(xs))
80+
if hasnulls(xs...)
8281
return Nullable{U}()
8382
else
8483
return Nullable(f(map(unsafe_get, xs)...))

0 commit comments

Comments
 (0)