Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e8a6f1f

Browse files
committed
Get rid of radix tree's general purpose memory context
Previously, this was notionally used only for the entry point of the tree and as a convenient parent for other contexts. For shared memory, the creator previously allocated the entry point in this context, but attaching backends didn't have access to that, so they just used the caller's context. For the sake of consistency, allocate every instance of an entry point in the caller's context. For local memory, allocate the control object in the caller's context as well. This commit also makes the "leaf context" the notional parent of the child contexts used for nodes, so it's a bit of a misnomer, but a future commit will make the node contexts independent rather than children, so leave it this way for now to avoid code churn. The memory context parameter for RT_CREATE is now unused in the case of shared memory, so remove it and adjust callers to match. In passing, remove unused "context" member from struct TidStore, which seems to have been an oversight. Reviewed by Masahiko Sawada Discussion: https://postgr.es/m/CANWCAZZDCo4k5oURg_pPxM6+WZ1oiG=sqgjmQiELuyP0Vtrwig@mail.gmail.com
1 parent 960013f commit e8a6f1f

File tree

3 files changed

+41
-65
lines changed

3 files changed

+41
-65
lines changed

src/backend/access/common/tidstore.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ typedef struct BlocktableEntry
113113
/* Per-backend state for a TidStore */
114114
struct TidStore
115115
{
116-
/* MemoryContext where the TidStore is allocated */
117-
MemoryContext context;
118-
119-
/* MemoryContext that the radix tree uses */
116+
/*
117+
* MemoryContext for the radix tree when using local memory, NULL for
118+
* shared memory
119+
*/
120120
MemoryContext rt_context;
121121

122122
/* Storage for TIDs. Use either one depending on TidStoreIsShared() */
@@ -167,7 +167,6 @@ TidStoreCreateLocal(size_t max_bytes, bool insert_only)
167167
size_t maxBlockSize = ALLOCSET_DEFAULT_MAXSIZE;
168168

169169
ts = palloc0(sizeof(TidStore));
170-
ts->context = CurrentMemoryContext;
171170

172171
/* choose the maxBlockSize to be no larger than 1/16 of max_bytes */
173172
while (16 * maxBlockSize > max_bytes)
@@ -201,8 +200,7 @@ TidStoreCreateLocal(size_t max_bytes, bool insert_only)
201200

202201
/*
203202
* Similar to TidStoreCreateLocal() but create a shared TidStore on a
204-
* DSA area. The TID storage will live in the DSA area, and the memory
205-
* context rt_context will have only meta data of the radix tree.
203+
* DSA area.
206204
*
207205
* The returned object is allocated in backend-local memory.
208206
*/
@@ -215,11 +213,6 @@ TidStoreCreateShared(size_t max_bytes, int tranche_id)
215213
size_t dsa_max_size = DSA_MAX_SEGMENT_SIZE;
216214

217215
ts = palloc0(sizeof(TidStore));
218-
ts->context = CurrentMemoryContext;
219-
220-
ts->rt_context = AllocSetContextCreate(CurrentMemoryContext,
221-
"TID storage meta data",
222-
ALLOCSET_SMALL_SIZES);
223216

224217
/*
225218
* Choose the initial and maximum DSA segment sizes to be no longer than
@@ -235,8 +228,7 @@ TidStoreCreateShared(size_t max_bytes, int tranche_id)
235228
dsa_init_size = dsa_max_size;
236229

237230
area = dsa_create_ext(tranche_id, dsa_init_size, dsa_max_size);
238-
ts->tree.shared = shared_ts_create(ts->rt_context, area,
239-
tranche_id);
231+
ts->tree.shared = shared_ts_create(area, tranche_id);
240232
ts->area = area;
241233

242234
return ts;
@@ -328,13 +320,13 @@ TidStoreDestroy(TidStore *ts)
328320
if (TidStoreIsShared(ts))
329321
{
330322
shared_ts_free(ts->tree.shared);
331-
332323
dsa_detach(ts->area);
333324
}
334325
else
326+
{
335327
local_ts_free(ts->tree.local);
336-
337-
MemoryContextDelete(ts->rt_context);
328+
MemoryContextDelete(ts->rt_context);
329+
}
338330

339331
pfree(ts);
340332
}

src/include/lib/radixtree.h

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ typedef dsa_pointer RT_HANDLE;
275275
#endif
276276

277277
#ifdef RT_SHMEM
278-
RT_SCOPE RT_RADIX_TREE *RT_CREATE(MemoryContext ctx, dsa_area *dsa, int tranche_id);
278+
RT_SCOPE RT_RADIX_TREE *RT_CREATE(dsa_area *dsa, int tranche_id);
279279
RT_SCOPE RT_RADIX_TREE *RT_ATTACH(dsa_area *dsa, dsa_pointer dp);
280280
RT_SCOPE void RT_DETACH(RT_RADIX_TREE * tree);
281281
RT_SCOPE RT_HANDLE RT_GET_HANDLE(RT_RADIX_TREE * tree);
@@ -706,8 +706,6 @@ typedef struct RT_RADIX_TREE_CONTROL
706706
/* Entry point for allocating and accessing the tree */
707707
struct RT_RADIX_TREE
708708
{
709-
MemoryContext context;
710-
711709
/* pointing to either local memory or DSA */
712710
RT_RADIX_TREE_CONTROL *ctl;
713711

@@ -1809,31 +1807,25 @@ RT_SET(RT_RADIX_TREE * tree, uint64 key, RT_VALUE_TYPE * value_p)
18091807
/***************** SETUP / TEARDOWN *****************/
18101808

18111809
/*
1812-
* Create the radix tree in the given memory context and return it.
1810+
* Create the radix tree root in the caller's memory context and return it.
18131811
*
1814-
* All local memory required for a radix tree is allocated in the given
1815-
* memory context and its children. Note that RT_FREE() will delete all
1816-
* allocated space within the given memory context, so the dsa_area should
1817-
* be created in a different context.
1812+
* The tree's nodes and leaves are allocated in "ctx" and its children for
1813+
* local memory, or in "dsa" for shared memory.
18181814
*/
18191815
RT_SCOPE RT_RADIX_TREE *
18201816
#ifdef RT_SHMEM
1821-
RT_CREATE(MemoryContext ctx, dsa_area *dsa, int tranche_id)
1817+
RT_CREATE(dsa_area *dsa, int tranche_id)
18221818
#else
18231819
RT_CREATE(MemoryContext ctx)
18241820
#endif
18251821
{
18261822
RT_RADIX_TREE *tree;
1827-
MemoryContext old_ctx;
18281823
RT_CHILD_PTR rootnode;
18291824
#ifdef RT_SHMEM
18301825
dsa_pointer dp;
18311826
#endif
18321827

1833-
old_ctx = MemoryContextSwitchTo(ctx);
1834-
18351828
tree = (RT_RADIX_TREE *) palloc0(sizeof(RT_RADIX_TREE));
1836-
tree->context = ctx;
18371829

18381830
#ifdef RT_SHMEM
18391831
tree->dsa = dsa;
@@ -1858,7 +1850,7 @@ RT_CREATE(MemoryContext ctx)
18581850
}
18591851

18601852
/* By default we use the passed context for leaves. */
1861-
tree->leaf_context = tree->context;
1853+
tree->leaf_context = ctx;
18621854

18631855
#ifndef RT_VARLEN_VALUE_SIZE
18641856

@@ -1880,8 +1872,6 @@ RT_CREATE(MemoryContext ctx)
18801872
tree->ctl->start_shift = 0;
18811873
tree->ctl->max_val = RT_SHIFT_GET_MAX_VAL(0);
18821874

1883-
MemoryContextSwitchTo(old_ctx);
1884-
18851875
return tree;
18861876
}
18871877

@@ -2054,13 +2044,16 @@ RT_FREE(RT_RADIX_TREE * tree)
20542044
*/
20552045
tree->ctl->magic = 0;
20562046
dsa_free(tree->dsa, tree->ctl->handle);
2057-
#endif
2058-
2047+
#else
20592048
/*
2060-
* Free all space allocated within the tree's context and delete all child
2049+
* Free all space allocated within the leaf context and delete all child
20612050
* contexts such as those used for nodes.
20622051
*/
2063-
MemoryContextReset(tree->context);
2052+
MemoryContextReset(tree->leaf_context);
2053+
2054+
pfree(tree->ctl);
2055+
#endif
2056+
pfree(tree);
20642057
}
20652058

