Skip to content

Commit 4fcc37a

Browse files
authored
Tweak the conversion code (#1268)
* src/conversion.jl: order code differently I never could remember whether handle_recursion belongs to recursion_info_j or _g and had to look at call sites to find out. Now it is clear (to me, at least) from the way the code is arranged. * Some more code re-ordering to make it more uniform * More cleanup
1 parent 6e6d18e commit 4fcc37a

File tree

2 files changed

+32
-40
lines changed

2 files changed

+32
-40
lines changed

src/conversion.jl

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ const RecDict_j = IdDict{Tuple{Any, Type}, Any}
4444

4545
const JuliaCacheDict = Union{Nothing,RecDict_j}
4646

47+
# helper functions for recursion in conversion from GAP to Julia
48+
function recursion_info_j(::Type{T}, obj, recursive::Bool, recursion_dict::JuliaCacheDict) where {T}
49+
if recursive && recursion_dict === nothing
50+
return RecDict_j()
51+
else
52+
return recursion_dict
53+
end
54+
end
55+
56+
function handle_recursion(obj, ret_val, rec::Bool, rec_dict::Union{Nothing,IdDict})
57+
if rec_dict !== nothing
58+
# We assume that `obj` is not yet cached.
59+
rec_dict[obj] = ret_val
60+
end
61+
return rec ? rec_dict : nothing
62+
end
63+
64+
# Switch off recursion (hence avoid the creation of a dictionary)
65+
# if `isbitstype(T)` or `T <: GAP.Obj` holds (includes `GapObj`).
66+
function _needs_tracking_gap_to_julia(::Type{T}) where T
67+
return !(isbitstype(T) || T <: GAP.Obj)
68+
end
69+
4770

4871
"""
4972
RecDict_g = IdDict{Any,Any}
@@ -56,17 +79,6 @@ const RecDict_g = IdDict{Any,Any}
5679

5780
const GapCacheDict = Union{Nothing,RecDict_g}
5881

59-
60-
# helper functions for recursion in conversion from GAP to Julia
61-
function recursion_info_j(::Type{T}, obj, recursive::Bool, recursion_dict::JuliaCacheDict) where {T}
62-
if recursive && recursion_dict === nothing
63-
return RecDict_j()
64-
else
65-
return recursion_dict
66-
end
67-
end
68-
69-
7082
# helper functions for recursion (conversion from Julia to GAP)
7183
function recursion_info_g(::Type{T}, obj, ret_val, recursive::Bool, recursion_dict::GapCacheDict) where {T}
7284
rec = recursive && _needs_tracking_julia_to_gap(T)
@@ -84,21 +96,6 @@ function recursion_info_g(::Type{T}, obj, ret_val, recursive::Bool, recursion_di
8496

8597
end
8698

87-
function handle_recursion(obj, ret_val, rec::Bool, rec_dict::Union{Nothing,IdDict})
88-
if rec_dict !== nothing
89-
# We assume that `obj` is not yet cached.
90-
rec_dict[obj] = ret_val
91-
end
92-
return rec ? rec_dict : nothing
93-
end
94-
95-
96-
# Switch off recursion (hence avoid the creation of a dictionary)
97-
# if `isbitstype(T)` or `T <: GAP.Obj` holds (includes `GapObj`).
98-
function _needs_tracking_gap_to_julia(::Type{T}) where T
99-
return !(isbitstype(T) || T <: GAP.Obj)
100-
end
101-
10299

103100
# Helper to make use of `Val{true}` and `Val{false}` more efficient:
104101
# whenever we use patterns like `Val(recursive)` this throws a curve ball

src/gap_to_julia.jl

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,11 @@ function gap_to_julia_internal(
9999
# if the desired result is already stored then return it
100100
recursive && recursion_dict !== nothing && haskey(recursion_dict, (obj, TT)) && return recursion_dict[(obj, TT)]
101101

102-
T = eltype(TT)
103-
rec = recursive && _needs_tracking_gap_to_julia(T)
104-
105102
len_list = length(obj)
106103
ret_val = TT(undef, len_list)::TT
107104

105+
T = eltype(TT)
106+
rec = recursive && _needs_tracking_gap_to_julia(T)
108107
rec_dict = recursion_info_j(TT, obj, rec, recursion_dict)
109108
recursion_dict = handle_recursion((obj, TT), ret_val, rec, rec_dict)
110109

@@ -147,13 +146,12 @@ function gap_to_julia_internal(
147146

148147
recursive && recursion_dict !== nothing && haskey(recursion_dict, (obj, TT)) && return recursion_dict[(obj, TT)]
149148

150-
T = eltype(TT)
151-
rec = recursive && _needs_tracking_gap_to_julia(T)
152-
153149
elm = Wrappers.ELM_MAT
154150
#T not for holes!
155151
ret_val = TT(undef, nrows, ncols)::TT
156152

153+
T = eltype(TT)
154+
rec = recursive && _needs_tracking_gap_to_julia(T)
157155
rec_dict = recursion_info_j(TT, obj, rec, recursion_dict)
158156
recursion_dict = handle_recursion((obj, TT), ret_val, rec, rec_dict)
159157

@@ -188,18 +186,17 @@ function gap_to_julia_internal(
188186

189187
recursive && recursion_dict !== nothing && haskey(recursion_dict, (obj, TT)) && return recursion_dict[(obj, TT)]
190188

191-
T = eltype(TT)
192-
rec = recursive && _needs_tracking_gap_to_julia(T)
193-
194189
ret_val = TT()
195190

191+
T = eltype(TT)
192+
rec = recursive && _needs_tracking_gap_to_julia(T)
196193
rec_dict = recursion_info_j(TT, obj, rec, recursion_dict)
197-
handle_recursion((obj, TT), ret_val, rec, rec_dict)
194+
recursion_dict = handle_recursion((obj, TT), ret_val, rec, rec_dict)
198195

199196
for i = 1:length(newobj)
200197
current_obj = ElmList(newobj, i)
201198
if (rec || !(current_obj isa T)) && !isbitstype(typeof(current_obj))
202-
push!(ret_val, gap_to_julia_internal(T, current_obj, rec_dict, BoolVal(rec)))
199+
push!(ret_val, gap_to_julia_internal(T, current_obj, recursion_dict, BoolVal(rec)))
203200
else
204201
push!(ret_val, current_obj)
205202
end
@@ -246,7 +243,6 @@ function gap_to_julia_internal(
246243

247244
# Switch off recursion if none of the entry types needs recursion.
248245
rec = recursive && (_needs_tracking_gap_to_julia(S) || any(X ->_needs_tracking_gap_to_julia(X), parameters[1:(len-1)]))
249-
250246
rec_dict = recursion_info_j(TT, obj, rec, recursion_dict)
251247

252248
list = [
@@ -274,7 +270,7 @@ function gap_to_julia_internal(
274270
end
275271

276272
ret_val = TT(list)
277-
recursion_dict = handle_recursion((obj, TT), ret_val, rec, rec_dict)
273+
handle_recursion((obj, TT), ret_val, rec, rec_dict)
278274
return ret_val
279275
end
280276

@@ -303,7 +299,6 @@ function gap_to_julia_internal(
303299
ret_val = TT()
304300

305301
rec = recursive && _needs_tracking_gap_to_julia(T)
306-
307302
rec_dict = recursion_info_j(TT, obj, rec, recursion_dict)
308303
recursion_dict = handle_recursion((obj, TT), ret_val, rec, rec_dict)
309304

0 commit comments

Comments
 (0)