mirror of
https://github.com/postgres/postgres.git
synced 2025-07-03 20:02:46 +03:00
Reimplement the linked list data structure used throughout the backend.
In the past, we used a 'Lispy' linked list implementation: a "list" was merely a pointer to the head node of the list. The problem with that design is that it makes lappend() and length() linear time. This patch fixes that problem (and others) by maintaining a count of the list length and a pointer to the tail node along with each head node pointer. A "list" is now a pointer to a structure containing some meta-data about the list; the head and tail pointers in that structure refer to ListCell structures that maintain the actual linked list of nodes. The function names of the list API have also been changed to, I hope, be more logically consistent. By default, the old function names are still available; they will be disabled-by-default once the rest of the tree has been updated to use the new API names.
This commit is contained in:
8
src/backend/utils/cache/catcache.c
vendored
8
src/backend/utils/cache/catcache.c
vendored
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/cache/catcache.c,v 1.111 2003/11/29 19:52:00 pgsql Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/cache/catcache.c,v 1.112 2004/05/26 04:41:40 neilc Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -1317,6 +1317,7 @@ SearchCatCacheList(CatCache *cache,
|
||||
CatCList *cl;
|
||||
CatCTup *ct;
|
||||
List *ctlist;
|
||||
ListCell *ctlist_item;
|
||||
int nmembers;
|
||||
Relation relation;
|
||||
SysScanDesc scandesc;
|
||||
@ -1510,15 +1511,16 @@ SearchCatCacheList(CatCache *cache,
|
||||
cl->hash_value = lHashValue;
|
||||
cl->n_members = nmembers;
|
||||
/* The list is backwards because we built it with lcons */
|
||||
ctlist_item = list_head(ctlist);
|
||||
for (i = nmembers; --i >= 0;)
|
||||
{
|
||||
cl->members[i] = ct = (CatCTup *) lfirst(ctlist);
|
||||
cl->members[i] = ct = (CatCTup *) lfirst(ctlist_item);
|
||||
Assert(ct->c_list == NULL);
|
||||
ct->c_list = cl;
|
||||
/* mark list dead if any members already dead */
|
||||
if (ct->dead)
|
||||
cl->dead = true;
|
||||
ctlist = lnext(ctlist);
|
||||
ctlist_item = lnext(ctlist_item);
|
||||
}
|
||||
|
||||
DLAddHead(&cache->cc_lists, &cl->cache_elem);
|
||||
|
25
src/backend/utils/cache/relcache.c
vendored
25
src/backend/utils/cache/relcache.c
vendored
@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.202 2004/05/08 19:09:25 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.203 2004/05/26 04:41:40 neilc Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -1872,7 +1872,7 @@ RelationCacheInvalidate(void)
|
||||
Relation relation;
|
||||
List *rebuildFirstList = NIL;
|
||||
List *rebuildList = NIL;
|
||||
List *l;
|
||||
ListCell *l;
|
||||
|
||||
/* Phase 1 */
|
||||
hash_seq_init(&status, RelationIdCache);
|
||||
@ -2577,23 +2577,24 @@ RelationGetIndexList(Relation relation)
|
||||
static List *
|
||||
insert_ordered_oid(List *list, Oid datum)
|
||||
{
|
||||
List *l;
|
||||
ListCell *prev;
|
||||
|
||||
/* Does the datum belong at the front? */
|
||||
if (list == NIL || datum < lfirsto(list))
|
||||
return lconso(datum, list);
|
||||
if (list == NIL || datum < linitial_oid(list))
|
||||
return lcons_oid(datum, list);
|
||||
/* No, so find the entry it belongs after */
|
||||
l = list;
|
||||
prev = list_head(list);
|
||||
for (;;)
|
||||
{
|
||||
List *n = lnext(l);
|
||||
ListCell *curr = lnext(prev);
|
||||
|
||||
if (n == NIL || datum < lfirsto(n))
|
||||
break; /* it belongs before n */
|
||||
l = n;
|
||||
if (curr == NULL || datum < lfirst_oid(curr))
|
||||
break; /* it belongs after 'prev', before 'curr' */
|
||||
|
||||
prev = curr;
|
||||
}
|
||||
/* Insert datum into list after item l */
|
||||
lnext(l) = lconso(datum, lnext(l));
|
||||
/* Insert datum into list after 'prev' */
|
||||
lappend_cell_oid(list, prev, datum);
|
||||
return list;
|
||||
}
|
||||
|
||||
|
4
src/backend/utils/cache/typcache.c
vendored
4
src/backend/utils/cache/typcache.c
vendored
@ -36,7 +36,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/cache/typcache.c,v 1.5 2004/04/01 21:28:45 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/cache/typcache.c,v 1.6 2004/05/26 04:41:40 neilc Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -432,7 +432,7 @@ assign_record_type_typmod(TupleDesc tupDesc)
|
||||
Oid hashkey[REC_HASH_KEYS];
|
||||
bool found;
|
||||
int i;
|
||||
List *l;
|
||||
ListCell *l;
|
||||
int32 newtypmod;
|
||||
MemoryContext oldcxt;
|
||||
|
||||
|
Reference in New Issue
Block a user