Skip to content

Commit

Permalink
All tests pass
Browse files Browse the repository at this point in the history
Currently because `String` steals the buffer allocated,
we create a copy before conversion.
This may be suboptimal.

See JuliaLang/julia#26093
  • Loading branch information
rejuvyesh committed Aug 10, 2018
1 parent 2ec9698 commit 65efb46
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/mjextra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ resetData(m::jlModel, d::jlData) = resetData(m.m, d.d)
#################################### Name Wrappers

function name2idx(m::jlModel, num::Integer, names::Vector{Cint})
sname = String(m.names)
sname = String(copy(m.names))
idx = names[1] + 1
split_names = split(sname[idx:end], '\0', limit=(num+1))[1:num]
d = Dict{Symbol, Integer}(Symbol(split_names[i]) => i for i=1:num)
Expand All @@ -224,7 +224,7 @@ end

function name2range(m::jlModel, num::Integer,
names::Vector{Cint}, addresses::Vector{Cint}, dims::Vector{Cint})
sname = String(m.names)
sname = String(copy(m.names))
idx = names[1] + 1
split_names = split(sname[idx:end], '\0', limit=(num+1))[1:num]
d = Dict{Symbol, AbstractRange}(Symbol(split_names[i]) => (addresses[i]+1):(addresses[i]+dims[i]) for i=1:num)
Expand Down
14 changes: 8 additions & 6 deletions test/test_names.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ m, d = mj.mapmujoco(pm, pd)
sensors = mj.name2range(m, mj.get(m, :nsensor),
m.name_sensoradr, m.sensor_adr, m.sensor_dim)

sname = String(m.names)
sname = String(copy(m.names)) # See https://github.com/JuliaLang/julia/pull/26093
nsnsr = length(m.name_sensoradr)
idx = m.name_sensoradr[1] + 1
snsr_names = split(sname[idx:end], '\0', limit=(nsnsr+1))[1:nsnsr]
Expand All @@ -27,12 +27,14 @@ snsrs = Dict{Symbol, SubArray{Float64}}(Symbol(snsr_names[i]) =>
bodies = mj.name2idx(m, mj.get(m, :nbody), m.name_bodyadr) # maps body name to it's index

steps = 1000
tic()
for i=1:steps
mj.step(m,d)

t = @elapsed begin
for i=1:steps
mj.step(m,d)
end
end
t = toc()
info("Time for $steps mj.steps: ", t)

@info "Time for $steps mj.steps: $t"

@test d.xquat[:, bodies[:world] ] == [1.0, 0.0, 0.0, 0.0]
@test isapprox( d.sensordata[sensors[:accel]][1], 8.524, rtol=1e-3)
Expand Down

0 comments on commit 65efb46

Please sign in to comment.