Commit ffe5998e authored by marko's avatar marko

branches/zip: row_merge_build_indexes(): Allocate all buffers with a

single os_mem_alloc_large() call.  The function mem_alloc() that was
used previously allocates unaligned memory.

row_merge_blocks(): Replace block1, block2, block3 with a single array.
Replace buf1, buf2, buf3 with buf[3].  Replace b1..b3 with b0..b2.
Replace mrec1,2 with mrec0,1.  Replace offsets1,2 with offsets0,1.

row_merge(), row_merge_sort(): Replace block1, block2, block3 with a
single array.
parent fd91f7bb
...@@ -959,41 +959,37 @@ row_merge_blocks( ...@@ -959,41 +959,37 @@ row_merge_blocks(
dict_index_t* index, /* in: index being created */ dict_index_t* index, /* in: index being created */
merge_file_t* file, /* in/out: file containing merge_file_t* file, /* in/out: file containing
index entries */ index entries */
row_merge_block_t* block1, /* in/out: input buffer */ row_merge_block_t* block, /* in/out: 3 buffers */
row_merge_block_t* block2, /* in/out: input buffer */ ulint* foffs0, /* in/out: offset of first
row_merge_block_t* block3, /* in/out: output buffer */
ulint* foffs1, /* in/out: offset of first
source list in the file */ source list in the file */
ulint* foffs2, /* in/out: offset of second ulint* foffs1, /* in/out: offset of second
source list in the file */ source list in the file */
merge_file_t* of) /* in/out: output file */ merge_file_t* of) /* in/out: output file */
{ {
mem_heap_t* heap; /* memory heap for offsets1, offsets2 */ mem_heap_t* heap; /* memory heap for offsets0, offsets1 */
mrec_buf_t buf1; /* buffer for handling split mrec1 in block1 */ mrec_buf_t buf[3]; /* buffer for handling split mrec in block[] */
mrec_buf_t buf2; /* buffer for handling split mrec2 in block2 */ const byte* b0; /* pointer to block[0] */
mrec_buf_t buf3; /* buffer for handling split mrec in block3 */ const byte* b1; /* pointer to block[1] */
const byte* b1; /* pointer to block1 */ byte* b2; /* pointer to block[2] */
const byte* b2; /* pointer to block2 */ const mrec_t* mrec0; /* merge rec, points to block[0] or buf[0] */
byte* b3; /* pointer to block3 */ const mrec_t* mrec1; /* merge rec, points to block[1] or buf[1] */
const mrec_t* mrec1; /* merge record, points to block1 or buf1 */ ulint* offsets0;/* offsets of mrec0 */
const mrec_t* mrec2; /* merge record, points to block2 or buf2 */
ulint* offsets1;/* offsets of mrec1 */ ulint* offsets1;/* offsets of mrec1 */
ulint* offsets2;/* offsets of mrec2 */
heap = row_merge_heap_create(index, &offsets1, &offsets2); heap = row_merge_heap_create(index, &offsets0, &offsets1);
/* Write a record and read the next record. Split the output /* Write a record and read the next record. Split the output
file in two halves, which can be merged on the following pass. */ file in two halves, which can be merged on the following pass. */
#define ROW_MERGE_WRITE_GET_NEXT(N, AT_END) \ #define ROW_MERGE_WRITE_GET_NEXT(N, AT_END) \
do { \ do { \
b3 = row_merge_write_rec(block3, &buf3, b3, \ b2 = row_merge_write_rec(&block[2], &buf[2], b2, \
of->fd, &of->offset, \ of->fd, &of->offset, \
mrec##N, offsets##N); \ mrec##N, offsets##N); \
if (UNIV_UNLIKELY(!b3)) { \ if (UNIV_UNLIKELY(!b2)) { \
goto corrupt; \ goto corrupt; \
} \ } \
b##N = row_merge_read_rec(block##N, &buf##N, \ b##N = row_merge_read_rec(&block[N], &buf[N], \
b##N, index, \ b##N, index, \
file->fd, foffs##N, \ file->fd, foffs##N, \
&mrec##N, offsets##N); \ &mrec##N, offsets##N); \
...@@ -1005,30 +1001,30 @@ row_merge_blocks( ...@@ -1005,30 +1001,30 @@ row_merge_blocks(
} \ } \
} while (0) } while (0)
if (!row_merge_read(file->fd, *foffs1, block1) if (!row_merge_read(file->fd, *foffs0, &block[0])
|| !row_merge_read(file->fd, *foffs2, block2)) { || !row_merge_read(file->fd, *foffs1, &block[1])) {
corrupt: corrupt:
mem_heap_free(heap); mem_heap_free(heap);
return(DB_CORRUPTION); return(DB_CORRUPTION);
} }
b1 = *block1; b0 = block[0];
b2 = *block2; b1 = block[1];
b3 = *block3; b2 = block[2];
b1 = row_merge_read_rec(block1, &buf1, b1, index, file->fd, b0 = row_merge_read_rec(&block[0], &buf[0], b0, index, file->fd,
foffs0, &mrec0, offsets0);
b1 = row_merge_read_rec(&block[1], &buf[1], b1, index, file->fd,
foffs1, &mrec1, offsets1); foffs1, &mrec1, offsets1);
b2 = row_merge_read_rec(block2, &buf2, b2, index, file->fd, if (UNIV_UNLIKELY(!b0 && mrec0)
foffs2, &mrec2, offsets2); || UNIV_UNLIKELY(!b1 && mrec1)) {
if (UNIV_UNLIKELY(!b1 && mrec1)
|| UNIV_UNLIKELY(!b2 && mrec2)) {
goto corrupt; goto corrupt;
} }
while (mrec1 && mrec2) { while (mrec0 && mrec1) {
switch (row_merge_cmp(mrec1, mrec2, switch (row_merge_cmp(mrec0, mrec1,
offsets1, offsets2, index)) { offsets0, offsets1, index)) {
case 0: case 0:
if (UNIV_UNLIKELY if (UNIV_UNLIKELY
(dict_index_is_unique(index))) { (dict_index_is_unique(index))) {
...@@ -1037,10 +1033,10 @@ row_merge_blocks( ...@@ -1037,10 +1033,10 @@ row_merge_blocks(
} }
/* fall through */ /* fall through */
case -1: case -1:
ROW_MERGE_WRITE_GET_NEXT(1, goto merged); ROW_MERGE_WRITE_GET_NEXT(0, goto merged);
break; break;
case 1: case 1:
ROW_MERGE_WRITE_GET_NEXT(2, goto merged); ROW_MERGE_WRITE_GET_NEXT(1, goto merged);
break; break;
default: default:
ut_error; ut_error;
...@@ -1049,23 +1045,23 @@ row_merge_blocks( ...@@ -1049,23 +1045,23 @@ row_merge_blocks(
} }
merged: merged:
if (mrec1) { if (mrec0) {
/* append all mrec1 to output */ /* append all mrec0 to output */
for (;;) { for (;;) {
ROW_MERGE_WRITE_GET_NEXT(1, break); ROW_MERGE_WRITE_GET_NEXT(0, break);
} }
} }
if (mrec2) { if (mrec1) {
/* append all mrec2 to output */ /* append all mrec1 to output */
for (;;) { for (;;) {
ROW_MERGE_WRITE_GET_NEXT(2, break); ROW_MERGE_WRITE_GET_NEXT(1, break);
} }
} }
mem_heap_free(heap); mem_heap_free(heap);
b3 = row_merge_write_eof(block3, b3, of->fd, &of->offset); b2 = row_merge_write_eof(&block[2], b2, of->fd, &of->offset);
return(b3 ? DB_SUCCESS : DB_CORRUPTION); return(b2 ? DB_SUCCESS : DB_CORRUPTION);
} }
/***************************************************************** /*****************************************************************
...@@ -1079,14 +1075,12 @@ row_merge( ...@@ -1079,14 +1075,12 @@ row_merge(
dict_index_t* index, /* in: index being created */ dict_index_t* index, /* in: index being created */
merge_file_t* file, /* in/out: file containing merge_file_t* file, /* in/out: file containing
index entries */ index entries */
row_merge_block_t* block1, /* in/out: input buffer */ row_merge_block_t* block, /* in/out: 3 buffers */
row_merge_block_t* block2, /* in/out: input buffer */
row_merge_block_t* block3, /* in/out: output buffer */
int* tmpfd) /* in/out: temporary file int* tmpfd) /* in/out: temporary file
handle */ handle */
{ {
ulint foffs1; /* first input offset */ ulint foffs0; /* first input offset */
ulint foffs2; /* second input offset */ ulint foffs1; /* second input offset */
ulint half; /* upper limit of foffs1 */ ulint half; /* upper limit of foffs1 */
ulint error; /* error code */ ulint error; /* error code */
merge_file_t of; /* output file */ merge_file_t of; /* output file */
...@@ -1098,12 +1092,12 @@ row_merge( ...@@ -1098,12 +1092,12 @@ row_merge(
half = file->offset / 2; half = file->offset / 2;
/* Merge blocks to the output file. */ /* Merge blocks to the output file. */
foffs1 = 0; foffs0 = 0;
foffs2 = half; foffs1 = half;
for (; foffs1 < half; foffs1++, foffs2++) { for (; foffs0 < half; foffs0++, foffs1++) {
error = row_merge_blocks(index, file, block1, block2, block3, error = row_merge_blocks(index, file, block,
&foffs1, &foffs2, &of); &foffs0, &foffs1, &of);
if (error != DB_SUCCESS) { if (error != DB_SUCCESS) {
return(error); return(error);
...@@ -1111,9 +1105,9 @@ row_merge( ...@@ -1111,9 +1105,9 @@ row_merge(
} }
/* Copy the last block, if there is one. */ /* Copy the last block, if there is one. */
while (foffs2 < file->offset) { while (foffs1 < file->offset) {
if (!row_merge_read(file->fd, foffs2++, block2) if (!row_merge_read(file->fd, foffs1++, block)
|| !row_merge_write(of.fd, of.offset++, block2)) { || !row_merge_write(of.fd, of.offset++, block)) {
return(DB_CORRUPTION); return(DB_CORRUPTION);
} }
} }
...@@ -1136,9 +1130,7 @@ row_merge_sort( ...@@ -1136,9 +1130,7 @@ row_merge_sort(
dict_index_t* index, /* in: index being created */ dict_index_t* index, /* in: index being created */
merge_file_t* file, /* in/out: file containing merge_file_t* file, /* in/out: file containing
index entries */ index entries */
row_merge_block_t* block1, /* in/out: input buffer */ row_merge_block_t* block, /* in/out: 3 buffers */
row_merge_block_t* block2, /* in/out: input buffer */
row_merge_block_t* block3, /* in/out: output buffer */
int* tmpfd) /* in/out: temporary file int* tmpfd) /* in/out: temporary file
handle */ handle */
{ {
...@@ -1147,8 +1139,7 @@ row_merge_sort( ...@@ -1147,8 +1139,7 @@ row_merge_sort(
blksz = 1; blksz = 1;
for (;; blksz *= 2) { for (;; blksz *= 2) {
ulint error = row_merge(index, file, ulint error = row_merge(index, file, block, tmpfd);
block1, block2, block3, tmpfd);
if (error != DB_SUCCESS) { if (error != DB_SUCCESS) {
return(error); return(error);
} }
...@@ -1658,9 +1649,8 @@ row_merge_build_indexes( ...@@ -1658,9 +1649,8 @@ row_merge_build_indexes(
ulint n_indexes) /* in: size of indexes[] */ ulint n_indexes) /* in: size of indexes[] */
{ {
merge_file_t* merge_files; merge_file_t* merge_files;
row_merge_block_t* block1; row_merge_block_t* block;
row_merge_block_t* block2; ulint block_size;
row_merge_block_t* block3;
ulint i; ulint i;
ulint error; ulint error;
int tmpfd; int tmpfd;
...@@ -1677,9 +1667,8 @@ row_merge_build_indexes( ...@@ -1677,9 +1667,8 @@ row_merge_build_indexes(
fields */ fields */
merge_files = mem_alloc(n_indexes * sizeof *merge_files); merge_files = mem_alloc(n_indexes * sizeof *merge_files);
block1 = mem_alloc(sizeof *block1); block_size = 3 * sizeof *block;
block2 = mem_alloc(sizeof *block2); block = os_mem_alloc_large(&block_size);
block3 = mem_alloc(sizeof *block3);
for (i = 0; i < n_indexes; i++) { for (i = 0; i < n_indexes; i++) {
...@@ -1692,7 +1681,7 @@ row_merge_build_indexes( ...@@ -1692,7 +1681,7 @@ row_merge_build_indexes(
secondary index entries for merge sort */ secondary index entries for merge sort */
error = row_merge_read_clustered_index( error = row_merge_read_clustered_index(
trx, old_table, indexes, merge_files, n_indexes, block1); trx, old_table, indexes, merge_files, n_indexes, block);
if (error != DB_SUCCESS) { if (error != DB_SUCCESS) {
...@@ -1706,12 +1695,12 @@ row_merge_build_indexes( ...@@ -1706,12 +1695,12 @@ row_merge_build_indexes(
for (i = 0; i < n_indexes; i++) { for (i = 0; i < n_indexes; i++) {
error = row_merge_sort(indexes[i], &merge_files[i], error = row_merge_sort(indexes[i], &merge_files[i],
block1, block2, block3, &tmpfd); block, &tmpfd);
if (error == DB_SUCCESS) { if (error == DB_SUCCESS) {
error = row_merge_insert_index_tuples( error = row_merge_insert_index_tuples(
trx, indexes[i], new_table, trx, indexes[i], new_table,
merge_files[i].fd, block1); merge_files[i].fd, block);
} }
/* Close the temporary file to free up space. */ /* Close the temporary file to free up space. */
...@@ -1731,9 +1720,7 @@ row_merge_build_indexes( ...@@ -1731,9 +1720,7 @@ row_merge_build_indexes(
} }
mem_free(merge_files); mem_free(merge_files);
mem_free(block1); os_mem_free_large(block, block_size);
mem_free(block2);
mem_free(block3);
return(error); return(error);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment