linux sort - beyondnlp/nlp GitHub Wiki
man sort๋ฅผ ์คํ์ํค๋ฉด ์ฌ์ฉ๋ฒ ์ค ์๋์ ๊ฐ์ ์ต์ ์ด ๋์จ๋ค.
-S, --buffer-size=SIZE
use SIZE for main memory buffer
- ํ์ง๋ง ์ ํํ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ์ ๋๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ์ง ์๋ ๊ฒ ๊ฐ๋ค. ์ถฉ๋ถํ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํฐ ์๋ฒ์์๋ ๋ถ๊ตฌํ๊ณ
- ๊ทธ๋์ ์ง์ ์์ค๋ฅผ ์ด์ด ํ์ธํด ๋ณด๊ธฐ๋ก ํ๋ค.
- http://ftp.gnu.org/gnu/coreutils/coreutils-7.1.tar.gz ์ฝ๋๋ฅผ ๋ค์ด
- sort.cํ์ผ์์ specify_sort_size()ํจ์๋ฅผ ๋ถ์
struct line{
char *text; /* Text of the line. */
size_t length; /* Length including final newline. */
char *keybeg; /* Start of first key. */
char *keylim; /* Limit of first key. */
};
* #define NMERGE_DEFAULT 16
* #define MIN_MERGE_BUFFER_SIZE (2 + sizeof (struct line))
* #define MIN_SORT_SIZE (nmerge * MIN_MERGE_BUFFER_SIZE)
-
์ฌ์ฉ์๊ฐ ๋ฃ์ ๋ฌธ์์ด์ ์ซ์๋ก ๋ณํํ๋ค.
- ex> 10k = 10 * 1024 = 10240
- enum strtol_error e = xstrtoumax (s, &suffix, 10, &n, "EgGkKmMPtTYZ");
-
๋ง์ฝ ๋ฐฑ๋ถ์จ๋ก ์ฃผ์์ ๊ฒฝ์ฐ๋ ์ฒ๋ฆฌ( 10% = ๋ฌผ๋ฆฌ์ ์ฌ์ฉ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ป์ด์ * 0.1 ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋น )
- double mem = physmem_total () * n / 100;
-
์์์ ๊ณ์ฐํ ๋ฉ๋ชจ๋ฆฌ์ MIN_SORT_SIZE์ค ํฐ ๊ฐ์ ์ฌ์ฉํด์ ํ ๋น( ์ต์ข ๊ฒฐ๊ณผ๋ sort_size ๋ณ์์ ๋ด๊ธด๋ค )
-
initbuf()
- ์๋ ํ๋ผ๋ฏธํฐ ์ค alloc๋ ์์์ ์ค๋ช ํ sort_size์ ๊ฐ์ ๊ฐ์ด๋ค.
- ์ํ๋ ํฌ๊ธฐ๋งํผ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ ์ ์์ผ๋ฉด ๋ฐ์ผ๋ก ์ค์ฌ๊ฐ๋ฉด์ ์๋ํ๋ค.
- ๋ฉ๋ชจ๋ฆฌ๊ฐ ์์ผ๋ฉด ์ฑ๋ฅ์ ๋์์ํฅ์ ์ฃผ๊ฒ ์ง๋ง ์๋๋ ๊ฒ๋ณด๋ค๋ ์ข์ผ๋...
static void initbuf (struct buffer *buf, size_t line_bytes, size_t alloc){
for (;;){
alloc += sizeof (struct line) - alloc % sizeof (struct line); <- ํ ๋นํ ๋ฉ๋ชจ๋ฆฌ ์ฐ์ฐ
buf->buf = malloc (alloc); <- ๋ฉ๋ชจ๋ฆฌ ํ ๋น( alloc๋งํผ )
if (buf->buf) <- ๋ฉ๋ชจ๋ฆฌํ ๋น์ด ์ฑ๊ณตํ๋ฉด
break; <- ๋ฃจํ ํ์ถ
alloc /= 2; <- ํ ๋นํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ฐ์ผ๋ก ์ค์ฌ๋ณธ๋ค.
if (alloc <= line_bytes + 1) <- line ๊ตฌ์กฐ์ฒด์ ํฌ๊ธฐ๋ณด๋ค ํ ๋นํ ์์ด ์๋ค๋ฉด ์ข
๋ฃ(์๋ฏธ์๋ค)
xalloc_die ();
}
buf->line_bytes = line_bytes;
buf->alloc = alloc;
buf->used = buf->left = buf->nlines = 0;
buf->eof = false;
}
This option can improve the performance of sort by causing it to start with a larger or smaller sort buffer than the default.
However, this option affects only the initial buffer size.
The buffer grows beyond size if sort encounters input lines larger than size.
- -S์ต์ ์ ์ด๊ธฐ์ ์ค์ ํ๋ ๋ฒํผ ํฌ๊ธฐ์๋ง ์ํฅ์ ์ค๋ค.
๋๋ผ๋ฉด ์ด๋ ๊ฒ ํ๊ฒ ๋ค. (ํ๋ค)
- ์๋ ๋ด์ฉ์ sort.cํ์ผ์ ์๋ ์ฃผ์์ค ์ผ๋ถ๋ฅผ ํด์ํ ๋ด์ฉ์ด๋ค.
* ์ํ๋ ํฌ๊ธฐ๋งํผ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ ์ ์์ผ๋ฉด ๋ฐ์ผ๋ก ์ค์ฌ๊ฐ๋ฉด์ ์๋ํ๋ค.
* ๋ฉ๋ชจ๋ฆฌ๊ฐ ์์ผ๋ฉด ์ฑ๋ฅ์ ๋์ ์ํฅ์ ์ฃผ๊ฒ ์ง๋ง ์๋๋ ๊ฒ๋ณด๋ค๋ ์ข์ผ๋...
- ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ถฉ๋ถํ ์ฌ์ฉํด์ ์ ๋ ฌํ์ง ๋ชปํ ๊ฑฐ๋ฉด ์ํ๋๊ฒ ๋ซ๋ค.
- ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ถฉ๋ถํ ํฌ๊ฒ ์ก์ mmap์ผ๋ก ์ฝ์ ํ ๋ผ์ธ๋ง๋ค ํฌ์ธํฐ๋ฅผ ๋ฌ์ ์ด ํฌ์ธํฐ๋ฅผ ์ ๋ ฌํ๋ ๊ฒ์ผ๋ก ํด๊ฒฐํ๋ค.
- ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ถฉ๋ถํ ํฐ ๊ฒฝ์ฐ sort๋ณด๋ค ์ต๋ ๋ช๋ฐฐ ๋น ๋ฅธ ์๋๋ฅผ ๋ณด์ธ๋ค.
- ์คํจํ๋๋ผ๋ ํ์ผ์ splitํ์ฌ ๊ฐ๊ฐ ๋๋๊ณ ๋ค์ merge sortingํ๋ ๊ฒ์ด ๊ฒฝ์ ์ ์ด๋ค.