Skip to content

Commit df7b3af

Browse files
committed
constexpr + noexcept sort
1 parent ee8c8e2 commit df7b3af

File tree

2 files changed

+53
-52
lines changed

2 files changed

+53
-52
lines changed

src/jrd/sort.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
#include <io.h> // lseek, read, write, close
6363
#endif
6464

65-
const USHORT RUN_GROUP = 8;
66-
const USHORT MAX_MERGE_LEVEL = 2;
65+
constexpr USHORT RUN_GROUP = 8;
66+
constexpr USHORT MAX_MERGE_LEVEL = 2;
6767

6868
using namespace Jrd;
6969
using namespace Firebird;
@@ -76,8 +76,8 @@ using namespace Firebird;
7676
// dimitr: this comment is outdated since FB 1.5, where max buffer size
7777
// of (128KB - overhead) has been replaced with exact 128KB.
7878

79-
const ULONG MAX_SORT_BUFFER_SIZE = 1024 * 128; // 128KB
80-
const ULONG MIN_RECORDS_TO_ALLOC = 8;
79+
constexpr ULONG MAX_SORT_BUFFER_SIZE = 1024 * 128; // 128KB
80+
constexpr ULONG MIN_RECORDS_TO_ALLOC = 8;
8181

8282
// the size of sr_bckptr (everything before sort_record) in bytes
8383
#define SIZEOF_SR_BCKPTR offsetof(sr, sr_sort_record)
@@ -123,23 +123,23 @@ static ULONG high_key[] =
123123

