Skip to content

Commit 746ced5

Browse files
committed
Revert behavior of deep= on mutable keys.
Mutable keys are a minefield for comparisons, as resolving equality require re-implementing a lot of the internal structures, as well as dealing with multiple mutable keys that are in the same equivalency class by deep=. Simplifying the implementation to not resole mutable keys is much simpler, faster, and has the benefit that deep= and deep-not= do not need to allocate.
1 parent 1b49934 commit 746ced5

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
All notable changes to this project will be documented in this file.
33

44
## ??? - Unreleased
5+
- Add `struct/rawget`
56
- Fix `deep=` and `deep-not=` to better handle degenerate cases with mutable table keys
67
- Long strings will now dedent on `\r\n` instead of just `\n`.
78
- Add `ev/to-file` for synchronous resource operations

src/boot/boot.janet

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,8 +2259,6 @@
22592259
:string (buffer ds)
22602260
ds))
22612261

2262-
(def- mutable-types {:table true :array true :buffer true})
2263-
22642262
(defn deep-not=
22652263
``Like `not=`, but mutable types (arrays, tables, buffers) are considered
22662264
equal if they have identical structure. Much slower than `not=`.``
@@ -2282,20 +2280,10 @@
22822280
(or (= tx :struct) (= tx :table))
22832281
(or (not= (length x) (length y))
22842282
(do
2283+
(def rawget (if (= tx :struct) struct/rawget table/rawget))
22852284
(var ret false)
2286-
(def mut-keys-x @{})
22872285
(eachp [k v] x
2288-
(if (get mutable-types (type k))
2289-
(let [kk (freeze k)]
2290-
(put mut-keys-x kk (put (get mut-keys-x kk @{}) (freeze v) true)))
2291-
(if (deep-not= (get y k) v) (break (set ret true)))))
2292-
(when (next mut-keys-x) # handle case when we have mutable keys separately
2293-
(def mut-keys-y @{})
2294-
(eachp [k v] y
2295-
(if (get mutable-types (type k))
2296-
(let [kk (freeze k)]
2297-
(put mut-keys-y kk (put (get mut-keys-y kk @{}) (freeze v) true)))))
2298-
(set ret (deep-not= mut-keys-x mut-keys-y)))
2286+
(if (deep-not= (rawget y k) v) (break (set ret true))))
22992287
ret))
23002288
(= tx :buffer) (not= 0 (- (length x) (length y)) (memcmp x y))
23012289
(not= x y))))

src/core/struct.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,24 @@ JANET_CORE_FN(cfun_struct_to_table,
294294
return janet_wrap_table(tab);
295295
}
296296

297+
JANET_CORE_FN(cfun_struct_rawget,
298+
"(struct/rawget st key)",
299+
"Gets a value from a struct `st` without looking at the prototype struct. "
300+
"If `st` does not contain the key directly, the function will return "
301+
"nil without checking the prototype. Returns the value in the struct.") {
302+
janet_fixarity(argc, 2);
303+
JanetStruct st = janet_getstruct(argv, 0);
304+
return janet_struct_rawget(st, argv[1]);
305+
}
306+
297307
/* Load the struct module */
298308
void janet_lib_struct(JanetTable *env) {
299309
JanetRegExt struct_cfuns[] = {
300310
JANET_CORE_REG("struct/with-proto", cfun_struct_with_proto),
301311
JANET_CORE_REG("struct/getproto", cfun_struct_getproto),
302312
JANET_CORE_REG("struct/proto-flatten", cfun_struct_flatten),
303313
JANET_CORE_REG("struct/to-table", cfun_struct_to_table),
314+
JANET_CORE_REG("struct/rawget", cfun_struct_rawget),
304315
JANET_REG_END
305316
};
306317
janet_core_cfuns_ext(env, NULL, struct_cfuns);

test/suite-boot.janet

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,12 +1004,16 @@
10041004

10051005
# issue #1535
10061006
(loop [i :range [1 1000]]
1007-
(assert (deep= @{:key1 "value1" @"key" "value2"}
1008-
@{:key1 "value1" @"key" "value2"}) "deep= mutable keys"))
1007+
(assert (deep-not= @{:key1 "value1" @"key" "value2"}
1008+
@{:key1 "value1" @"key" "value2"}) "deep= mutable keys"))
10091009
(assert (deep-not= {"abc" 123} {@"abc" 123}) "deep= mutable keys vs immutable key")
1010-
(assert (deep= {@"" 1 @"" 2 @"" 3} {@"" 1 @"" 2 @"" 3}) "deep= duplicate mutable keys")
1011-
(assert (deep= {@"" @"" @"" @"" @"" 3} {@"" @"" @"" @"" @"" 3}) "deep= duplicate mutable keys 2")
1012-
(assert (deep= {@[] @"" @[] @"" @[] 3} {@[] @"" @[] @"" @[] 3}) "deep= duplicate mutable keys 3")
1013-
(assert (deep= {@{} @"" @{} @"" @{} 3} {@{} @"" @{} @"" @{} 3}) "deep= duplicate mutable keys 4")
1010+
(assert (deep-not= {@"" 1 @"" 2 @"" 3} {@"" 1 @"" 2 @"" 3}) "deep= duplicate mutable keys")
1011+
(assert (deep-not= {@"" @"" @"" @"" @"" 3} {@"" @"" @"" @"" @"" 3}) "deep= duplicate mutable keys 2")
1012+
(assert (deep-not= {@[] @"" @[] @"" @[] 3} {@[] @"" @[] @"" @[] 3}) "deep= duplicate mutable keys 3")
1013+
(assert (deep-not= {@{} @"" @{} @"" @{} 3} {@{} @"" @{} @"" @{} 3}) "deep= duplicate mutable keys 4")
1014+
(assert (deep-not= @{:key1 "value1" @"key2" @"value2"}
1015+
@{:key1 "value1" @"key2" "value2"}) "deep= mutable keys")
1016+
(assert (deep-not= @{:key1 "value1" [@"key2"] @"value2"}
1017+
@{:key1 "value1" [@"key2"] @"value2"}) "deep= mutable keys")
10141018

10151019
(end-suite)

test/suite-marsh.janet

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
207207
(assert (= 2 (length tclone)) "table/weak-values marsh 2")
208208
(gccollect)
209209
(assert (= 1 (length t)) "table/weak-value marsh 3")
210-
(assert (deep= t tclone) "table/weak-values marsh 4")
210+
(assert (deep= (freeze t) (freeze tclone)) "table/weak-values marsh 4")
211211

212212
# tables with prototypes
213213
(def t (table/weak-values 1))
@@ -219,7 +219,7 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
219219
(assert (= 2 (length tclone)) "marsh weak tables with prototypes 2")
220220
(gccollect)
221221
(assert (= 1 (length t)) "marsh weak tables with prototypes 3")
222-
(assert (deep= t tclone) "marsh weak tables with prototypes 4")
222+
(assert (deep= (freeze t) (freeze tclone)) "marsh weak tables with prototypes 4")
223223
(assert (deep= (getproto t) (getproto tclone)) "marsh weak tables with prototypes 5")
224224

225225
(end-suite)

0 commit comments

Comments
 (0)