Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite8a6f1f

Browse files
committed
Get rid of radix tree's general purpose memory context
Previously, this was notionally used only for the entry point of thetree and as a convenient parent for other contexts.For shared memory, the creator previously allocated the entry pointin 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 contextas well. This commit also makes the "leaf context" the notional parentof the child contexts used for nodes, so it's a bit of a misnomer,but a future commit will make the node contexts independent ratherthan children, so leave it this way for now to avoid code churn.The memory context parameter for RT_CREATE is now unused in the caseof 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 SawadaDiscussion:https://postgr.es/m/CANWCAZZDCo4k5oURg_pPxM6+WZ1oiG=sqgjmQiELuyP0Vtrwig@mail.gmail.com
1 parent960013f commite8a6f1f

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
structTidStore
115115
{
116-
/* MemoryContext where the TidStore is allocated */
117-
MemoryContextcontext;
118-
119-
/* MemoryContext that the radix tree uses */
116+
/*
117+
*MemoryContextfor the radix tree when using local memory, NULL for
118+
* shared memory
119+
*/
120120
MemoryContextrt_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_tmaxBlockSize=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_tdsa_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
returnts;
@@ -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
#ifdefRT_SHMEM
278-
RT_SCOPERT_RADIX_TREE*RT_CREATE(MemoryContextctx,dsa_area*dsa,inttranche_id);
278+
RT_SCOPERT_RADIX_TREE*RT_CREATE(dsa_area*dsa,inttranche_id);
279279
RT_SCOPERT_RADIX_TREE*RT_ATTACH(dsa_area*dsa,dsa_pointerdp);
280280
RT_SCOPEvoidRT_DETACH(RT_RADIX_TREE*tree);
281281
RT_SCOPERT_HANDLERT_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
structRT_RADIX_TREE
708708
{
709-
MemoryContextcontext;
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 thegiven memory context and return it.
1810+
* Create the radix treerootin thecaller'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_SCOPERT_RADIX_TREE*
18201816
#ifdefRT_SHMEM
1821-
RT_CREATE(MemoryContextctx,dsa_area*dsa,inttranche_id)
1817+
RT_CREATE(dsa_area*dsa,inttranche_id)
18221818
#else
18231819
RT_CREATE(MemoryContextctx)
18241820
#endif
18251821
{
18261822
RT_RADIX_TREE*tree;
1827-
MemoryContextold_ctx;
18281823
RT_CHILD_PTRrootnode;
18291824
#ifdefRT_SHMEM
18301825
dsa_pointerdp;
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
#ifdefRT_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
#ifndefRT_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
returntree;
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 thetree's context and delete all child
2049+
* Free all space allocated within theleaf 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
returntotal;

‎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
staticvoid
121121
test_empty(void)
122122
{
123-
MemoryContextradixtree_ctx;
124123
rt_radix_tree*radixtree;
125124
rt_iter*iter;
126125
uint64key;
127126
#ifdefTEST_SHARED_RT
128127
inttranche_id=LWLockNewTrancheId();
129128
dsa_area*dsa;
130-
#endif
131-
132-
radixtree_ctx=AllocSetContextCreate(CurrentMemoryContext,
133-
"test_radix_tree",
134-
ALLOCSET_SMALL_SIZES);
135129

136-
#ifdefTEST_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+
MemoryContextradixtree_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
staticvoid
166163
test_basic(rt_node_class_test_elem*test_info,intshift,boolasc)
167164
{
168-
MemoryContextradixtree_ctx;
169165
rt_radix_tree*radixtree;
170166
rt_iter*iter;
171167
uint64*keys;
172168
intchildren=test_info->nkeys;
173169
#ifdefTEST_SHARED_RT
174170
inttranche_id=LWLockNewTrancheId();
175171
dsa_area*dsa;
176-
#endif
177172

178-
radixtree_ctx=AllocSetContextCreate(CurrentMemoryContext,
179-
"test_radix_tree",
180-
ALLOCSET_SMALL_SIZES);
181-
182-
#ifdefTEST_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+
MemoryContextradixtree_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
staticvoid
301295
test_random(void)
302296
{
303-
MemoryContextradixtree_ctx;
304297
rt_radix_tree*radixtree;
305298
rt_iter*iter;
306299
pg_prng_statestate;
@@ -313,18 +306,16 @@ test_random(void)
313306
#ifdefTEST_SHARED_RT
314307
inttranche_id=LWLockNewTrancheId();
315308
dsa_area*dsa;
316-
#endif
317309

318-
radixtree_ctx=AllocSetContextCreate(CurrentMemoryContext,
319-
"test_radix_tree",
320-
ALLOCSET_SMALL_SIZES);
321-
322-
#ifdefTEST_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+
MemoryContextradixtree_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)

[8]ページ先頭

©2009-2025 Movatter.jp