GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||
GeezerVison is showing github.com — Don't return an entry twice when removing while iterating a primitive map by CCCLL · Pull Request #18070 · netty/netty Conversation
Contributor
Motivation: The iterators of IntObjectHashMap<String> map = new IntObjectHashMap<>(8); map.put(6, "a"); // index 6 map.put(14, "b"); // hashes to 6, stored at index 7 map.put(15, "c"); // hashes to 7, wraps around to index 0 Iterator<PrimitiveEntry<String>> it = map.entries().iterator(); while (it.hasNext()) { int key = it.next().key(); System.out.println(key); if (key != 15) { it.remove(); } } // prints 15, 6, 14, 15 The same happens through Modification:
Result: Removing entries while iterating returns every entry exactly once. The example above prints The new test fails for all five map types before the change and passes after it. I also ran a randomized check with The iteration order changes when index 0 is occupied. The order of these maps is not specified, and the tests above do not depend on it.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions… map Motivation: The iterators of IntObjectHashMap and the other generated primitive maps scan the arrays from index 0. remove() calls removeAt(...), which moves the displaced entries that follow the removed one back into the free slot. That run of entries can wrap around from the end of the arrays to index 0, so an entry at the start of the arrays, which the iterator has already returned, can be moved to a slot that is still ahead of the iterator and is returned again. For example, with a capacity of 8 the keys 6, 14 and 15 are stored at index 6, 7 and 0, and removing 6 and 14 while iterating returns 15 twice. Modification: - Start the iteration right after an empty slot and wrap around at the end of the arrays. A run of occupied slots ends before that empty slot, so removeAt(...) never moves an entry that was already returned to a slot ahead of the iterator. calcMaxSize(int) ensures that there is always an empty slot. - Add iteratorRemoveShouldNotReturnEntriesTwice to the map tests. Result: Removing entries while iterating returns every entry exactly once.
|
There was an error while loading. Please reload this page.
✅ All tests passed ✅🏷️ Commit: 226ad1a Learn more about TestLens at testlens.app/docs. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Auto-port PR for 5.0: #18085 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Auto-port PR for 4.1: #18087 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…ing a primitive map (#18085) Auto-port of #18070 to 5.0 Cherry-picked commit: 0ba649e --- Motivation: The iterators of `IntObjectHashMap` and the other generated primitive maps scan the arrays from index 0. `remove()` calls `removeAt(...)`, which moves the displaced entries that follow the removed one back into the free slot. That run of entries can wrap around from the end of the arrays to index 0, so an entry at the start of the arrays, which the iterator has already returned, can be moved to a slot that is still ahead of the iterator and is returned again: ```java IntObjectHashMap<String> map = new IntObjectHashMap<>(8); map.put(6, "a"); // index 6 map.put(14, "b"); // hashes to 6, stored at index 7 map.put(15, "c"); // hashes to 7, wraps around to index 0 Iterator<PrimitiveEntry<String>> it = map.entries().iterator(); while (it.hasNext()) { int key = it.next().key(); System.out.println(key); if (key != 15) { it.remove(); } } // prints 15, 6, 14, 15 ``` The same happens through `entrySet()`, `keySet()` and `values()`. Entries are only ever returned twice, never skipped. This has been the case since `remove()` started moving entries back instead of leaving tombstones (07024a4, 2014). The callers in Netty that remove while iterating (`DefaultHttp2Connection.close(...)` and `WeightedFairQueueByteDistributor`) remove the entries they visit, so I did not find an affected caller in Netty itself. Code that removes only some of the entries can see an entry twice. Modification: - Start the iteration right after an empty slot and wrap around at the end of the arrays. A run of occupied slots ends before that empty slot, so `removeAt(...)` never moves an entry that was already returned to a slot ahead of the iterator. `calcMaxSize(int)` ensures that there is always an empty slot. - Add `iteratorRemoveShouldNotReturnEntriesTwice` to the map tests. Result: Removing entries while iterating returns every entry exactly once. The example above prints `6, 14, 15`. The new test fails for all five map types before the change and passes after it. I also ran a randomized check with `LongObjectHashMap` (capacity 1 to 64, load factor 0.1 to 0.99, up to 48 keys, removing each visited entry with a random probability through `entries()`, `entrySet()` and `values()`) that every key is returned exactly once and that the map ends up with the expected content: 1,899 of 300,000 runs failed with 4.2.19.Final, none of 600,000 runs failed with this change. Tests of `common`, `codec-http`, `codec-http2`, `codec-mqtt`, `codec-redis`, `codec-dns` and `resolver-dns` pass. I could not run the `codec-http3` tests locally because they need the native QUIC library; its settings tests check the content, not the order. The iteration order changes when index 0 is occupied. The order of these maps is not specified, and the tests above do not depend on it. Co-authored-by: CCCLL <[email protected]>
…ing a primitive map (#18087) Auto-port of #18070 to 4.1 Cherry-picked commit: 0ba649e --- Motivation: The iterators of `IntObjectHashMap` and the other generated primitive maps scan the arrays from index 0. `remove()` calls `removeAt(...)`, which moves the displaced entries that follow the removed one back into the free slot. That run of entries can wrap around from the end of the arrays to index 0, so an entry at the start of the arrays, which the iterator has already returned, can be moved to a slot that is still ahead of the iterator and is returned again: ```java IntObjectHashMap<String> map = new IntObjectHashMap<>(8); map.put(6, "a"); // index 6 map.put(14, "b"); // hashes to 6, stored at index 7 map.put(15, "c"); // hashes to 7, wraps around to index 0 Iterator<PrimitiveEntry<String>> it = map.entries().iterator(); while (it.hasNext()) { int key = it.next().key(); System.out.println(key); if (key != 15) { it.remove(); } } // prints 15, 6, 14, 15 ``` The same happens through `entrySet()`, `keySet()` and `values()`. Entries are only ever returned twice, never skipped. This has been the case since `remove()` started moving entries back instead of leaving tombstones (07024a4, 2014). The callers in Netty that remove while iterating (`DefaultHttp2Connection.close(...)` and `WeightedFairQueueByteDistributor`) remove the entries they visit, so I did not find an affected caller in Netty itself. Code that removes only some of the entries can see an entry twice. Modification: - Start the iteration right after an empty slot and wrap around at the end of the arrays. A run of occupied slots ends before that empty slot, so `removeAt(...)` never moves an entry that was already returned to a slot ahead of the iterator. `calcMaxSize(int)` ensures that there is always an empty slot. - Add `iteratorRemoveShouldNotReturnEntriesTwice` to the map tests. Result: Removing entries while iterating returns every entry exactly once. The example above prints `6, 14, 15`. The new test fails for all five map types before the change and passes after it. I also ran a randomized check with `LongObjectHashMap` (capacity 1 to 64, load factor 0.1 to 0.99, up to 48 keys, removing each visited entry with a random probability through `entries()`, `entrySet()` and `values()`) that every key is returned exactly once and that the map ends up with the expected content: 1,899 of 300,000 runs failed with 4.2.19.Final, none of 600,000 runs failed with this change. Tests of `common`, `codec-http`, `codec-http2`, `codec-mqtt`, `codec-redis`, `codec-dns` and `resolver-dns` pass. I could not run the `codec-http3` tests locally because they need the native QUIC library; its settings tests check the content, not the order. The iteration order changes when index 0 is occupied. The order of these maps is not specified, and the tests above do not depend on it. Co-authored-by: CCCLL <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.