20662059
/***************** ITERATION *****************/
@@ -2674,7 +2667,7 @@ RT_MEMORY_USAGE(RT_RADIX_TREE * tree)
26742667
Assert(tree->ctl->magic == RT_RADIX_TREE_MAGIC);
26752668
total = dsa_get_total_size(tree->dsa);
26762669
#else
2677-
total = MemoryContextMemAllocated(tree->context, true);
2670+
total = MemoryContextMemAllocated(tree->leaf_context, true);
26782671
#endif
26792672

26802673
return total;

src/test/modules/test_radixtree/test_radixtree.c

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,22 @@ PG_FUNCTION_INFO_V1(test_radixtree);
120120
static void
121121
test_empty(void)
122122
{
123-
MemoryContext radixtree_ctx;
124123
rt_radix_tree *radixtree;
125124
rt_iter *iter;
126125
uint64 key;
127126
#ifdef TEST_SHARED_RT
128127
int tranche_id = LWLockNewTrancheId();
129128
dsa_area *dsa;
130-
#endif
131-
132-
radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
133-
"test_radix_tree",
134-
ALLOCSET_SMALL_SIZES);
135129

136-
#ifdef TEST_SHARED_RT
137130
LWLockRegisterTranche(tranche_id, "test_radix_tree");
138131
dsa = dsa_create(tranche_id);
139-
140-
radixtree = rt_create(radixtree_ctx, dsa, tranche_id);
132+
radixtree = rt_create(dsa, tranche_id);
141133
#else
134+
MemoryContext radixtree_ctx;
135+
136+
radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
137+
"test_radix_tree",
138+
ALLOCSET_SMALL_SIZES);
142139
radixtree = rt_create(radixtree_ctx);
143140
#endif
144141