124124
namespace
125125
{
126-
static const char* const SCRATCH = "fb_sort_";
126+
static constexpr const char* SCRATCH = "fb_sort_";
127127

128128
class RunSort
129129
{
130130
public:
131-
explicit RunSort(run_control* irun) : run(irun) {}
132-
RunSort() : run(NULL) {}
131+
explicit RunSort(run_control* irun) noexcept : run(irun) {}
132+
RunSort() noexcept : run(NULL) {}
133133

134-
static FB_UINT64 generate(const RunSort& item)
134+
static FB_UINT64 generate(const RunSort& item) noexcept
135135
{
136136
return item.run->run_seek;
137137
}
138138

139139
run_control* run;
140140
};
141141

142-
inline void swap(SORTP** a, SORTP** b)
142+
inline void swap(SORTP** a, SORTP** b) noexcept
143143
{
144144
((SORTP***) (*a))[BACK_OFFSET] = b;
145145
((SORTP***) (*b))[BACK_OFFSET] = a;
@@ -552,8 +552,8 @@ void Sort::sort(thread_db* tdbb)
552552
// itself allocated memory by at least TempSpace::getMinBlockSize chunks.
553553
// As we need contiguous memory don't ask for bigger parts
554554
const ULONG rec_size = m_longs << SHIFTLONG;
555-
ULONG allocSize = m_max_alloc_size * RUN_GROUP;
556-
ULONG allocated = allocate(run_count, allocSize, true);
555+
const ULONG allocSize = m_max_alloc_size * RUN_GROUP;
556+
const ULONG allocated = allocate(run_count, allocSize, true);
557557

558558
if (allocated < run_count)
559559
{
@@ -1486,7 +1486,8 @@ ULONG Sort::allocate(ULONG n, ULONG chunkSize, bool useFreeSpace)
14861486

14871487
if (segments.getCount())
14881488
{
1489-
TempSpace::SegmentInMemory *seg = segments.begin(), *lastSeg = segments.end();
1489+
auto seg = segments.begin();
1490+
const auto* lastSeg = segments.end();
14901491
for (run = m_runs, count = 0; count < n; run = run->run_next, count++)
14911492
{
14921493
if (!run->run_buffer)
@@ -1720,7 +1721,7 @@ void Sort::mergeRuns(USHORT n)
17201721
}
17211722

17221723

1723-
void Sort::quick(SLONG size, SORTP** pointers, ULONG length)
1724+
void Sort::quick(SLONG size, SORTP** pointers, ULONG length) noexcept
17241725
{
17251726
/**************************************
17261727
*
@@ -2197,7 +2198,7 @@ void SortOwner::unlinkAll()
21972198
{
21982199
// Move cached buffers to the database level cache to be reused later by other attachments
21992200

2200-
const size_t MAX_CACHED_SORT_BUFFERS = 8; // 1MB
2201+
constexpr size_t MAX_CACHED_SORT_BUFFERS = 8; // 1MB
22012202

22022203
SyncLockGuard guard(&dbb->dbb_sortbuf_sync, SYNC_EXCLUSIVE, FB_FUNCTION);
22032204

src/jrd/sort.h

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ struct sort_record
9696

9797
};
9898

99-
const ULONG MAX_SORT_RECORD = 1024 * 1024; // 1MB
99+
inline constexpr ULONG MAX_SORT_RECORD = 1024 * 1024; // 1MB
100100

101101
// the record struct actually contains the keyids etc, and the back_pointer
102102
// which points to the sort_record structure.
@@ -129,32 +129,32 @@ until we are out of records to sort or memory.
129129

130130
// skd_dtype
131131

132-
const int SKD_long = 1;
133-
const int SKD_ulong = 2;
134-
const int SKD_short = 3;
135-
const int SKD_ushort = 4;
136-
const int SKD_text = 5;
137-
const int SKD_float = 6;
138-
const int SKD_double = 7;
139-
const int SKD_quad = 8;
140-
const int SKD_timestamp = 9;
141-
const int SKD_bytes = 10;
142-
const int SKD_varying = 11; // non-international
143-
const int SKD_cstring = 12; // non-international
144-
const int SKD_sql_time = 13;
145-
const int SKD_sql_date = 14;
146-
const int SKD_int64 = 15;
147-
const int SKD_dec64 = 16;
148-
const int SKD_dec128 = 17;
149-
const int SKD_sql_time_tz = 18;
150-
const int SKD_timestamp_tz = 19;
151-
const int SKD_int128 = 20;
132+
inline constexpr int SKD_long = 1;
133+
inline constexpr int SKD_ulong = 2;
134+
inline constexpr int SKD_short = 3;
135+
inline constexpr int SKD_ushort = 4;
136+
inline constexpr int SKD_text = 5;
137+
inline constexpr int SKD_float = 6;
138+
inline constexpr int SKD_double = 7;
139+
inline constexpr int SKD_quad = 8;
140+
inline constexpr int SKD_timestamp = 9;
141+
inline constexpr int SKD_bytes = 10;
142+
inline constexpr int SKD_varying = 11; // non-international
143+
inline constexpr int SKD_cstring = 12; // non-international
144+
inline constexpr int SKD_sql_time = 13;
145+
inline constexpr int SKD_sql_date = 14;
146+
inline constexpr int SKD_int64 = 15;
147+
inline constexpr int SKD_dec64 = 16;
148+
inline constexpr int SKD_dec128 = 17;
149+
inline constexpr int SKD_sql_time_tz = 18;
150+
inline constexpr int SKD_timestamp_tz = 19;
151+
inline constexpr int SKD_int128 = 20;
152152

153153
// skd_flags
154-
const UCHAR SKD_ascending = 0; // default initializer
155-
const UCHAR SKD_descending = 1;
156-
const UCHAR SKD_binary = 2;
157-
const UCHAR SKD_separate_data = 4;
154+
inline constexpr UCHAR SKD_ascending = 0; // default initializer
155+
inline constexpr UCHAR SKD_descending = 1;
156+
inline constexpr UCHAR SKD_binary = 2;
157+
inline constexpr UCHAR SKD_separate_data = 4;
158158

159159
// Sort key definition block
160160

@@ -170,9 +170,9 @@ struct sort_key_def
170170
public:
171171
ULONG skd_vary_offset; // Offset to varying/cstring length
172172

173-
USHORT getSkdLength() const { return skd_length; }
173+
USHORT getSkdLength() const noexcept { return skd_length; }
174174

175-
void setSkdLength(UCHAR dtype, USHORT dscLength)
175+
void setSkdLength(UCHAR dtype, USHORT dscLength) noexcept
176176
{
177177
skd_dtype = dtype;
178178

@@ -191,9 +191,9 @@ struct sort_key_def
191191
}
192192
}
193193

194-
ULONG getSkdOffset() const { return skd_offset; }
194+
ULONG getSkdOffset() const noexcept { return skd_offset; }
195195

196-
void setSkdOffset(const sort_key_def* prev = nullptr, dsc* desc = nullptr)
196+
void setSkdOffset(const sort_key_def* prev = nullptr, dsc* desc = nullptr) noexcept
197197
{
198198
skd_offset = 0;
199199
if (prev)
@@ -220,9 +220,9 @@ struct run_merge_hdr
220220

221221
// rmh_type
222222

223-
const int RMH_TYPE_RUN = 0;
224-
const int RMH_TYPE_MRG = 1;
225-
const int RMH_TYPE_SORT = 2;
223+
inline constexpr int RMH_TYPE_RUN = 0;
224+
inline constexpr int RMH_TYPE_MRG = 1;
225+
inline constexpr int RMH_TYPE_SORT = 2;
226226

227227

228228
// Run control block
@@ -270,8 +270,8 @@ typedef bool (*FPTR_REJECT_DUP_CALLBACK)(const UCHAR*, const UCHAR*, void*);
270270

271271
// flags as set in m_flags
272272

273-
const int scb_sorted = 1; // stream has been sorted
274-
const int scb_reuse_buffer = 2; // reuse buffer if possible
273+
inline constexpr int scb_sorted = 1; // stream has been sorted
274+
inline constexpr int scb_reuse_buffer = 2; // reuse buffer if possible
275275

276276
class Sort
277277
{
@@ -286,7 +286,7 @@ class Sort
286286
void put(Jrd::thread_db*, ULONG**);
287287
void sort(Jrd::thread_db*);
288288

289-
bool isSorted() const
289+
bool isSorted() const noexcept
290290
{
291291
return m_flags & scb_sorted;
292292
}
@@ -298,7 +298,7 @@ class Sort
298298
return seek + bytes;
299299
}
300300

301-
static FB_UINT64 writeBlock(TempSpace* space, FB_UINT64 seek, UCHAR* address, ULONG length)
301+
static FB_UINT64 writeBlock(TempSpace* space, FB_UINT64 seek, const UCHAR* address, ULONG length)
302302
{
303303
const size_t bytes = space->write(seek, address, length);
304304
fb_assert(bytes == length);
@@ -325,7 +325,7 @@ class Sort
325325
void checkFile(const run_control*);
326326
#endif
327327

328-
static void quick(SLONG, SORTP**, ULONG);
328+
static void quick(SLONG, SORTP**, ULONG) noexcept;
329329

330330
Database* m_dbb; // Database
331331
SortOwner* m_owner; // Sort owner
@@ -421,7 +421,7 @@ class SortOwner
421421
}
422422
}
423423

424-
MemoryPool& getPool() const
424+
MemoryPool& getPool() const noexcept
425425
{
426426
return pool;
427427
}

0 commit comments

Comments
 (0)