about summary refs log tree commit diff
path: root/benchmark/vector/common.hpp
blob: c96d6d017d9cba28682ee733068b5e6bf1dece20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// immer: immutable data structures for C++
// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
//
// This software is distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
//

#pragma once

#include <utility>
#include <cstddef>
#include <limits>

#include "benchmark/config.hpp"

#if IMMER_BENCHMARK_LIBRRB
extern "C" {
#define restrict __restrict__
#include <rrb.h>
#undef restrict
}
#include <immer/heap/gc_heap.hpp>
#endif

namespace immer {
template <typename T, typename MP> class array;
} // namespace immer

namespace {

auto make_generator(std::size_t runs)
{
    assert(runs > 0);
    auto engine = std::default_random_engine{42};
    auto dist = std::uniform_int_distribution<std::size_t>{0, runs-1};
    auto r = std::vector<std::size_t>(runs);
    std::generate_n(r.begin(), runs, std::bind(dist, engine));
    return r;
}

struct push_back_fn
{
    template <typename T, typename U>
    auto operator() (T&& v, U&& x)
    { return std::forward<T>(v).push_back(std::forward<U>(x)); }
};

struct push_front_fn
{
    template <typename T, typename U>
    auto operator() (T&& v, U&& x)
    { return std::forward<T>(v).push_front(std::forward<U>(x)); }
};

struct set_fn
{
    template <typename T, typename I, typename U>
    decltype(auto) operator() (T&& v, I i, U&& x)
    { return std::forward<T>(v).set(i, std::forward<U>(x)); }
};

struct store_fn
{
    template <typename T, typename I, typename U>
    decltype(auto) operator() (T&& v, I i, U&& x)
    { return std::forward<T>(v).store(i, std::forward<U>(x)); }
};

template <typename T>
struct get_limit : std::integral_constant<
    std::size_t, std::numeric_limits<std::size_t>::max()> {};

template <typename T, typename MP>
struct get_limit<immer::array<T, MP>> : std::integral_constant<
    std::size_t, 10000> {};

auto make_librrb_vector(std::size_t n)
{
    auto v = rrb_create();
    for (auto i = 0u; i < n; ++i) {
        v = rrb_push(v, reinterpret_cast<void*>(i));
    }
    return v;
}

auto make_librrb_vector_f(std::size_t n)
{
    auto v = rrb_create();
    for (auto i = 0u; i < n; ++i) {
        auto f = rrb_push(rrb_create(),
                          reinterpret_cast<void*>(i));
        v = rrb_concat(f, v);
    }
    return v;
}


// copied from:
// https://github.com/ivmai/bdwgc/blob/master/include/gc_allocator.h

template <class GC_tp>
struct GC_type_traits
{
  std::false_type GC_is_ptr_free;
};

# define GC_DECLARE_PTRFREE(T)                  \
    template<> struct GC_type_traits<T> {       \
        std::true_type GC_is_ptr_free;          \
    }

GC_DECLARE_PTRFREE(char);
GC_DECLARE_PTRFREE(signed char);
GC_DECLARE_PTRFREE(unsigned char);
GC_DECLARE_PTRFREE(signed short);
GC_DECLARE_PTRFREE(unsigned short);
GC_DECLARE_PTRFREE(signed int);
GC_DECLARE_PTRFREE(unsigned int);
GC_DECLARE_PTRFREE(signed long);
GC_DECLARE_PTRFREE(unsigned long);
GC_DECLARE_PTRFREE(float);
GC_DECLARE_PTRFREE(double);
GC_DECLARE_PTRFREE(long double);

template <class IsPtrFree>
inline void* GC_selective_alloc(size_t n, IsPtrFree, bool ignore_off_page)
{
    return ignore_off_page
        ? GC_MALLOC_IGNORE_OFF_PAGE(n)
        : GC_MALLOC(n);
}

template <>
inline void* GC_selective_alloc<std::true_type>(size_t n,
                                                std::true_type,
                                                bool ignore_off_page)
{
    return ignore_off_page
        ? GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(n)
        : GC_MALLOC_ATOMIC(n);
}

template <class T>
class gc_allocator
{
public:
    typedef size_t       size_type;
    typedef ptrdiff_t    difference_type;
    typedef T*       pointer;
    typedef const T* const_pointer;
    typedef T&       reference;
    typedef const T& const_reference;
    typedef T        value_type;

    template <class T1> struct rebind {
        typedef gc_allocator<T1> other;
    };

    gc_allocator()  {}
    gc_allocator(const gc_allocator&) throw() {}
    template <class T1>
    explicit gc_allocator(const gc_allocator<T1>&) throw() {}
    ~gc_allocator() throw() {}

    pointer address(reference GC_x) const { return &GC_x; }
    const_pointer address(const_reference GC_x) const { return &GC_x; }

    // GC_n is permitted to be 0.  The C++ standard says nothing about what
    // the return value is when GC_n == 0.
    T* allocate(size_type GC_n, const void* = 0)
    {
        GC_type_traits<T> traits;
        return static_cast<T *>
            (GC_selective_alloc(GC_n * sizeof(T),
                                traits.GC_is_ptr_free, false));
    }

    // p is not permitted to be a null pointer.
    void deallocate(pointer p, size_type /* GC_n */)
    { GC_FREE(p); }

    size_type max_size() const throw()
    { return size_t(-1) / sizeof(T); }

    void construct(pointer p, const T& __val) { new(p) T(__val); }
    void destroy(pointer p) { p->~T(); }
};

template<>
class gc_allocator<void>
{
    typedef size_t      size_type;
    typedef ptrdiff_t   difference_type;
    typedef void*       pointer;
    typedef const void* const_pointer;
    typedef void        value_type;

    template <class T1> struct rebind {
        typedef gc_allocator<T1> other;
    };
};

template <class T1, class T2>
inline bool operator==(const gc_allocator<T1>&, const gc_allocator<T2>&)
{ return true; }

template <class T1, class T2>
inline bool operator!=(const gc_allocator<T1>&, const gc_allocator<T2>&)
{ return false; }

} // anonymous namespace