Commit ef4df57
committed
perf: increase min buckets on very small types
Consider `HashSet<u8>` on x86_64 with SSE with various bucket sizes and
how many bytes the allocation ends up being:
| buckets | capacity | allocated bytes |
| ------- | -------- | --------------- |
| 4 | 3 | 36 |
| 8 | 7 | 40 |
| 16 | 14 | 48 |
| 32 | 28 | 80 |
In general, doubling the number of buckets should roughly double the
number of bytes used. However, for small bucket sizes for these small
TableLayouts (4 -> 8, 8 -> 16), it doesn't happen. This is an edge case
which happens because of padding of the control bytes and adding the
Group::WIDTH. Taking the buckets from 4 to 16 (4x) only takes the
allocated bytes from 36 to 48 (~1.3x).
This platform isn't the only one with edges. Here's aarch64 on an M1
for the same `HashSet<u8>`:
| buckets | capacity | allocated bytes |
| ------- | -------- | --------------- |
| 4 | 3 | 20 |
| 8 | 7 | 24 |
| 16 | 14 | 40 |
Notice 4 -> 8 buckets leading to only 4 more bytes (20 -> 24) instead
of roughly doubling.
Generalized, `buckets * table_layout.size` needs to be at least as big
as `table_layout.ctrl_align`. For the cases I listed above, we'd get
these new minimum bucket sizes:
- x86_64 with SSE: 16
- aarch64: 8
This is a niche optimization. However, it also removes possible
undefined behavior edge case in resize operations. In addition, it
may be a useful property to utilize over-sized allocations (see
#523).1 parent 4c824c5 commit ef4df57
1 file changed
+50
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
195 | | - | |
| 195 | + | |
196 | 196 | | |
197 | 197 | | |
198 | | - | |
199 | | - | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
200 | 221 | | |
201 | 222 | | |
202 | | - | |
| 223 | + | |
203 | 224 | | |
204 | 225 | | |
205 | 226 | | |
| |||
1126 | 1147 | | |
1127 | 1148 | | |
1128 | 1149 | | |
1129 | | - | |
| 1150 | + | |
1130 | 1151 | | |
1131 | 1152 | | |
1132 | 1153 | | |
| |||
1257 | 1278 | | |
1258 | 1279 | | |
1259 | 1280 | | |
1260 | | - | |
1261 | | - | |
1262 | | - | |
1263 | | - | |
1264 | | - | |
1265 | | - | |
1266 | | - | |
1267 | | - | |
| 1281 | + | |
| 1282 | + | |
1268 | 1283 | | |
1269 | 1284 | | |
1270 | 1285 | | |
| |||
1782 | 1797 | | |
1783 | 1798 | | |
1784 | 1799 | | |
1785 | | - | |
1786 | | - | |
| 1800 | + | |
| 1801 | + | |
1787 | 1802 | | |
1788 | 1803 | | |
1789 | 1804 | | |
| |||
4571 | 4586 | | |
4572 | 4587 | | |
4573 | 4588 | | |
| 4589 | + | |
| 4590 | + | |
| 4591 | + | |
| 4592 | + | |
| 4593 | + | |
| 4594 | + | |
| 4595 | + | |
| 4596 | + | |
| 4597 | + | |
| 4598 | + | |
| 4599 | + | |
| 4600 | + | |
| 4601 | + | |
| 4602 | + | |
| 4603 | + | |
| 4604 | + | |
| 4605 | + | |
| 4606 | + | |
| 4607 | + | |
| 4608 | + | |
4574 | 4609 | | |
4575 | 4610 | | |
4576 | 4611 | | |
| |||
0 commit comments