Skip to content

Commit 89ad03d

Browse files
lemireDaniel Lemire
andauthored
Better inlining (#766)
* better inlining * more inlining * lint * moving file * remove unnecessary static. * simplifying inlining --------- Co-authored-by: Daniel Lemire <dlemire@lemire.me>
1 parent 961bab1 commit 89ad03d

File tree

7 files changed

+173
-165
lines changed

7 files changed

+173
-165
lines changed

amalgamation.sh

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,25 @@ DEMOCPP="amalgamation_demo.cpp"
3737
ALL_PUBLIC_H="
3838
$SCRIPTPATH/include/roaring/roaring_version.h
3939
$SCRIPTPATH/include/roaring/portability.h
40+
$SCRIPTPATH/include/roaring/isadetection.h
4041
$SCRIPTPATH/include/roaring/roaring_types.h
4142
$SCRIPTPATH/include/roaring/bitset/bitset.h
43+
$SCRIPTPATH/include/roaring/containers/container_defs.h
44+
$SCRIPTPATH/include/roaring/array_util.h
45+
$SCRIPTPATH/include/roaring/bitset_util.h
46+
$SCRIPTPATH/include/roaring/containers/array.h
47+
$SCRIPTPATH/include/roaring/containers/bitset.h
48+
$SCRIPTPATH/include/roaring/containers/run.h
49+
$SCRIPTPATH/include/roaring/containers/convert.h
50+
$SCRIPTPATH/include/roaring/containers/mixed_equal.h
51+
$SCRIPTPATH/include/roaring/containers/mixed_subset.h
52+
$SCRIPTPATH/include/roaring/containers/mixed_andnot.h
53+
$SCRIPTPATH/include/roaring/containers/mixed_intersection.h
54+
$SCRIPTPATH/include/roaring/containers/mixed_negation.h
55+
$SCRIPTPATH/include/roaring/containers/mixed_union.h
56+
$SCRIPTPATH/include/roaring/containers/mixed_xor.h
57+
$SCRIPTPATH/include/roaring/containers/containers.h
58+
$SCRIPTPATH/include/roaring/roaring_array.h
4259
$SCRIPTPATH/include/roaring/roaring.h
4360
$SCRIPTPATH/include/roaring/memory.h
4461
$SCRIPTPATH/include/roaring/roaring64.h
@@ -56,25 +73,8 @@ $SCRIPTPATH/cpp/roaring/roaring64map.hh
5673
# need to be in this order.
5774
#
5875
ALL_PRIVATE_H="
59-
$SCRIPTPATH/include/roaring/isadetection.h
6076
$SCRIPTPATH/include/roaring/containers/perfparameters.h
61-
$SCRIPTPATH/include/roaring/containers/container_defs.h
62-
$SCRIPTPATH/include/roaring/array_util.h
6377
$SCRIPTPATH/include/roaring/utilasm.h
64-
$SCRIPTPATH/include/roaring/bitset_util.h
65-
$SCRIPTPATH/include/roaring/containers/array.h
66-
$SCRIPTPATH/include/roaring/containers/bitset.h
67-
$SCRIPTPATH/include/roaring/containers/run.h
68-
$SCRIPTPATH/include/roaring/containers/convert.h
69-
$SCRIPTPATH/include/roaring/containers/mixed_equal.h
70-
$SCRIPTPATH/include/roaring/containers/mixed_subset.h
71-
$SCRIPTPATH/include/roaring/containers/mixed_andnot.h
72-
$SCRIPTPATH/include/roaring/containers/mixed_intersection.h
73-
$SCRIPTPATH/include/roaring/containers/mixed_negation.h
74-
$SCRIPTPATH/include/roaring/containers/mixed_union.h
75-
$SCRIPTPATH/include/roaring/containers/mixed_xor.h
76-
$SCRIPTPATH/include/roaring/containers/containers.h
77-
$SCRIPTPATH/include/roaring/roaring_array.h
7878
$SCRIPTPATH/include/roaring/art/art.h
7979
"
8080

@@ -89,7 +89,7 @@ $SCRIPTPATH/include/roaring/art/art.h
8989
ALL_PRIVATE_C=$( ( ( \
9090
[ -d $SCRIPTPATH/.git ] \
9191
&& ( type git >/dev/null 2>&1 ) \
92-
&& ( git -C $SCRIPTPATH ls-files 'src/*.c' ) \
92+
&& ( git -C $SCRIPTPATH ls-files 'src/*.c') \
9393
) || ( find $SCRIPTPATH/src -name '*.c' ) ) | sort )
9494
# Verify up-front that all the files exist
9595
#
@@ -251,7 +251,6 @@ echo "Creating ${DEMOCPP}..."
251251
cat <<< '
252252
#include <iostream>
253253
#include "roaring.hh"
254-
//#include "roaring.c"
255254
256255
int main() {
257256
roaring::Roaring r1;

include/roaring/containers/container_defs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ namespace internal { // No extern "C" (contains template)
3232
* Then undefine the awkward macro so it's not used any more than it has to be.
3333
*/
3434
typedef ROARING_CONTAINER_T container_t;
35-
#undef ROARING_CONTAINER_T
3635

3736
/*
3837
* See ROARING_CONTAINER_T for notes on using container_t as a base class.

include/roaring/containers/containers.h

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,15 +2434,131 @@ roaring_container_iterator_t container_init_iterator_last(const container_t *c,
24342434
* Moves the iterator to the next entry. Returns true and sets `value` if a
24352435
* value is present.
24362436
*/
2437-
bool container_iterator_next(const container_t *c, uint8_t typecode,
2438-
roaring_container_iterator_t *it, uint16_t *value);
2437+
inline bool container_iterator_next(const container_t *c, uint8_t typecode,
2438+
roaring_container_iterator_t *it,
2439+
uint16_t *value) {
2440+
switch (typecode) {
2441+
case BITSET_CONTAINER_TYPE: {
2442+
const bitset_container_t *bc = const_CAST_bitset(c);
2443+
it->index++;
2444+
2445+
uint32_t wordindex = it->index / 64;
2446+
if (wordindex >= BITSET_CONTAINER_SIZE_IN_WORDS) {
2447+
return false;
2448+
}
2449+
2450+
uint64_t word =
2451+
bc->words[wordindex] & (UINT64_MAX << (it->index % 64));
2452+
// next part could be optimized/simplified
2453+
while (word == 0 &&
2454+
(wordindex + 1 < BITSET_CONTAINER_SIZE_IN_WORDS)) {
2455+
wordindex++;
2456+
word = bc->words[wordindex];
2457+
}
2458+
if (word != 0) {
2459+
it->index = wordindex * 64 + roaring_trailing_zeroes(word);
2460+
*value = it->index;
2461+
return true;
2462+
}
2463+
return false;
2464+
}
2465+
case ARRAY_CONTAINER_TYPE: {
2466+
const array_container_t *ac = const_CAST_array(c);
2467+
it->index++;
2468+
if (it->index < ac->cardinality) {
2469+
*value = ac->array[it->index];
2470+
return true;
2471+
}
2472+
return false;
2473+
}
2474+
case RUN_CONTAINER_TYPE: {
2475+
if (*value == UINT16_MAX) { // Avoid overflow to zero
2476+
return false;
2477+
}
2478+
2479+
const run_container_t *rc = const_CAST_run(c);
2480+
uint32_t limit =
2481+
rc->runs[it->index].value + rc->runs[it->index].length;
2482+
if (*value < limit) {
2483+
(*value)++;
2484+
return true;
2485+
}
2486+
2487+
it->index++;
2488+
if (it->index < rc->n_runs) {
2489+
*value = rc->runs[it->index].value;
2490+
return true;
2491+
}
2492+
return false;
2493+
}
2494+
default:
2495+
assert(false);
2496+
roaring_unreachable;
2497+
return false;
2498+
}
2499+
}
24392500

24402501
/**
24412502
* Moves the iterator to the previous entry. Returns true and sets `value` if a
24422503
* value is present.
24432504
*/
2444-
bool container_iterator_prev(const container_t *c, uint8_t typecode,
2445-
roaring_container_iterator_t *it, uint16_t *value);
2505+
inline bool container_iterator_prev(const container_t *c, uint8_t typecode,
2506+
roaring_container_iterator_t *it,
2507+
uint16_t *value) {
2508+
switch (typecode) {
2509+
case BITSET_CONTAINER_TYPE: {
2510+
if (--it->index < 0) {
2511+
return false;
2512+
}
2513+
2514+
const bitset_container_t *bc = const_CAST_bitset(c);
2515+
int32_t wordindex = it->index / 64;
2516+
uint64_t word =
2517+
bc->words[wordindex] & (UINT64_MAX >> (63 - (it->index % 64)));
2518+
2519+
while (word == 0 && --wordindex >= 0) {
2520+
word = bc->words[wordindex];
2521+
}
2522+
if (word == 0) {
2523+
return false;
2524+
}
2525+
2526+
it->index = (wordindex * 64) + (63 - roaring_leading_zeroes(word));
2527+
*value = it->index;
2528+
return true;
2529+
}
2530+
case ARRAY_CONTAINER_TYPE: {
2531+
if (--it->index < 0) {
2532+
return false;
2533+
}
2534+
const array_container_t *ac = const_CAST_array(c);
2535+
*value = ac->array[it->index];
2536+
return true;
2537+
}
2538+
case RUN_CONTAINER_TYPE: {
2539+
if (*value == 0) {
2540+
return false;
2541+
}
2542+
2543+
const run_container_t *rc = const_CAST_run(c);
2544+
(*value)--;
2545+
if (*value >= rc->runs[it->index].value) {
2546+
return true;
2547+
}
2548+
2549+
if (--it->index < 0) {
2550+
return false;
2551+
}
2552+
2553+
*value = rc->runs[it->index].value + rc->runs[it->index].length;
2554+
return true;
2555+
}
2556+
default:
2557+
assert(false);
2558+
roaring_unreachable;
2559+
return false;
2560+
}
2561+
}
24462562

24472563
/**
24482564
* Moves the iterator to the smallest entry that is greater than or equal to

include/roaring/roaring.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
// Include other headers after roaring_types.h
1515
#include <roaring/bitset/bitset.h>
16+
#include <roaring/containers/containers.h>
1617
#include <roaring/memory.h>
1718
#include <roaring/portability.h>
19+
#include <roaring/roaring_array.h>
1820
#include <roaring/roaring_version.h>
1921

2022
#ifdef __cplusplus
@@ -462,7 +464,27 @@ bool roaring_bitmap_remove_checked(roaring_bitmap_t *r, uint32_t x);
462464
/**
463465
* Check if value is present
464466
*/
465-
bool roaring_bitmap_contains(const roaring_bitmap_t *r, uint32_t val);
467+
inline bool roaring_bitmap_contains(const roaring_bitmap_t *r, uint32_t val) {
468+
// For performance reasons, this function is inline and uses internal
469+
// functions directly.
470+
#ifdef __cplusplus
471+
using namespace ::roaring::internal;
472+
#endif
473+
const uint16_t hb = val >> 16;
474+
/*
475+
* the next function call involves a binary search and lots of branching.
476+
*/
477+
int32_t i = ra_get_index(&r->high_low_container, hb);
478+
if (i < 0) return false;
479+
480+
uint8_t typecode;
481+
// next call ought to be cheap
482+
container_t *container = ra_get_container_at_index(&r->high_low_container,
483+
(uint16_t)i, &typecode);
484+
// rest might be a tad expensive, possibly involving another round of binary
485+
// search
486+
return container_contains(container, val & 0xFFFF, typecode);
487+
}
466488

467489
/**
468490
* Check whether a range of values from range_start (included)

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ endif(ROARING_DISABLE_NEON)
6262
target_link_libraries(roaring PUBLIC "$<BUILD_INTERFACE:roaring-headers>")
6363
target_link_libraries(roaring PUBLIC "$<BUILD_INTERFACE:roaring-headers-cpp>")
6464

65+
target_include_directories(roaring PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
66+
6567
#
6668
#install(TARGETS roaring DESTINATION lib)
6769
#

0 commit comments

Comments
 (0)