tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Using mmap(2) in sort(1) instead of temp files



I'm trying to speed up sort(1) by using mmap(2) instead of temp
files.

ftmp() (see code below) is called in the sort functions to create and
return a temp file. mkstemp() is used to create the temp file, then
the file pointer (returned by fdopen) is returned to the sort functions
for use. I'm trying to understand where and how mmap should come
into the picture here, and how to implement this feature.

According to me, replacing fdopen with mmap, then returning the
address of the mapped region to the sort functions should do it.
This will also require the sort functions themselves to be modified
to work with the mapped region instead of a file. In addition to this,
what else should be taken care of?

PS: It was mentioned in the TODO file
> speed up sort(1) by using mmap(2) rather than temp files

-- IC

Code snippet:

FILE *
ftmp(void)
{
	sigset_t set, oset;
	FILE *fp;
	int fd;
	char path[MAXPATHLEN];

	(void)snprintf(path, sizeof(path), "%s%s%s", tmpdir,
		       (tmpdir[strlen(tmpdir)-1] != '/') ? "/" : "", _NAME_TMP);

	sigfillset(&set);
	(void)sigprocmask(SIG_BLOCK, &set, &oset);
	if ((fd = mkstemp(path)) < 0)
		err(2, "ftmp: mkstemp(\"%s\")", path);
	if (!(fp = fdopen(fd, "w+")))
		err(2, "ftmp: fdopen(\"%s\")", path);
	if (!DEBUG('t'))
		(void)unlink(path);

	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
	return (fp);
}


Home | Main Index | Thread Index | Old Index