Skip to content

Commit 408e9f4

Browse files
authored
Merge pull request #649 from ancapdev/circ-inbounds
inbounds optimizations for CircularDeque and CircularBuffer
2 parents 64cc589 + af6e097 commit 408e9f4

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

src/circ_deque.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Get the item at the front of the queue.
3939
"""
4040
@inline function first(D::CircularDeque)
4141
@boundscheck D.n > 0 || throw(BoundsError())
42-
return D.buffer[D.first]
42+
return @inbounds D.buffer[D.first]
4343
end
4444

4545
"""
@@ -49,7 +49,7 @@ Get the item from the back of the queue.
4949
"""
5050
@inline function last(D::CircularDeque)
5151
@boundscheck D.n > 0 || throw(BoundsError())
52-
return D.buffer[D.last]
52+
return @inbounds D.buffer[D.last]
5353
end
5454

5555
@inline function Base.push!(D::CircularDeque, v)
@@ -61,7 +61,7 @@ end
6161
return D
6262
end
6363

64-
@inline function Base.pop!(D::CircularDeque)
64+
@inline Base.@propagate_inbounds function Base.pop!(D::CircularDeque)
6565
v = last(D)
6666
D.n -= 1
6767
tmp = D.last - 1
@@ -88,7 +88,7 @@ end
8888
8989
Remove the element at the front.
9090
"""
91-
@inline function popfirst!(D::CircularDeque)
91+
@inline Base.@propagate_inbounds function popfirst!(D::CircularDeque)
9292
v = first(D)
9393
D.n -= 1
9494
tmp = D.first + 1

src/circular_buffer.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ end
6969
Remove the element at the back.
7070
"""
7171
@inline function Base.pop!(cb::CircularBuffer)
72-
(cb.length == 0) && throw(ArgumentError("array must be non-empty"))
72+
@boundscheck (cb.length == 0) && throw(ArgumentError("array must be non-empty"))
7373
i = _buffer_index(cb, cb.length)
7474
cb.length -= 1
75-
return cb.buffer[i]
75+
return @inbounds cb.buffer[i]
7676
end
7777

7878
"""
@@ -87,7 +87,7 @@ Add an element to the back and overwrite front if full.
8787
else
8888
cb.length += 1
8989
end
90-
cb.buffer[_buffer_index(cb, cb.length)] = data
90+
@inbounds cb.buffer[_buffer_index(cb, cb.length)] = data
9191
return cb
9292
end
9393

@@ -97,13 +97,11 @@ end
9797
Remove the element from the front of the `CircularBuffer`.
9898
"""
9999
function popfirst!(cb::CircularBuffer)
100-
if cb.length == 0
101-
throw(ArgumentError("array must be non-empty"))
102-
end
100+
@boundscheck (cb.length == 0) && throw(ArgumentError("array must be non-empty"))
103101
i = cb.first
104102
cb.first = (cb.first + 1 > cb.capacity ? 1 : cb.first + 1)
105103
cb.length -= 1
106-
return cb.buffer[i]
104+
return @inbounds cb.buffer[i]
107105
end
108106

109107
"""
@@ -118,7 +116,7 @@ function pushfirst!(cb::CircularBuffer, data)
118116
if length(cb) < cb.capacity
119117
cb.length += 1
120118
end
121-
cb.buffer[cb.first] = data
119+
@inbounds cb.buffer[cb.first] = data
122120
return cb
123121
end
124122

0 commit comments

Comments
 (0)