Commit a6f6b781 authored by Kevin Grittner's avatar Kevin Grittner

Use static inline function for BufferGetPage()

I was initially concerned that the some of the hundreds of
references to BufferGetPage() where the literal
BGP_NO_SNAPSHOT_TEST were passed might not optimize as well as a
macro, leading to some hard-to-find performance regressions in
corner cases.  Inspection of disassembled code has shown identical
code at all inspected locations, and the size difference doesn't
amount to even one byte per such call.  So make it readable.

Per gripes from Álvaro Herrera and Tom Lane
parent 80647bf6
...@@ -4295,7 +4295,7 @@ IssuePendingWritebacks(WritebackContext *context) ...@@ -4295,7 +4295,7 @@ IssuePendingWritebacks(WritebackContext *context)
* For best performance, keep the tests that are fastest and/or most likely to * For best performance, keep the tests that are fastest and/or most likely to
* exclude a page from old snapshot testing near the front. * exclude a page from old snapshot testing near the front.
*/ */
extern Page extern void
TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page) TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
{ {
Assert(relation != NULL); Assert(relation != NULL);
...@@ -4311,6 +4311,4 @@ TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page) ...@@ -4311,6 +4311,4 @@ TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_SNAPSHOT_TOO_OLD), (errcode(ERRCODE_SNAPSHOT_TOO_OLD),
errmsg("snapshot too old"))); errmsg("snapshot too old")));
return page;
} }
...@@ -177,31 +177,6 @@ extern PGDLLIMPORT int32 *LocalRefCount; ...@@ -177,31 +177,6 @@ extern PGDLLIMPORT int32 *LocalRefCount;
(Size)BLCKSZ \ (Size)BLCKSZ \
) )
/*
* BufferGetPage
* Returns the page associated with a buffer.
*
* agetest will normally be a literal, so use a macro at the outer level to
* give the compiler a chance to optimize away the runtime code to check it.
*
* TestForOldSnapshot(), if it doesn't throw an error, will return the page
* argument it is passed, so the same result will go back to this macro's
* caller for either agetest value; it is a matter of whether to call the
* function to perform the test. For call sites where the check is not needed
* (which is the vast majority of them), the snapshot and relation parameters
* can, and generally should, be NULL.
*/
#define BufferGetPage(buffer, snapshot, relation, agetest) \
( \
( \
AssertMacro((agetest) == BGP_NO_SNAPSHOT_TEST || (agetest) == BGP_TEST_FOR_OLD_SNAPSHOT), \
((agetest) == BGP_NO_SNAPSHOT_TEST) \
) ? \
((Page)BufferGetBlock(buffer)) \
: \
(TestForOldSnapshot(snapshot, relation, (Page)BufferGetBlock(buffer))) \
)
/* /*
* prototypes for functions in bufmgr.c * prototypes for functions in bufmgr.c
*/ */
...@@ -268,10 +243,33 @@ extern bool BgBufferSync(struct WritebackContext *wb_context); ...@@ -268,10 +243,33 @@ extern bool BgBufferSync(struct WritebackContext *wb_context);
extern void AtProcExit_LocalBuffers(void); extern void AtProcExit_LocalBuffers(void);
extern Page TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page); extern void TestForOldSnapshot(Snapshot snapshot, Relation relation, Page page);
/* in freelist.c */ /* in freelist.c */
extern BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype); extern BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype);
extern void FreeAccessStrategy(BufferAccessStrategy strategy); extern void FreeAccessStrategy(BufferAccessStrategy strategy);
/* inline functions */
/*
* BufferGetPage
* Returns the page associated with a buffer.
*
* For call sites where the check is not needed (which is the vast majority of
* them), the snapshot and relation parameters can, and generally should, be
* NULL.
*/
static inline Page
BufferGetPage(Buffer buffer, Snapshot snapshot, Relation relation,
BufferGetPageAgeTest agetest)
{
Page page = (Page) BufferGetBlock(buffer);
if (agetest == BGP_TEST_FOR_OLD_SNAPSHOT)
TestForOldSnapshot(snapshot, relation, page);
return page;
}
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment