@@ -79,7 +79,7 @@ function relax_container_types(vnv::DynamicPPL.VarNamedVector, vn::VarName, val)
7979end
8080function relax_container_types (vnv:: DynamicPPL.VarNamedVector , vns, vals)
8181 if need_varnames_relaxation (vnv, vns, vals)
82- varname_to_index_new = convert (OrderedDict {VarName,Int}, vnv. varname_to_index)
82+ varname_to_index_new = convert (Dict {VarName,Int}, vnv. varname_to_index)
8383 varnames_new = convert (Vector{VarName}, vnv. varnames)
8484 else
8585 varname_to_index_new = vnv. varname_to_index
517517 @testset " deterministic" begin
518518 n = 5
519519 vn = @varname (x)
520- vnv = DynamicPPL. VarNamedVector (OrderedDict (vn => [true ]))
520+ vnv = DynamicPPL. VarNamedVector (Dict (vn => [true ]))
521521 @test ! DynamicPPL. has_inactive (vnv)
522522 # Growing should not create inactive ranges.
523523 for i in 1 : n
543543 @testset " random" begin
544544 n = 5
545545 vn = @varname (x)
546- vnv = DynamicPPL. VarNamedVector (OrderedDict (vn => [true ]))
546+ vnv = DynamicPPL. VarNamedVector (Dict (vn => [true ]))
547547 @test ! DynamicPPL. has_inactive (vnv)
548548
549549 # Insert a bunch of random-length vectors.
579579 @test is_transformed (vnv, @varname (t[1 ]))
580580 @test subset (vnv, vns) == vnv
581581 end
582+
583+ @testset " loosen and tighten types" begin
584+ """
585+ test_tightenability(vnv::VarNamedVector)
586+
587+ Test that tighten_types!! is a no-op on `vnv`.
588+ """
589+ function test_tightenability (vnv:: DynamicPPL.VarNamedVector )
590+ @test vnv == DynamicPPL. tighten_types!! (deepcopy (vnv))
591+ # TODO (mhauru) We would like to check something more stringent here, namely that
592+ # the operation is compiled to a direct no-op, with no instructions at all. I
593+ # don't know how to do that though, so for now we just check that it doesn't
594+ # allocate.
595+ @allocations (DynamicPPL. tighten_types!! (vnv)) == 0
596+ return nothing
597+ end
598+
599+ vn = @varname (a[1 ])
600+ # Test that tighten_types!! is a no-op on an empty VarNamedVector.
601+ vnv = DynamicPPL. VarNamedVector ()
602+ @test DynamicPPL. is_tightly_typed (vnv)
603+ test_tightenability (vnv)
604+ # Also check that it literally returns the same object, and both tighten and loosen
605+ # are type stable.
606+ @test vnv === DynamicPPL. tighten_types!! (vnv)
607+ @inferred DynamicPPL. tighten_types!! (vnv)
608+ @inferred DynamicPPL. loosen_types!! (vnv, VarName, Any, Any)
609+ # Likewise for a VarNamedVector with something pushed into it.
610+ vnv = DynamicPPL. VarNamedVector ()
611+ vnv = setindex!! (vnv, 1.0 , vn)
612+ @test DynamicPPL. is_tightly_typed (vnv)
613+ test_tightenability (vnv)
614+ @test vnv === DynamicPPL. tighten_types!! (vnv)
615+ @inferred DynamicPPL. tighten_types!! (vnv)
616+ @inferred DynamicPPL. loosen_types!! (vnv, VarName, Any, Any)
617+ # Likewise for a VarNamedVector with abstract element-types, when that is needed for
618+ # the current contents because mixed types have been pushed into it. However, this
619+ # time, since the types are only as tight as they can be, but not actually concrete,
620+ # tighten_types!! can't be type stable.
621+ vnv = DynamicPPL. VarNamedVector ()
622+ vnv = setindex!! (vnv, 1.0 , vn)
623+ vnv = setindex!! (vnv, 2 , @varname (b))
624+ @test ! DynamicPPL. is_tightly_typed (vnv)
625+ test_tightenability (vnv)
626+ @inferred DynamicPPL. loosen_types!! (vnv, VarName, Any, Any)
627+ # Likewise when first mixed types are pushed, but then deleted.
628+ vnv = DynamicPPL. VarNamedVector ()
629+ vnv = setindex!! (vnv, 1.0 , vn)
630+ vnv = setindex!! (vnv, 2 , @varname (b))
631+ @test ! DynamicPPL. is_tightly_typed (vnv)
632+ vnv = delete!! (vnv, vn)
633+ @test DynamicPPL. is_tightly_typed (vnv)
634+ test_tightenability (vnv)
635+ @test vnv === DynamicPPL. tighten_types!! (vnv)
636+ @inferred DynamicPPL. tighten_types!! (vnv)
637+ @inferred DynamicPPL. loosen_types!! (vnv, VarName, Any, Any)
638+
639+ # Test that loosen_types!! does really loosen them and that tighten_types!! reverts
640+ # that.
641+ vnv = DynamicPPL. VarNamedVector ()
642+ vnv = setindex!! (vnv, 1.0 , vn)
643+ @test DynamicPPL. is_tightly_typed (vnv)
644+ k = eltype (vnv. varnames)
645+ e = eltype (vnv. vals)
646+ t = eltype (vnv. transforms)
647+ # Loosen key type.
648+ vnv = @inferred DynamicPPL. loosen_types!! (vnv, VarName, e, t)
649+ @test ! DynamicPPL. is_tightly_typed (vnv)
650+ vnv = DynamicPPL. tighten_types!! (vnv)
651+ @test DynamicPPL. is_tightly_typed (vnv)
652+ # Loosen element type
653+ vnv = @inferred DynamicPPL. loosen_types!! (vnv, k, Real, t)
654+ @test ! DynamicPPL. is_tightly_typed (vnv)
655+ vnv = DynamicPPL. tighten_types!! (vnv)
656+ @test DynamicPPL. is_tightly_typed (vnv)
657+ # Loosen transformation type
658+ vnv = @inferred DynamicPPL. loosen_types!! (vnv, k, e, Function)
659+ @test ! DynamicPPL. is_tightly_typed (vnv)
660+ vnv = DynamicPPL. tighten_types!! (vnv)
661+ @test DynamicPPL. is_tightly_typed (vnv)
662+ # Loosening to the same types as currently should do nothing.
663+ vnv = @inferred DynamicPPL. loosen_types!! (vnv, k, e, t)
664+ @test DynamicPPL. is_tightly_typed (vnv)
665+ @allocations (DynamicPPL. loosen_types!! (vnv, k, e, t)) == 0
666+ end
582667end
583668
584669@testset " VarInfo + VarNamedVector" begin
0 commit comments