summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2014-04-11 15:46:52 +0000
committerIngo Schwarze <schwarze@openbsd.org>2014-04-11 15:46:52 +0000
commita5e585fedddeff465f3d03c7056b2649c50593fb (patch)
tree509b291e6a33e294a7f7b1ff6375eddfc51bdaf0
parent2f4e06cbd2a887b4a85880dbcb59c36fb93535d2 (diff)
downloadmandoc-a5e585fedddeff465f3d03c7056b2649c50593fb.tar.gz
Further apropos(1) speed optimization was trickier than anticipated.
Contrary to what i initially thought, almost all time is now spent inside sqlite3(3) routines, and i found no easy way calling less of them. However, sqlite(3) spends substantial time in malloc(3), and even more (twice that) in its immediate malloc wrapper, sqlite3MemMalloc(), keeping track of all individual malloc chunk sizes. Typically about 90% of the malloced memory is used for purposes of the pagecache. By providing an mmap(3) MAP_ANON SQLITE_CONFIG_PAGECACHE, execution time decreases by 20-25% for simple (Nd and/or Nm) queries, 10-20% for non-NAME queries, and even apropos(1) resident memory size as reported by top(1) decreases by 20% for simple and by 60% for non-NAME queries. The new function, mansearch_setup(), spends no measurable time. The pagesize chosen is optimal: * Substantially smaller pages yield no gain at all. * Larger pages provide no additional benefit and just waste memory. The chosen number of pages in the cache is a compromise: * For simple queries, a handful of pages would suffice to get the full speed effect, at an apropos(1) resident memory size of about 2.0 MB. * For non-NAME queries, a large pagecache with 2k pages (2.5 MB) might gain a few more percent in speed, but at the expense of doubling the apropos(1) resident memory size for *all* queries. * The chosen number of 256 pages (330 kB) allows nearly full speed gain for all queries at the price of a 15% resident memory size increase.
-rw-r--r--apropos.c2
-rw-r--r--mansearch.c48
-rw-r--r--mansearch.h1
3 files changed, 51 insertions, 0 deletions
diff --git a/apropos.c b/apropos.c
index 086a842e..ac7e6d87 100644
--- a/apropos.c
+++ b/apropos.c
@@ -95,6 +95,7 @@ main(int argc, char *argv[])
search.flags = whatis ? MANSEARCH_WHATIS : 0;
manpath_parse(&paths, conf_file, defpaths, auxpaths);
+ mansearch_setup(1);
ch = mansearch(&search, &paths, argc, argv, outkey, &res, &sz);
manpath_free(&paths);
@@ -110,6 +111,7 @@ main(int argc, char *argv[])
}
free(res);
+ mansearch_setup(0);
return(sz ? EXIT_SUCCESS : EXIT_FAILURE);
usage:
fprintf(stderr, "usage: %s [-C file] [-M path] [-m path] "
diff --git a/mansearch.c b/mansearch.c
index a4348d64..b6c00a9a 100644
--- a/mansearch.c
+++ b/mansearch.c
@@ -19,6 +19,7 @@
#include "config.h"
#endif
+#include <sys/mman.h>
#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
@@ -101,6 +102,53 @@ static void sql_regexp(sqlite3_context *context,
static char *sql_statement(const struct expr *);
int
+mansearch_setup(int start)
+{
+ static void *pagecache;
+ int c;
+
+#define PC_PAGESIZE 1280
+#define PC_NUMPAGES 256
+
+ if (start) {
+ if (NULL != pagecache) {
+ fprintf(stderr, "pagecache already enabled\n");
+ return((int)MANDOCLEVEL_BADARG);
+ }
+
+ pagecache = mmap(NULL, PC_PAGESIZE * PC_NUMPAGES,
+ PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+
+ if (MAP_FAILED == pagecache) {
+ perror("mmap");
+ pagecache = NULL;
+ return((int)MANDOCLEVEL_SYSERR);
+ }
+
+ c = sqlite3_config(SQLITE_CONFIG_PAGECACHE,
+ pagecache, PC_PAGESIZE, PC_NUMPAGES);
+
+ if (SQLITE_OK == c)
+ return((int)MANDOCLEVEL_OK);
+
+ fprintf(stderr, "pagecache: %s\n", sqlite3_errstr(c));
+
+ } else if (NULL == pagecache) {
+ fprintf(stderr, "pagecache missing\n");
+ return((int)MANDOCLEVEL_BADARG);
+ }
+
+ if (-1 == munmap(pagecache, PC_PAGESIZE * PC_NUMPAGES)) {
+ perror("munmap");
+ pagecache = NULL;
+ return((int)MANDOCLEVEL_SYSERR);
+ }
+
+ pagecache = NULL;
+ return((int)MANDOCLEVEL_OK);
+}
+
+int
mansearch(const struct mansearch *search,
const struct manpaths *paths,
int argc, char *argv[],
diff --git a/mansearch.h b/mansearch.h
index d5a10ba3..894c8476 100644
--- a/mansearch.h
+++ b/mansearch.h
@@ -85,6 +85,7 @@ struct mansearch {
#define MANSEARCH_WHATIS 0x01 /* whatis mode: equality, no key */
};
+int mansearch_setup(int);
int mansearch(const struct mansearch *cfg, /* options */
const struct manpaths *paths, /* manpaths */
int argc, /* size of argv */