diff options
author | Jim Meyering <jim@meyering.net> | 2001-03-03 18:23:29 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2001-03-03 18:23:29 +0000 |
commit | 25f95ca91e54d573732cd22ecedc35e97ee81248 (patch) | |
tree | 82551ff4d6cced26e0c2a9f905fb673dc4c191cf | |
parent | cfd53be2c8e7b0ef5961d1a10c400054f43a2b07 (diff) | |
download | coreutils-25f95ca91e54d573732cd22ecedc35e97ee81248.tar.xz |
Tune allocation and comparison of nodes
representing temp files. This improved CPU performance of
'sort -S 1 *.[ch]' by 17% on my host.
(struct tempnode): name member now uses struct hack.
(temphead): Now a pointer, not a structure. All uses changed.
(create_temp_file): Allocate node using struct hack.
(zaptemp): Free node using struct hack. Use pointer comparison, not
string comparison.
-rw-r--r-- | src/sort.c | 45 |
1 files changed, 22 insertions, 23 deletions
diff --git a/src/sort.c b/src/sort.c index 7c59b6721..0b50c0395 100644 --- a/src/sort.c +++ b/src/sort.c @@ -342,11 +342,12 @@ Set LC_ALL=C to get the traditional sort order that uses native byte values.\n\ static sigset_t caught_signals; /* The list of temporary files. */ -static struct tempnode +struct tempnode { - char *name; - struct tempnode * volatile next; -} temphead; + struct tempnode *volatile next; + char name[1]; /* Actual size is 1 + file name length. */ +}; +static struct tempnode *volatile temphead; /* Clean up any remaining temporary files. */ @@ -355,7 +356,7 @@ cleanup (void) { struct tempnode *node; - for (node = temphead.next; node; node = node->next) + for (node = temphead; node; node = node->next) unlink (node->name); } @@ -372,13 +373,13 @@ create_temp_file (FILE **pfp) int saved_errno; char const *temp_dir = temp_dirs[temp_dir_index]; size_t len = strlen (temp_dir); - char *file = xmalloc (len + sizeof slashbase); - struct tempnode *node = (struct tempnode *) xmalloc (sizeof *node); + struct tempnode *node = + (struct tempnode *) xmalloc (sizeof node->next + len + sizeof slashbase); + char *file = node->name; memcpy (file, temp_dir, len); memcpy (file + len, slashbase, sizeof slashbase); - node->name = file; - node->next = temphead.next; + node->next = temphead; if (++temp_dir_index == temp_dir_count) temp_dir_index = 0; @@ -386,7 +387,7 @@ create_temp_file (FILE **pfp) sigprocmask (SIG_BLOCK, &caught_signals, &oldset); fd = mkstemp (file); if (0 <= fd) - temphead.next = node; + temphead = node; saved_errno = errno; sigprocmask (SIG_SETMASK, &oldset, NULL); errno = saved_errno; @@ -484,19 +485,17 @@ add_temp_dir (char const *dir) static void zaptemp (const char *name) { - struct tempnode *node, *temp; + struct tempnode *volatile *pnode; + struct tempnode *node; - for (node = &temphead; node->next; node = node->next) - if (STREQ (name, node->next->name)) - break; - if (node->next) - { - temp = node->next; - unlink (temp->name); - node->next = temp->next; - free (temp->name); - free ((char *) temp); - } + for (pnode = &temphead; (node = *pnode); pnode = &node->next) + if (node->name == name) + { + unlink (name); + *pnode = node->next; + free ((char *) node); + break; + } } #ifdef ENABLE_NLS @@ -1906,7 +1905,7 @@ sort (char **files, int nfiles, FILE *ofp, const char *output_file) { int i = n_temp_files; tempfiles = (char **) xmalloc (n_temp_files * sizeof (char *)); - for (node = temphead.next; i > 0; node = node->next) + for (node = temphead; i > 0; node = node->next) tempfiles[--i] = node->name; merge (tempfiles, n_temp_files, ofp, output_file); free ((char *) tempfiles); |