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

Commit24d8595

Browse files
committed
Introduce LogicalTapeSetExtend().
Increases the number of tapes in a logical tape set. This will beimportant for disk-based hash aggregation, because the maximum numberof tapes is not known ahead of time.While discussing this change, it was observed to regress theperformance of Sort for at least one test case. The performanceregression was because some versions of GCC switch to an inlinedversion of memcpy() in LogicalTapeWrite() after this change. Noperformance regression for clang was observed.Because the regression is due to an arbitrary decision by thecompiler, I decided it shouldn't hold up this change. If it needs tobe fixed, we can find a workaround.Author: Adam Lee, Jeff DavisDiscussion:https://postgr.es/m/e54bfec11c59689890f277722aaaabd05f78e22c.camel%40j-davis.com
1 parent17d3fcd commit24d8595

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

‎src/backend/utils/sort/logtape.c

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ struct LogicalTapeSet
191191
SizefreeBlocksLen;/* current allocated length of freeBlocks[] */
192192

193193
/* The array of logical tapes. */
194-
intnTapes;/* # of logical tapes in set */
195-
LogicalTapetapes[FLEXIBLE_ARRAY_MEMBER];/* has nTapes nentries */
194+
intnTapes;/* # of logical tapes in set */
195+
LogicalTape*tapes;/* has nTapes nentries */
196196
};
197197

198198
staticvoidltsWriteBlock(LogicalTapeSet*lts,longblocknum,void*buffer);
@@ -201,6 +201,7 @@ static long ltsGetFreeBlock(LogicalTapeSet *lts);
201201
staticvoidltsReleaseBlock(LogicalTapeSet*lts,longblocknum);
202202
staticvoidltsConcatWorkerTapes(LogicalTapeSet*lts,TapeShare*shared,
203203
SharedFileSet*fileset);
204+
staticvoidltsInitTape(LogicalTape*lt);
204205
staticvoidltsInitReadBuffer(LogicalTapeSet*lts,LogicalTape*lt);
205206

206207

@@ -536,6 +537,27 @@ ltsConcatWorkerTapes(LogicalTapeSet *lts, TapeShare *shared,
536537
lts->nHoleBlocks=lts->nBlocksAllocated-nphysicalblocks;
537538
}
538539

540+
/*
541+
* Initialize per-tape struct. Note we allocate the I/O buffer lazily.
542+
*/
543+
staticvoid
544+
ltsInitTape(LogicalTape*lt)
545+
{
546+
lt->writing= true;
547+
lt->frozen= false;
548+
lt->dirty= false;
549+
lt->firstBlockNumber=-1L;
550+
lt->curBlockNumber=-1L;
551+
lt->nextBlockNumber=-1L;
552+
lt->offsetBlockNumber=0L;
553+
lt->buffer=NULL;
554+
lt->buffer_size=0;
555+
/* palloc() larger than MaxAllocSize would fail */
556+
lt->max_size=MaxAllocSize;
557+
lt->pos=0;
558+
lt->nbytes=0;
559+
}
560+
539561
/*
540562
* Lazily allocate and initialize the read buffer. This avoids waste when many
541563
* tapes are open at once, but not all are active between rewinding and
@@ -579,15 +601,13 @@ LogicalTapeSetCreate(int ntapes, TapeShare *shared, SharedFileSet *fileset,
579601
intworker)
580602
{
581603
LogicalTapeSet*lts;
582-
LogicalTape*lt;
583604
inti;
584605

585606
/*
586607
* Create top-level struct including per-tape LogicalTape structs.
587608
*/
588609
Assert(ntapes>0);
589-
lts= (LogicalTapeSet*)palloc(offsetof(LogicalTapeSet,tapes)+
590-
ntapes*sizeof(LogicalTape));
610+
lts= (LogicalTapeSet*)palloc(sizeof(LogicalTapeSet));
591611
lts->nBlocksAllocated=0L;
592612
lts->nBlocksWritten=0L;
593613
lts->nHoleBlocks=0L;
@@ -596,30 +616,10 @@ LogicalTapeSetCreate(int ntapes, TapeShare *shared, SharedFileSet *fileset,
596616
lts->freeBlocks= (long*)palloc(lts->freeBlocksLen*sizeof(long));
597617
lts->nFreeBlocks=0;
598618
lts->nTapes=ntapes;
619+
lts->tapes= (LogicalTape*)palloc(ntapes*sizeof(LogicalTape));
599620

600-
/*
601-
* Initialize per-tape structs. Note we allocate the I/O buffer and the
602-
* first block for a tape only when it is first actually written to. This
603-
* avoids wasting memory space when tuplesort.c overestimates the number
604-
* of tapes needed.
605-
*/
606621
for (i=0;i<ntapes;i++)
607-
{
608-
lt=&lts->tapes[i];
609-
lt->writing= true;
610-
lt->frozen= false;
611-
lt->dirty= false;
612-
lt->firstBlockNumber=-1L;
613-
lt->curBlockNumber=-1L;
614-
lt->nextBlockNumber=-1L;
615-
lt->offsetBlockNumber=0L;
616-
lt->buffer=NULL;
617-
lt->buffer_size=0;
618-
/* palloc() larger than MaxAllocSize would fail */
619-
lt->max_size=MaxAllocSize;
620-
lt->pos=0;
621-
lt->nbytes=0;
622-
}
622+
ltsInitTape(&lts->tapes[i]);
623623

624624
/*
625625
* Create temp BufFile storage as required.
@@ -1004,6 +1004,25 @@ LogicalTapeFreeze(LogicalTapeSet *lts, int tapenum, TapeShare *share)
10041004
}
10051005
}
10061006

1007+
/*
1008+
* Add additional tapes to this tape set. Not intended to be used when any
1009+
* tapes are frozen.
1010+
*/
1011+
void
1012+
LogicalTapeSetExtend(LogicalTapeSet*lts,intnAdditional)
1013+
{
1014+
inti;
1015+
intnTapesOrig=lts->nTapes;
1016+
1017+
lts->nTapes+=nAdditional;
1018+
1019+
lts->tapes= (LogicalTape*)repalloc(
1020+
lts->tapes,lts->nTapes*sizeof(LogicalTape));
1021+
1022+
for (i=nTapesOrig;i<lts->nTapes;i++)
1023+
ltsInitTape(&lts->tapes[i]);
1024+
}
1025+
10071026
/*
10081027
* Backspace the tape a given number of bytes. (We also support a more
10091028
* general seek interface, see below.)

‎src/include/utils/logtape.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ extern void LogicalTapeRewindForRead(LogicalTapeSet *lts, int tapenum,
6767
externvoidLogicalTapeRewindForWrite(LogicalTapeSet*lts,inttapenum);
6868
externvoidLogicalTapeFreeze(LogicalTapeSet*lts,inttapenum,
6969
TapeShare*share);
70+
externvoidLogicalTapeSetExtend(LogicalTapeSet*lts,intnAdditional);
7071
externsize_tLogicalTapeBackspace(LogicalTapeSet*lts,inttapenum,
7172
size_tsize);
7273
externvoidLogicalTapeSeek(LogicalTapeSet*lts,inttapenum,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp