Skip to content

Commit e38def2

Browse files
committed
Reduce calls to Meta.parse()
It's only called for ConcretizedSlice now, which could potentially be removed too.
1 parent d7ef8d7 commit e38def2

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/varname.jl

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,9 +868,31 @@ end
868868
# Alternate implementation with StructTypes
869869
# -----------------------------------------
870870

871+
index_to_dict(i::Integer) = Dict(:type => "integer", :value => i)
872+
index_to_dict(r::UnitRange) = Dict(:type => "unitrange", :first => first(r), :last => last(r))
873+
index_to_dict(::Colon) = Dict(:type => "colon")
874+
index_to_dict(s::ConcretizedSlice{T,R}) where {T,R} = Dict(:type => "concretized_slice", :range => repr(s.range))
875+
index_to_dict(t::Tuple) = Dict(:type => "tuple", :values => [index_to_dict(x) for x in t])
876+
877+
function dict_to_index(dict)
878+
dict = Dict(Symbol(k) => v for (k, v) in dict)
879+
if dict[:type] == "integer"
880+
return dict[:value]
881+
elseif dict[:type] == "unitrange"
882+
return dict[:first]:dict[:last]
883+
elseif dict[:type] == "colon"
884+
return Colon()
885+
elseif dict[:type] == "concretized_slice"
886+
return ConcretizedSlice(eval(Meta.parse(dict[:range])))
887+
elseif dict[:type] == "tuple"
888+
return tuple(map(dict_to_index, dict[:values])...)
889+
end
890+
end
891+
892+
871893
optic_to_dict(::typeof(identity)) = Dict(:type => "identity")
872894
optic_to_dict(::PropertyLens{sym}) where {sym} = Dict(:type => "property", :field => String(sym))
873-
optic_to_dict(i::IndexLens) = Dict(:type => "index", :indices => index_to_str(i.indices))
895+
optic_to_dict(i::IndexLens) = Dict(:type => "index", :indices => index_to_dict(i.indices))
874896
optic_to_dict(c::ComposedOptic) = Dict(:type => "composed", :outer => optic_to_dict(c.outer), :inner => optic_to_dict(c.inner))
875897

876898
function dict_to_optic(dict)
@@ -882,7 +904,7 @@ function dict_to_optic(dict)
882904
if dict[:type] == "identity"
883905
return identity
884906
elseif dict[:type] == "index"
885-
return IndexLens(eval(Meta.parse(dict[:indices])))
907+
return IndexLens(dict_to_index(dict[:indices]))
886908
elseif dict[:type] == "property"
887909
return PropertyLens{Symbol(dict[:field])}()
888910
elseif dict[:type] == "composed"

0 commit comments

Comments
 (0)