Skip to content

Commit 76dc6df

Browse files
authored
Micro-optimise append_optic; fix serialisation for JSON@1.5.0 (#154)
* Micro-optimise `append_optic` * Add tests * tweak tests * bump workflow versions * Change OrderedDict -> NamedTuple JuliaIO/JSON.jl#447 * Add changelog note
1 parent 97d0f21 commit 76dc6df

File tree

7 files changed

+14
-13
lines changed

7 files changed

+14
-13
lines changed

.github/workflows/CI.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ jobs:
3131
os: macos-latest
3232

3333
steps:
34-
- uses: actions/checkout@v5
34+
- uses: actions/checkout@v6
3535

3636
- uses: julia-actions/setup-julia@v2
3737
with:
3838
version: ${{ matrix.runner.version }}
3939

40-
- uses: julia-actions/cache@v2
40+
- uses: julia-actions/cache@v3
4141

4242
- uses: julia-actions/julia-buildpkg@v1
4343

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.14.2
2+
3+
Fix string serialisation of VarNames such that the order of keyword arguments is preserved (this was previously guaranteed, but JSON.jl v1.5.0 introduced a change that caused the keyword arguments to always be sorted.)
4+
5+
Minor performance optimisation for `AbstractPPL.append_optic`.
6+
17
## 0.14.1
28

39
Export the `concretize_top_level` function, which concretizes only the indices contained in an `AbstractPPL.Index`, and does not recurse into child optics.

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ uuid = "7a57a42e-76ec-4ea3-a279-07e840d6d9cf"
33
keywords = ["probablistic programming"]
44
license = "MIT"
55
desc = "Common interfaces for probabilistic programming"
6-
version = "0.14.1"
6+
version = "0.14.2"
77

88
[deps]
99
AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001"

src/varname/serialize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function optic_to_dict(i::Index)
124124
# For some reason if you don't do the isempty check, it gets serialised as `{}`
125125
# rather than `[]`
126126
"ix" => isempty(i.ix) ? [] : collect(map(index_to_dict, i.ix)),
127-
"kw" => OrderedDict(String(x) => index_to_dict(y) for (x, y) in pairs(i.kw)),
127+
"kw" => NamedTuple{keys(i.kw)}(map(index_to_dict, values(i.kw))),
128128
"child" => optic_to_dict(i.child),
129129
)
130130
end

src/varname/varname.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,4 @@ x.a.b[1]
400400
function append_optic(vn::VarName{sym}, optic::AbstractOptic) where {sym}
401401
return VarName{sym}(cat(getoptic(vn), optic))
402402
end
403+
append_optic(vn::VarName, ::Iden) = vn

test/varname/serialize.jl

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,11 @@ using Test
4141
@varname(x[j=2, i=1]),
4242
@varname(x[i=1, j=2]),
4343
@varname(x[].a[j=2].b[3, 4, 5, [6]]),
44+
@varname(x[[1, 2, 5, 6]]),
4445
]
4546
for vn in vns
4647
@test string_to_varname(varname_to_string(vn)) == vn
4748
end
48-
49-
# For this VarName, the {de,}serialisation works correctly but we must
50-
# test in a different way because equality comparison of structs with
51-
# vector fields (such as Accessors.IndexLens) compares the memory
52-
# addresses rather than the contents (thus vn_vec == vn_vec2 returns
53-
# false).
54-
vn_vec = @varname(x[[1, 2, 5, 6]])
55-
vn_vec2 = string_to_varname(varname_to_string(vn_vec))
56-
@test hash(vn_vec) == hash(vn_vec2)
5749
end
5850

5951
@testset "deserialisation fails for unconcretised dynamic indices" begin

test/varname/varname.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ using JET: @test_call
226226
@test append_optic(@varname(x), @opticof(_[1])) == @varname(x[1])
227227
@test append_optic(@varname(x.a), @opticof(_[1])) == @varname(x.a[1])
228228
@test append_optic(@varname(x[1]), @opticof(_.a)) == @varname(x[1].a)
229+
@test append_optic(@varname(x), @opticof(_)) == @varname(x)
230+
@test append_optic(@varname(x.a), @opticof(_)) == @varname(x.a)
229231
end
230232
end
231233

0 commit comments

Comments
 (0)