summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2010-12-03 14:39:23 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2010-12-03 23:43:30 -0800
commitf35f4b3396e337c4fe0fb75e8887d5fbc94dbde8 (patch)
treef698e540459e5ce1d660123af24ab214bd2374a0 /src
parenteb989f4b75cd8ef738bfee4f7363e73da293cff3 (diff)
downloadcoreutils-f35f4b3396e337c4fe0fb75e8887d5fbc94dbde8.tar.xz
sort: tune struct_merge_node slightly
* src/sort.c (struct merge_node): 'lock' is now the actual lock, not a pointer to the lock; there's no need for indirection here. Make 'level' unsigned int instead of size_t, since it is a bit-shift count; also, move it next to a bool so that it's more likely to take less space. All uses changed. (sortlines, sort): Spell out initialization instead of using an initializer. This makes the initializer a bit easier to understand, and avoids unnecessary stores into the spin lock.
Diffstat (limited to 'src')
-rw-r--r--src/sort.c40
1 files changed, 25 insertions, 15 deletions
diff --git a/src/sort.c b/src/sort.c
index 3d89a3257..141af5286 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -238,10 +238,10 @@ struct merge_node
struct line **dest; /* Pointer to destination of merge. */
size_t nlo; /* Total Lines remaining from LO. */
size_t nhi; /* Total lines remaining from HI. */
- size_t level; /* Level in merge tree. */
struct merge_node *parent; /* Parent node. */
+ unsigned int level; /* Level in merge tree. */
bool queued; /* Node is already in heap. */
- pthread_spinlock_t *lock; /* Lock for node operations. */
+ pthread_spinlock_t lock; /* Lock for node operations. */
};
/* Priority queue of merge nodes. */
@@ -3157,7 +3157,7 @@ compare_nodes (void const *a, void const *b)
static inline void
lock_node (struct merge_node *node)
{
- pthread_spin_lock (node->lock);
+ pthread_spin_lock (&node->lock);
}
/* Unlock a merge tree NODE. */
@@ -3165,7 +3165,7 @@ lock_node (struct merge_node *node)
static inline void
unlock_node (struct merge_node *node)
{
- pthread_spin_unlock (node->lock);
+ pthread_spin_unlock (&node->lock);
}
/* Destroy merge QUEUE. */
@@ -3477,10 +3477,17 @@ sortlines (struct line *restrict lines, struct line *restrict dest,
struct line *lo = dest - total_lines;
struct line *hi = lo - nlo;
struct line **parent_end = (lo_child)? &parent->end_lo : &parent->end_hi;
- pthread_spinlock_t lock;
- pthread_spin_init (&lock, PTHREAD_PROCESS_PRIVATE);
- struct merge_node node = {lo, hi, lo, hi, parent_end, nlo, nhi,
- parent->level + 1, parent, false, &lock};
+
+ struct merge_node node;
+ node.lo = node.end_lo = lo;
+ node.hi = node.end_hi = hi;
+ node.dest = parent_end;
+ node.nlo = nlo;
+ node.nhi = nhi;
+ node.parent = parent;
+ node.level = parent->level + 1;
+ node.queued = false;
+ pthread_spin_init (&node.lock, PTHREAD_PROCESS_PRIVATE);
/* Calculate thread arguments. */
unsigned long int lo_threads = nthreads / 2;
@@ -3516,7 +3523,7 @@ sortlines (struct line *restrict lines, struct line *restrict dest,
merge_loop (merge_queue, total_lines, tfp, temp_output);
}
- pthread_spin_destroy (&lock);
+ pthread_spin_destroy (&node.lock);
}
/* Scan through FILES[NTEMPS .. NFILES-1] looking for a file that is
@@ -3793,16 +3800,19 @@ sort (char * const *files, size_t nfiles, char const *output_file,
struct merge_node_queue merge_queue;
queue_init (&merge_queue, 2 * nthreads);
- pthread_spinlock_t lock;
- pthread_spin_init (&lock, PTHREAD_PROCESS_PRIVATE);
- struct merge_node node =
- {NULL, NULL, NULL, NULL, NULL, buf.nlines,
- buf.nlines, MERGE_END, NULL, false, &lock};
+ struct merge_node node;
+ node.lo = node.hi = node.end_lo = node.end_hi = NULL;
+ node.dest = NULL;
+ node.nlo = node.nhi = buf.nlines;
+ node.parent = NULL;
+ node.level = MERGE_END;
+ node.queued = false;
+ pthread_spin_init (&node.lock, PTHREAD_PROCESS_PRIVATE);
sortlines (line, line, nthreads, buf.nlines, &node, true,
&merge_queue, tfp, temp_output);
queue_destroy (&merge_queue);
- pthread_spin_destroy (&lock);
+ pthread_spin_destroy (&node.lock);
}
else
write_unique (line - 1, tfp, temp_output);