Skip to content

Commit c3e8dff

Browse files
authored
Merge pull request #162 from andyferris/fix-inbounds-in-map
Fix usage of `@inbounds` in `map!`
2 parents 59b4ea6 + 46491ad commit c3e8dff

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

src/map.jl

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@
22

33
function Base.map!(f, out::AbstractDictionary, d::AbstractDictionary, d2::AbstractDictionary, ds::AbstractDictionary...)
44
if sharetokens(out, d, d2, ds...)
5-
@inbounds for t in tokens(out)
6-
settokenvalue!(out, t, f(gettokenvalue(d, t), gettokenvalue(d2, t), map(x -> @inbounds(gettokenvalue(x, t)), ds)...))
5+
for t in tokens(out)
6+
y = f(@inbounds(gettokenvalue(d, t)), @inbounds(gettokenvalue(d2, t)), map(x -> @inbounds(gettokenvalue(x, t)), ds)...)
7+
@inbounds settokenvalue!(out, t, y)
78
end
89
elseif istokenizable(out)
910
@boundscheck if !isequal(keys(out), keys(d)) || !isequal(keys(out), keys(d2)) || any(dict -> !isequal(keys(out), keys(dict)), ds)
1011
throw(IndexError("Indices do not match"))
1112
end
12-
@inbounds for txs in zip(tokens(out), d, d2, ds...)
13-
t = txs[1]
14-
xs = Base.tail(txs)
15-
settokenvalue!(out, t, f(xs...))
13+
for txs in zip(tokens(out), d, d2, ds...)
14+
t = @inbounds txs[1]
15+
xs = @inbounds Base.tail(txs)
16+
y = f(xs...)
17+
@inbounds settokenvalue!(out, t, y)
1618
end
1719
else
1820
@boundscheck if !isequal(keys(out), keys(d)) || !isequal(keys(out), keys(d2)) || any(dict -> !isequal(keys(out), keys(dict)), ds)
1921
throw(IndexError("Indices do not match"))
2022
end
21-
@inbounds for ixs in zip(keys(out), d, d2, ds...)
22-
i = ixs[1]
23-
xs = Base.tail(ixs)
24-
out[i] = f(xs...)
23+
for ixs in zip(keys(out), d, d2, ds...)
24+
i = @inbounds ixs[1]
25+
xs = @inbounds Base.tail(ixs)
26+
y = f(xs...)
27+
@inbounds out[i] = y
2528
end
2629
end
2730
return out
@@ -30,58 +33,69 @@ end
3033
# (avoid an _apply for the two-input case)
3134
function Base.map!(f, out::AbstractDictionary, d::AbstractDictionary, d2::AbstractDictionary)
3235
if sharetokens(out, d, d2)
33-
@inbounds for t in tokens(out)
34-
settokenvalue!(out, t, f(gettokenvalue(d, t), gettokenvalue(d2, t)))
36+
for t in tokens(out)
37+
x = @inbounds gettokenvalue(d, t)
38+
x2 = @inbounds gettokenvalue(d2, t)
39+
y = f(x, x2)
40+
@inbounds settokenvalue!(out, t, y)
3541
end
3642
elseif istokenizable(out)
3743
@boundscheck if !isequal(keys(out), keys(d)) || !isequal(keys(out), keys(d2))
3844
throw(IndexError("Indices do not match"))
3945
end
40-
@inbounds for (t, x, x2) in zip(tokens(out), d, d2)
41-
settokenvalue!(out, t, f(x, x2))
46+
for (t, x, x2) in zip(tokens(out), d, d2)
47+
y = f(x, x2)
48+
@inbounds settokenvalue!(out, t, y)
4249
end
4350
else
4451
@boundscheck if !isequal(keys(out), keys(d)) || !isequal(keys(out), keys(d2))
4552
throw(IndexError("Indices do not match"))
4653
end
47-
@inbounds for i in keys(out)
48-
out[i] = f(d[i], d2[i])
54+
for i in keys(out)
55+
y = f(@inbounds(d[i]), @inbounds(d2[i]))
56+
@inbounds out[i] = y
4957
end
5058
end
5159
return out
5260
end
5361

5462
function Base.map!(f, out::AbstractDictionary, d::AbstractDictionary)
5563
if sharetokens(out, d)
56-
@inbounds for t in tokens(out)
57-
settokenvalue!(out, t, f(gettokenvalue(d, t)))
64+
for t in tokens(out)
65+
x = @inbounds gettokenvalue(d, t)
66+
y = f(x)
67+
@inbounds settokenvalue!(out, t, y)
5868
end
5969
elseif istokenizable(out)
6070
@boundscheck if !isequal(keys(out), keys(d))
6171
throw(IndexError("Indices do not match"))
6272
end
63-
@inbounds for (t, x) in zip(tokens(out), d)
64-
settokenvalue!(out, t, f(x))
73+
for (t, x) in zip(tokens(out), d)
74+
y = f(x)
75+
@inbounds settokenvalue!(out, t, y)
6576
end
6677
else
6778
@boundscheck if !isequal(keys(out), keys(d))
6879
throw(IndexError("Indices do not match"))
6980
end
70-
@inbounds for (i, x) in zip(keys(out), d)
71-
out[i] = f(x)
81+
for (i, x) in zip(keys(out), d)
82+
y = f(x)
83+
@inbounds out[i] = y
7284
end
7385
end
7486
return out
7587
end
7688

7789
function Base.map!(f, out::AbstractDictionary)
7890
if istokenizable(out)
79-
@inbounds for t in tokens(out)
80-
settokenvalue!(out, t, f())
91+
for t in tokens(out)
92+
y = f()
93+
@inbounds settokenvalue!(out, t, y)
8194
end
8295
else
83-
@inbounds for i in keys(out)
84-
out[i] = f()
96+
for i in keys(out)
97+
y = f()
98+
@inbounds out[i] = y
8599
end
86100
end
87101
return out

0 commit comments

Comments
 (0)