- Notifications
You must be signed in to change notification settings - Fork28
Commit1cff1b9
committed
Represent Lists as expansible arrays, not chains of cons-cells.
Originally, Postgres Lists were a more or less exact reimplementation ofLisp lists, which consist of chains of separately-allocated cons cells,each having a value and a next-cell link. We'd hacked that once before(commitd0b4399) to add a separate List header, but the data was stillin cons cells. That makes some operations -- notably list_nth() -- O(N),and it's bulky because of the next-cell pointers and per-cell pallocoverhead, and it's very cache-unfriendly if the cons cells end upscattered around rather than being adjacent.In this rewrite, we still have List headers, but the data is in aresizable array of values, with no next-cell links. Now we need atmost two palloc's per List, and often only one, since we can allocatesome values in the same palloc call as the List header. (Of course,extending an existing List may require repalloc's to enlarge the array.But this involves just O(log N) allocations not O(N).)Of course this is not without downsides. The key difficulty is thataddition or deletion of a list entry may now cause other entries tomove, which it did not before.For example, that breaks foreach() and sister macros, which historicallyused a pointer to the current cons-cell as loop state. We can repairthose macros transparently by making their actual loop state be aninteger list index; the exposed "ListCell *" pointer is no longer statecarried across loop iterations, but is just a derived value. (Inpractice, modern compilers can optimize things back to having just oneloop state value, at least for simple cases with inline loop bodies.)In principle, this is a semantics change for cases where the loop bodyinserts or deletes list entries ahead of the current loop index; butI found no such cases in the Postgres code.The change is not at all transparent for code that doesn't use foreach()but chases lists "by hand" using lnext(). The largest share of suchcode in the backend is in loops that were maintaining "prev" and "next"variables in addition to the current-cell pointer, in order to deletelist cells efficiently using list_delete_cell(). However, we no longerneed a previous-cell pointer to delete a list cell efficiently. Keepinga next-cell pointer doesn't work, as explained above, but we can improvematters by changing such code to use a regular foreach() loop and thenusing the new macro foreach_delete_current() to delete the current cell.(This macro knows how to update the associated foreach loop's state sothat no cells will be missed in the traversal.)There remains a nontrivial risk of code assuming that a ListCell *pointer will remain good over an operation that could now move the listcontents. To help catch such errors, list.c can be compiled with a newdefine symbol DEBUG_LIST_MEMORY_USAGE that forcibly moves list contentswhenever that could possibly happen. This makes list operationssignificantly more expensive so it's not normally turned on (though itis on by default if USE_VALGRIND is on).There are two notable API differences from the previous code:* lnext() now requires the List's header pointer in addition to thecurrent cell's address.* list_delete_cell() no longer requires a previous-cell argument.These changes are somewhat unfortunate, but on the other hand code usingeither function needs inspection to see if it is assuming anythingit shouldn't, so it's not all bad.Programmers should be aware of these significant performance changes:* list_nth() and related functions are now O(1); so there's nomajor access-speed difference between a list and an array.* Inserting or deleting a list element now takes time proportional tothe distance to the end of the list, due to moving the array elements.(However, it typically *doesn't* require palloc or pfree, so except inlong lists it's probably still faster than before.) Notably, lcons()used to be about the same cost as lappend(), but that's no longer trueif the list is long. Code that uses lcons() and list_delete_first()to maintain a stack might usefully be rewritten to push and pop at theend of the list rather than the beginning.* There are now list_insert_nth...() and list_delete_nth...() functionsthat add or remove a list cell identified by index. These have thedata-movement penalty explained above, but there's no search penalty.* list_concat() and variants now copy the second list's data intostorage belonging to the first list, so there is no longer anysharing of cells between the input lists. The second argument isnow declared "const List *" to reflect that it isn't changed.This patch just does the minimum needed to get the new implementationin place and fix bugs exposed by the regression tests. As suggestedby the foregoing, there's a fair amount of followup work remaining todo.Also, the ENABLE_LIST_COMPAT macros are finally removed in thiscommit. Code using those should have been gone a dozen years ago.Patch by me; thanks to David Rowley, Jesper Pedersen, and othersfor review.Discussion:https://postgr.es/m/11587.1550975080@sss.pgh.pa.us1 parent67b9b3c commit1cff1b9
File tree
86 files changed
+1248
-1115
lines changed- contrib
- file_fdw
- pg_trgm
- postgres_fdw
- sepgsql
- src
- backend
- access/common
- catalog
- commands
- executor
- libpq
- nodes
- optimizer
- geqo
- path
- plan
- prep
- util
- parser
- partitioning
- replication
- storage/sync
- tcop
- utils
- adt
- cache
- init
- mb
- include/nodes
- pl/plpgsql/src
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
86 files changed
+1248
-1115
lines changedLines changed: 3 additions & 6 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
360 | 360 |
| |
361 | 361 |
| |
362 | 362 |
| |
363 |
| - | |
364 |
| - | |
| 363 | + | |
365 | 364 |
| |
366 | 365 |
| |
367 | 366 |
| |
| |||
387 | 386 |
| |
388 | 387 |
| |
389 | 388 |
| |
390 |
| - | |
391 | 389 |
| |
392 | 390 |
| |
393 | 391 |
| |
394 | 392 |
| |
395 | 393 |
| |
396 | 394 |
| |
397 | 395 |
| |
398 |
| - | |
| 396 | + | |
399 | 397 |
| |
400 | 398 |
| |
401 | 399 |
| |
402 | 400 |
| |
403 | 401 |
| |
404 | 402 |
| |
405 |
| - | |
| 403 | + | |
406 | 404 |
| |
407 | 405 |
| |
408 |
| - | |
409 | 406 |
| |
410 | 407 |
| |
411 | 408 |
| |
|
Lines changed: 4 additions & 14 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1013 | 1013 |
| |
1014 | 1014 |
| |
1015 | 1015 |
| |
1016 |
| - | |
1017 |
| - | |
1018 |
| - | |
| 1016 | + | |
1019 | 1017 |
| |
1020 | 1018 |
| |
1021 | 1019 |
| |
| |||
1030 | 1028 |
| |
1031 | 1029 |
| |
1032 | 1030 |
| |
1033 |
| - | |
1034 |
| - | |
1035 |
| - | |
| 1031 | + | |
1036 | 1032 |
| |
1037 | 1033 |
| |
1038 | 1034 |
| |
1039 |
| - | |
1040 | 1035 |
| |
1041 | 1036 |
| |
1042 | 1037 |
| |
| |||
1050 | 1045 |
| |
1051 | 1046 |
| |
1052 | 1047 |
| |
1053 |
| - | |
1054 |
| - | |
| 1048 | + | |
| 1049 | + | |
1055 | 1050 |
| |
1056 |
| - | |
1057 |
| - | |
1058 | 1051 |
| |
1059 |
| - | |
1060 |
| - | |
1061 |
| - | |
1062 | 1052 |
| |
1063 | 1053 |
| |
1064 | 1054 |
| |
|
Lines changed: 3 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2610 | 2610 |
| |
2611 | 2611 |
| |
2612 | 2612 |
| |
2613 |
| - | |
| 2613 | + | |
2614 | 2614 |
| |
2615 | 2615 |
| |
2616 | 2616 |
| |
| |||
2673 | 2673 |
| |
2674 | 2674 |
| |
2675 | 2675 |
| |
2676 |
| - | |
| 2676 | + | |
2677 | 2677 |
| |
2678 | 2678 |
| |
2679 | 2679 |
| |
| |||
3001 | 3001 |
| |
3002 | 3002 |
| |
3003 | 3003 |
| |
3004 |
| - | |
| 3004 | + | |
3005 | 3005 |
| |
3006 | 3006 |
| |
3007 | 3007 |
| |
|
Lines changed: 2 additions & 9 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
207 | 207 |
| |
208 | 208 |
| |
209 | 209 |
| |
210 |
| - | |
211 |
| - | |
212 | 210 |
| |
213 | 211 |
| |
214 | 212 |
| |
215 |
| - | |
216 |
| - | |
| 213 | + | |
217 | 214 |
| |
218 | 215 |
| |
219 | 216 |
| |
220 |
| - | |
221 |
| - | |
222 | 217 |
| |
223 | 218 |
| |
224 |
| - | |
225 |
| - | |
226 |
| - | |
| 219 | + | |
227 | 220 |
| |
228 | 221 |
| |
229 | 222 |
| |
|
Lines changed: 2 additions & 7 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
93 | 93 |
| |
94 | 94 |
| |
95 | 95 |
| |
96 |
| - | |
97 |
| - | |
98 | 96 |
| |
99 | 97 |
| |
100 | 98 |
| |
101 | 99 |
| |
102 | 100 |
| |
103 | 101 |
| |
104 |
| - | |
105 |
| - | |
| 102 | + | |
106 | 103 |
| |
107 | 104 |
| |
108 | 105 |
| |
109 |
| - | |
110 | 106 |
| |
111 | 107 |
| |
112 | 108 |
| |
113 |
| - | |
| 109 | + | |
114 | 110 |
| |
115 | 111 |
| |
116 | 112 |
| |
| |||
123 | 119 |
| |
124 | 120 |
| |
125 | 121 |
| |
126 |
| - | |
127 | 122 |
| |
128 | 123 |
| |
129 | 124 |
| |
|
Lines changed: 2 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
262 | 262 |
| |
263 | 263 |
| |
264 | 264 |
| |
265 |
| - | |
| 265 | + | |
266 | 266 |
| |
267 | 267 |
| |
268 | 268 |
| |
269 | 269 |
| |
270 | 270 |
| |
271 | 271 |
| |
272 |
| - | |
| 272 | + | |
273 | 273 |
| |
274 | 274 |
| |
275 | 275 |
| |
|
Lines changed: 1 addition & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
3420 | 3420 |
| |
3421 | 3421 |
| |
3422 | 3422 |
| |
3423 |
| - | |
| 3423 | + | |
3424 | 3424 |
| |
3425 | 3425 |
| |
3426 | 3426 |
| |
|
Lines changed: 3 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
347 | 347 |
| |
348 | 348 |
| |
349 | 349 |
| |
350 |
| - | |
| 350 | + | |
351 | 351 |
| |
352 | 352 |
| |
353 | 353 |
| |
| |||
397 | 397 |
| |
398 | 398 |
| |
399 | 399 |
| |
400 |
| - | |
| 400 | + | |
401 | 401 |
| |
402 | 402 |
| |
403 | 403 |
| |
| |||
2465 | 2465 |
| |
2466 | 2466 |
| |
2467 | 2467 |
| |
2468 |
| - | |
| 2468 | + | |
2469 | 2469 |
| |
2470 | 2470 |
| |
2471 | 2471 |
| |
|
Lines changed: 3 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
3402 | 3402 |
| |
3403 | 3403 |
| |
3404 | 3404 |
| |
3405 |
| - | |
| 3405 | + | |
3406 | 3406 |
| |
3407 | 3407 |
| |
3408 | 3408 |
| |
3409 | 3409 |
| |
3410 | 3410 |
| |
3411 | 3411 |
| |
3412 | 3412 |
| |
3413 |
| - | |
| 3413 | + | |
3414 | 3414 |
| |
3415 | 3415 |
| |
3416 | 3416 |
| |
| |||
3421 | 3421 |
| |
3422 | 3422 |
| |
3423 | 3423 |
| |
3424 |
| - | |
| 3424 | + | |
3425 | 3425 |
| |
3426 | 3426 |
| |
3427 | 3427 |
| |
|
Lines changed: 1 addition & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
275 | 275 |
| |
276 | 276 |
| |
277 | 277 |
| |
278 |
| - | |
| 278 | + | |
279 | 279 |
| |
280 | 280 |
| |
281 | 281 |
| |
|
Lines changed: 10 additions & 4 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
37 | 37 |
| |
38 | 38 |
| |
39 | 39 |
| |
40 |
| - | |
| 40 | + | |
41 | 41 |
| |
42 | 42 |
| |
43 | 43 |
| |
| |||
186 | 186 |
| |
187 | 187 |
| |
188 | 188 |
| |
189 |
| - | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
190 | 192 |
| |
191 | 193 |
| |
192 | 194 |
| |
| |||
217 | 219 |
| |
218 | 220 |
| |
219 | 221 |
| |
220 |
| - | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
221 | 227 |
| |
222 | 228 |
| |
223 | 229 |
| |
224 | 230 |
| |
| 231 | + | |
225 | 232 |
| |
226 | 233 |
| |
227 |
| - | |
228 | 234 |
| |
229 | 235 |
| |
230 | 236 |
| |
|
Lines changed: 4 additions & 6 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
538 | 538 |
| |
539 | 539 |
| |
540 | 540 |
| |
541 |
| - | |
542 |
| - | |
543 |
| - | |
544 |
| - | |
545 |
| - | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
546 | 544 |
| |
547 | 545 |
| |
548 | 546 |
| |
| |||
557 | 555 |
| |
558 | 556 |
| |
559 | 557 |
| |
560 |
| - | |
| 558 | + | |
561 | 559 |
| |
562 | 560 |
| |
563 | 561 |
| |
|
Lines changed: 4 additions & 8 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
481 | 481 |
| |
482 | 482 |
| |
483 | 483 |
| |
484 |
| - | |
485 | 484 |
| |
486 | 485 |
| |
487 | 486 |
| |
| |||
499 | 498 |
| |
500 | 499 |
| |
501 | 500 |
| |
502 |
| - | |
503 |
| - | |
504 |
| - | |
| 501 | + | |
505 | 502 |
| |
506 | 503 |
| |
507 | 504 |
| |
508 | 505 |
| |
509 | 506 |
| |
510 | 507 |
| |
511 |
| - | |
| 508 | + | |
512 | 509 |
| |
513 |
| - | |
| 510 | + | |
514 | 511 |
| |
515 |
| - | |
| 512 | + | |
516 | 513 |
| |
517 |
| - | |
518 | 514 |
| |
519 | 515 |
| |
520 | 516 |
| |
|
Lines changed: 2 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
455 | 455 |
| |
456 | 456 |
| |
457 | 457 |
| |
458 |
| - | |
| 458 | + | |
| 459 | + | |
459 | 460 |
| |
460 | 461 |
| |
461 | 462 |
| |
|
0 commit comments
Comments
(0)