Repository navigation
Catch LRU overwrites that misplace or evict - #881
BOBOGUAGUA428 wants to merge 1 commit into
Conversation
| ] | ||
| }, | ||
| { | ||
| "label": "insert after an overwrite at capacity", |
There was a problem hiding this comment.
This case fails the in-place overwrite, but a store that evicts before it checks whether the key exists still passes every lru-cache case. The oldest key is the one being overwritten in put updates refresh recency, so evicting it and re-inserting it changes nothing. A case where the overwritten key is not the oldest separates it: capacity 2, put(1,1) put(2,2) get(1) put(1,10) get(2) get(1) expects [null, null, null, 1, null, 2, 10], while evict-first returns -1 for get(2). Worth adding next to this one and pinning in the browser test the same way.
There was a problem hiding this comment.
Added as "overwrite the newest key at capacity": capacity 2, put(1,1) put(2,2) get(1) put(1,10) get(2) get(1), expecting [null, null, null, 1, null, 2, 10]. Confirmed the evict-first store passes the other six cases and fails only this one, since every other overwrite targets the oldest key.
| }, | ||
| put(key, value) { | ||
| if (slots.has(key)) { | ||
| if (fault !== "overwrite keeps its old place") slots.delete(key); |
There was a problem hiding this comment.
replay has one fault, so only one wrong store is pinned. If the case above is added, give it a second fault here (evict when slots.size === capacity before looking at whether key is present) and assert that case rejects it. Otherwise nothing fails when that case is dropped.
There was a problem hiding this comment.
replay now takes a second fault, "evicts before checking the key", which evicts when slots.size === capacity before looking at whether key is present. The test pins it to "overwrite the newest key at capacity" next to the in-place fault and its case, and the label joins the tricky-case list in tests/web/contract.rs. Dropping either case now fails the test by its label.
No lru-cache case inserted a key after an overwrite filled the store, or overwrote a key that was not the oldest, so two wrong stores passed all five. One overwrites a key in place without making it most recently used, which is what a JavaScript Map gives when the key is not deleted first, since Map.set keeps an existing key's insertion order. The other evicts the oldest key on every put to a full store before checking whether the key is already there. "put updates refresh recency" overwrites the oldest key and then only reads, so neither shows. "insert after an overwrite at capacity" overwrites key 1 and inserts key 3: the in-place store evicts key 1 and returns 2, -1 and 3 instead of -1, 10 and 3. "overwrite the newest key at capacity" reads key 1 before overwriting it: the evict-first store drops key 2 and returns -1 instead of 2. A test in tests/browser/compiler-explorer.test.js replays every case against a correct store and requires each case, looked up by label, to reject its store; it fails against the cases on main. The labels join the tricky-case list in tests/web/contract.rs, and scripts/gen-problems.py regenerated web/judges/edge-thumbnail-store.json, with the lru-cache digest in tests/golden/task-posed.json recomputed the way tests/test_task_package.py computes it. Close sysprog21#869 Change-Id: I77978f57426c8bb45b7528ee6859c83ed12ed9c1
58e6b23 to
4ecea77
Compare
| ] | ||
| }, | ||
| { | ||
| "label": "insert after an overwrite at capacity", |
There was a problem hiding this comment.
Every case stores positive keys and values, but the constraints allow 0 for both. A store that tests presence by truthiness (if (!this.map.get(key)) return -1 in JavaScript, if self.cache.get(key): in Python) passes all seven cases, then returns -1 for a stored 0 and skips its recency refresh. A case like this rejects it:
{
"label": "zero is a stored value",
"input": [
["LRUCache", "put", "put", "get", "put", "get", "get"],
[[2], [0, 0], [1, 1], [0], [2, 2], [1], [0]]
],
"expected": [null, null, null, 0, null, -1, 0]
}The truthiness store returns -1, 1, -1 for the three get calls. A "zero read as missing" fault in the replay table of tests/browser/compiler-explorer.test.js would pin it the way the two faults here are pinned. This is outside #869, so a follow-up pull request works as well.
| "expected": [null, null, null, null, null, -1, 10, 3] | ||
| }, | ||
| { | ||
| "label": "overwrite the newest key at capacity", |
There was a problem hiding this comment.
Every case uses capacity 1 or 2, so no entry ever sits between two others: a read key always moves from one end to the other, and a linked list without sentinels never takes its middle-node unlink branch. A store that moves a read key one step toward the newest end instead of all the way passes all seven cases. A capacity-3 case that reads the middle key and then the oldest catches it:
{
"label": "refresh from the middle at capacity 3",
"input": [
["LRUCache", "put", "put", "put", "get", "get", "put", "put", "get", "get", "get", "get", "get"],
[[3], [1, 1], [2, 2], [3, 3], [2], [1], [4, 4], [5, 5], [1], [2], [3], [4], [5]]
],
"expected": [null, null, null, null, 2, 1, null, null, 1, -1, -1, 4, 5]
}The one-step store evicts 1 where a correct store evicts 2, so it returns -1, 2 for get(1) and get(2). As with the cases here, the label belongs in tests/web/contract.rs, and web/judges/edge-thumbnail-store.json and the lru-cache digest are regenerated with it. A follow-up is fine if this widens #869 too far.
No
lru-cachecase inserted a key after an overwrite filled the store, or overwrote a key that was not the oldest, so two wrongThumbnailStoreimplementations passed all five cases. One overwrites a key in place without making it most recently used (what a JavaScriptMapgives when the key is not deleted first). The other evicts the oldest key on everyputto a full store before checking whether the key is already there. The case "put updates refresh recency" overwrites the oldest key and then only reads, so neither shows."insert after an overwrite at capacity" runs
put(1,1) put(2,2) put(1,10) put(3,3) get(2) get(1) get(3)on capacity 2 and expects[null, null, null, null, null, -1, 10, 3]; the in-place store returns2, -1, 3for the last three calls. "overwrite the newest key at capacity" runsput(1,1) put(2,2) get(1) put(1,10) get(2) get(1)and expects[null, null, null, 1, null, 2, 10]; the evict-first store returns-1forget(2).A test in
tests/browser/compiler-explorer.test.jsreplays every case against a correct store and requires each new case, looked up by its label, to reject its store; it fails against the cases onmain. The labels join the tricky-case list intests/web/contract.rs.python3 scripts/gen-problems.pyregeneratedweb/judges/edge-thumbnail-store.json, and thelru-cachedigest intests/golden/task-posed.jsonis recomputed the waytests/test_task_package.pycomputes it.Close #869