Skip to content

Commit 2c48a4e

Browse files
committed
Add CachedRequestId mechanism for internal request cache.
1 parent 0cc8de3 commit 2c48a4e

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

src/jrd/Attachment.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ Jrd::Attachment::Attachment(MemoryPool* pool, Database* dbb, JProvider* provider
265265
att_generators(*pool),
266266
att_internal(*pool),
267267
att_dyn_req(*pool),
268+
att_internal_cached_statements(*pool),
268269
att_dec_status(DecimalStatus::DEFAULT),
269270
att_charsets(*pool),
270271
att_charset_ids(*pool),
@@ -681,9 +682,15 @@ Request* Jrd::Attachment::findSystemRequest(thread_db* tdbb, USHORT id, USHORT w
681682

682683
//Database::CheckoutLockGuard guard(this, dbb_cmp_clone_mutex);
683684

684-
fb_assert(which == IRQ_REQUESTS || which == DYN_REQUESTS);
685+
fb_assert(which == IRQ_REQUESTS || which == DYN_REQUESTS || which == CACHED_REQUESTS);
685686

686-
Statement* statement = (which == IRQ_REQUESTS ? att_internal[id] : att_dyn_req[id]);
687+
if (which == CACHED_REQUESTS && id >= att_internal_cached_statements.getCount())
688+
att_internal_cached_statements.grow(id + 1);
689+
690+
Statement* statement =
691+
which == IRQ_REQUESTS ? att_internal[id] :
692+
which == DYN_REQUESTS ? att_dyn_req[id] :
693+
att_internal_cached_statements[id];
687694

688695
if (!statement)
689696
return NULL;

src/jrd/Attachment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ class Attachment : public pool_alloc<type_att>
666666

667667
Firebird::Array<Statement*> att_internal; // internal statements
668668
Firebird::Array<Statement*> att_dyn_req; // internal dyn statements
669+
Firebird::Array<Statement*> att_internal_cached_statements; // internal cached statements
669670
Firebird::ICryptKeyCallback* att_crypt_callback; // callback for DB crypt
670671
Firebird::DecimalStatus att_dec_status; // error handling and rounding
671672

src/jrd/exe.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,8 +1897,15 @@ void AutoCacheRequest::cacheRequest()
18971897
thread_db* tdbb = JRD_get_thread_data();
18981898
Attachment* att = tdbb->getAttachment();
18991899

1900-
Statement** stmt = which == IRQ_REQUESTS ? &att->att_internal[id] :
1901-
which == DYN_REQUESTS ? &att->att_dyn_req[id] : nullptr;
1900+
if (which == CACHED_REQUESTS && id >= att->att_internal_cached_statements.getCount())
1901+
att->att_internal_cached_statements.grow(id + 1);
1902+
1903+
Statement** stmt =
1904+
which == IRQ_REQUESTS ? &att->att_internal[id] :
1905+
which == DYN_REQUESTS ? &att->att_dyn_req[id] :
1906+
which == CACHED_REQUESTS ? &att->att_internal_cached_statements[id] :
1907+
nullptr;
1908+
19021909
if (!stmt)
19031910
{
19041911
fb_assert(false);

src/jrd/exe_proto.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define JRD_EXE_PROTO_H
2626

2727
#include "../jrd/cmp_proto.h"
28+
#include <atomic>
2829

2930
namespace Jrd {
3031
class Request;
@@ -58,6 +59,29 @@ void EXE_unwind(Jrd::thread_db*, Jrd::Request*);
5859

5960
namespace Jrd
6061
{
62+
class CachedRequestId
63+
{
64+
public:
65+
CachedRequestId()
66+
: id(generator++)
67+
{
68+
fb_assert(id <= MAX_USHORT);
69+
}
70+
71+
CachedRequestId(const CachedRequestId&) = delete;
72+
CachedRequestId& operator=(const CachedRequestId&) = delete;
73+
74+
public:
75+
USHORT getId() const
76+
{
77+
return id;
78+
}
79+
80+
private:
81+
unsigned id;
82+
static inline std::atomic<unsigned> generator;
83+
};
84+
6185
// ASF: To make this class MT-safe in SS for v3, it should be AutoCacheRequest::release job to
6286
// inform CMP that the request is available for subsequent usage.
6387
class AutoCacheRequest
@@ -70,6 +94,13 @@ namespace Jrd
7094
{
7195
}
7296

97+
AutoCacheRequest(thread_db* tdbb, const CachedRequestId& cachedRequestId)
98+
: id(cachedRequestId.getId()),
99+
which(CACHED_REQUESTS),
100+
request(tdbb->getAttachment()->findSystemRequest(tdbb, id, which))
101+
{
102+
}
103+
73104
AutoCacheRequest()
74105
: id(0),
75106
which(0),
@@ -92,6 +123,15 @@ namespace Jrd
92123
request = tdbb->getAttachment()->findSystemRequest(tdbb, id, which);
93124
}
94125

126+
void reset(thread_db* tdbb, const CachedRequestId& cachedRequestId)
127+
{
128+
release();
129+
130+
id = cachedRequestId.getId();
131+
which = CACHED_REQUESTS;
132+
request = tdbb->getAttachment()->findSystemRequest(tdbb, id, which);
133+
}
134+
95135
void compile(thread_db* tdbb, const UCHAR* blr, ULONG blrLength)
96136
{
97137
if (request)

src/jrd/jrd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ class TrigVector : public Firebird::ObjectsArray<Trigger>
205205
//
206206
// Flags to indicate normal internal requests vs. dyn internal requests
207207
//
208+
// IRQ_REQUESTS and DYN_REQUESTS are deprecated
208209
const int IRQ_REQUESTS = 1;
209210
const int DYN_REQUESTS = 2;
211+
const int CACHED_REQUESTS = 3;
210212

211213

212214
// Procedure block

0 commit comments

Comments
 (0)