Commit bb903e03 authored by Geoff Simmons's avatar Geoff Simmons

Add the QP interface as a possible replacement for patricia tries.

For "quadbit patricia tries", inspired by the work of Tony Finch:
https://dotat.at/prog/qp/README.html

Radix 16 tries, examining a nibble at a time, to make the tries
smaller and reduce pointer chasing.
parent 9c27ee9f
......@@ -78,6 +78,9 @@ AC_FUNC_REALLOC
AC_TYPE_SIZE_T
AC_TYPE_UINT64_T
# Check if gcc has this builtin (clang has a has_builtin() macro).
AX_GCC_BUILTIN(__builtin_popcount)
# --enable-stack-protector
AC_ARG_ENABLE(stack-protector,
AS_HELP_STRING([--enable-stack-protector],[enable stack protector (default is YES)]),
......
......@@ -8,7 +8,10 @@ vmod_LTLIBRARIES = libvmod_selector.la
libvmod_selector_la_SOURCES = \
vmod_selector.c \
patricia.h \
patricia.c
patricia.c \
qp.h \
qp.c \
popcnt_compat.h
nodist_libvmod_selector_la_SOURCES = \
vcc_if.c \
......@@ -22,6 +25,8 @@ dist_man_MANS = vmod_selector.3
vmod_selector.c patricia.c: patricia.h
qp.c: qp.h popcnt_compat.h
vmod_selector.lo: $(nodist_libvmod_selector_la_SOURCES)
vcc_if.h vmod_selector.rst vmod_selector.man.rst: vcc_if.c
......
/*-
* Copyright (c) 2020 UPLEX Nils Goroll Systemoptimierung
* All rights reserved
*
* Author: Geoffrey Simmons <geoffrey.simmons@uplex.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#ifndef __has_builtin
#define __has_builtin(b) 0
#endif
#if defined(__POPCNT__) && \
(HAVE___BUILTIN_POPCOUNT || __has_builtin(__builtin_popcount))
static inline int
popcount (unsigned int x)
{
return (__builtin_popcount(x));
}
#else
/* Table from Stanford Bit Twiddling Hacks */
static const unsigned char popcnt_tbl[256] =
{
# define B2(n) n, n+1, n+1, n+2
# define B4(n) B2(n), B2(n+1), B2(n+1), B2(n+2)
# define B6(n) B4(n), B4(n+1), B4(n+1), B4(n+2)
B6(0), B6(1), B6(1), B6(2)
};
static inline int
popcount(uint16_t n)
{
return (popcnt_tbl[n >> 8] + popcnt_tbl[n & 0xff]);
}
#endif
This diff is collapsed.
/*-
* Copyright (c) 2020 UPLEX Nils Goroll Systemoptimierung
* All rights reserved
*
* Author: Geoffrey Simmons <geoffrey.simmons@uplex.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include "vsb.h"
struct pt_y;
struct match_data {
unsigned magic;
#define MATCH_DATA_MAGIC 0x0d9a845e
unsigned *indices;
unsigned limit;
unsigned n;
unsigned exact;
unsigned min;
unsigned max;
};
struct qp_stats {
unsigned magic;
#define QP_STATS_MAGIC 0x06d2b30c
uint64_t nodes;
uint64_t leaves;
uint64_t terms;
uint64_t nodesz;
uint64_t dmin;
uint64_t dmax;
double davg;
uint64_t fmin;
uint64_t fmax;
double favg;
};
int QP_Insert(struct pt_y * * restrict root, unsigned idx,
char * const restrict * const restrict strings);
unsigned QP_Lookup(const struct pt_y * const restrict root,
char * const restrict * const restrict strings,
const char * const restrict subject);
int QP_Prefixes(const struct pt_y * const restrict root,
char * const restrict * const restrict strings,
const char * const restrict subject,
struct match_data * const restrict match);
void QP_Stats(const struct pt_y * const restrict root,
char * const restrict * const restrict strings,
struct qp_stats * const restrict stats);
void QP_Free(struct pt_y *y);
struct vsb * QP_Dump(struct pt_y *root, char **strings);
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