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
/*
* Copyright (C) 2015-2020 Mateusz Łącki and Michał Startek.
*
* This file is part of IsoSpec.
*
* IsoSpec is free software: you can redistribute it and/or modify
* it under the terms of the Simplified ("2-clause") BSD licence.
*
* IsoSpec is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the Simplified BSD Licence
* along with IsoSpec. If not, see <https://opensource.org/licenses/BSD-2-Clause>.
*/
#pragma once
#include <cstring>
#include "conf.h"
#include "pod_vector.h"
namespace IsoSpec
{
// Configurations per allocator slab. Small on purpose: with glibc, a slab of
// up to 1032 bytes (4 isotopes x 64 ints = 1024) is recycled through the
// per-thread tcache when the next molecule is built, so many small molecules
// in a row run 16-20% faster than with 1000 (2000 tryptic peptides, threshold
// generator, x86-64-v3 and v4). Single large calculations do not notice.
constexpr int default_tab_size = 64;
template <typename T> inline void copyConf(
const T* source, T* destination,
int dim
){
memcpy(destination, source, dim*sizeof(T));
}
template <typename T> class Allocator
{
private:
T* currentTab;
int currentId;
const int dim, tabSize;
pod_vector<T*> prevTabs;
public:
explicit Allocator(const int dim, const int tabSize = default_tab_size);
~Allocator();
Allocator(const Allocator& other) = delete;
Allocator& operator=(const Allocator& other) = delete;
void shiftTables();
inline T* newConf()
{
currentId++;
if (currentId >= tabSize)
shiftTables();
return &(currentTab[ currentId * dim ]);
}
inline T* makeCopy(const T* conf)
{
T* currentPlace = newConf();
copyConf<T>( conf, currentPlace, dim );
return currentPlace;
}
};
} // namespace IsoSpec