@@ -165,26 +162,23 @@ test_empty(void)
165162
static void
166163
test_basic(rt_node_class_test_elem *test_info, int shift, bool asc)
167164
{
168-
MemoryContext radixtree_ctx;
169165
rt_radix_tree *radixtree;
170166
rt_iter *iter;
171167
uint64 *keys;
172168
int children = test_info->nkeys;
173169
#ifdef TEST_SHARED_RT
174170
int tranche_id = LWLockNewTrancheId();
175171
dsa_area *dsa;
176-
#endif
177172

178-
radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
179-
"test_radix_tree",
180-
ALLOCSET_SMALL_SIZES);
181-
182-
#ifdef TEST_SHARED_RT
183173
LWLockRegisterTranche(tranche_id, "test_radix_tree");
184174
dsa = dsa_create(tranche_id);
185-
186-
radixtree = rt_create(radixtree_ctx, dsa, tranche_id);
175+
radixtree = rt_create(dsa, tranche_id);
187176
#else
177+
MemoryContext radixtree_ctx;
178+
179+
radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
180+
"test_radix_tree",
181+
ALLOCSET_SMALL_SIZES);
188182
radixtree = rt_create(radixtree_ctx);
189183
#endif
190184

@@ -300,7 +294,6 @@ key_cmp(const void *a, const void *b)
300294
static void
301295
test_random(void)
302296
{
303-
MemoryContext radixtree_ctx;
304297
rt_radix_tree *radixtree;
305298
rt_iter *iter;
306299
pg_prng_state state;
@@ -313,18 +306,16 @@ test_random(void)
313306
#ifdef TEST_SHARED_RT
314307
int tranche_id = LWLockNewTrancheId();
315308
dsa_area *dsa;
316-
#endif
317309

318-
radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
319-
"test_radix_tree",
320-
ALLOCSET_SMALL_SIZES);
321-
322-
#ifdef TEST_SHARED_RT
323310
LWLockRegisterTranche(tranche_id, "test_radix_tree");
324311
dsa = dsa_create(tranche_id);
325-
326-
radixtree = rt_create(radixtree_ctx, dsa, tranche_id);
312+
radixtree = rt_create(dsa, tranche_id);
327313
#else
314+
MemoryContext radixtree_ctx;
315+
316+
radixtree_ctx = AllocSetContextCreate(CurrentMemoryContext,
317+
"test_radix_tree",
318+
ALLOCSET_SMALL_SIZES);
328319
radixtree = rt_create(radixtree_ctx);
329320
#endif
330321

0 commit comments

Comments
 (0)