@@ -16,11 +16,13 @@ const PyJuliaBase_Type = Ref(C.PyNULL)
16
16
17
17
# we store the actual julia values here
18
18
# the `value` field of `PyJuliaValueObject` indexes into here
19
- const PYJLVALUES = []
19
+ const PYJLVALUES = IdDict {Int,Any} ()
20
20
# unused indices in PYJLVALUES
21
21
const PYJLFREEVALUES = Int[]
22
22
# Thread safety for PYJLVALUES and PYJLFREEVALUES
23
23
const PYJLVALUES_LOCK = Threads. SpinLock ()
24
+ # Track next available index
25
+ const PYJLVALUES_NEXT_IDX = Ref (1 )
24
26
25
27
function _pyjl_new (t:: C.PyPtr , :: C.PyPtr , :: C.PyPtr )
26
28
o = ccall (UnsafePtr {C.PyTypeObject} (t). alloc[! ], C. PyPtr, (C. PyPtr, C. Py_ssize_t), t, 0 )
@@ -34,7 +36,7 @@ function _pyjl_dealloc(o::C.PyPtr)
34
36
idx = UnsafePtr {PyJuliaValueObject} (o). value[]
35
37
if idx != 0
36
38
Base. @lock PYJLVALUES_LOCK begin
37
- PYJLVALUES[idx] = nothing
39
+ delete! ( PYJLVALUES, idx)
38
40
push! (PYJLFREEVALUES, idx)
39
41
end
40
42
end
@@ -364,12 +366,12 @@ PyJuliaValue_SetValue(_o, @nospecialize(v)) = Base.GC.@preserve _o begin
364
366
if idx == 0
365
367
Base. @lock PYJLVALUES_LOCK begin
366
368
if isempty (PYJLFREEVALUES)
367
- push! (PYJLVALUES, v)
368
- idx = length (PYJLVALUES)
369
+ idx = PYJLVALUES_NEXT_IDX[]
370
+ PYJLVALUES_NEXT_IDX[] += 1
369
371
else
370
372
idx = pop! (PYJLFREEVALUES)
371
- PYJLVALUES[idx] = v
372
373
end
374
+ PYJLVALUES[idx] = v
373
375
end
374
376
UnsafePtr {PyJuliaValueObject} (o). value[] = idx
375
377
else
0 commit comments