Skip to content

Commit 61447fd

Browse files
committed
Avoid allocation in onresponse using UnsafeArrays.
1 parent 0a9f119 commit 61447fd

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ BinDeps 0.8
44
CMakeWrapper 0.2
55
StaticArrays 0.5
66
FastIOBuffers 0.0.1
7+
UnsafeArrays 0.2.0

src/LCMCore.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module LCMCore
22

33
using StaticArrays
44
using FastIOBuffers
5+
using UnsafeArrays
56

67
using Dates
78
using FileWatching: poll_fd

src/core.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,14 @@ end
146146

147147
function onresponse(rbuf::RecvBuf, channelptr::Ptr{UInt8}, opts::SubscriptionOptions{T}) where T
148148
check_channel_name(channelptr, opts)
149-
msgdata = unsafe_wrap(Vector{UInt8}, rbuf.data, rbuf.data_size)
150-
if T === Nothing
151-
opts.handler(opts.channel, msgdata)
152-
else
153-
msg = decode(msgdata, opts.msgtype)
154-
opts.handler(opts.channel, msg)
149+
GC.@preserve rbuf begin
150+
msgdata = UnsafeArray(rbuf.data, (Int(rbuf.data_size), ))
151+
if T === Nothing
152+
opts.handler(opts.channel, msgdata)
153+
else
154+
msg = decode(msgdata, opts.msgtype)
155+
opts.handler(opts.channel, msg)
156+
end
155157
end
156158
return nothing
157159
end

0 commit comments

Comments
 (0)