Skip to content

Commit

Permalink
core: add LIKELY and UNLIKELY macros
Browse files Browse the repository at this point in the history
helps the compiler optimize
  • Loading branch information
vaxerski committed Jan 17, 2025
1 parent 2bad733 commit b7a3c45
Show file tree
Hide file tree
Showing 58 changed files with 395 additions and 396 deletions.
2 changes: 1 addition & 1 deletion src/devices/VirtualPointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SP<CVirtualPointer> CVirtualPointer::create(SP<CVirtualPointerV1Resource> resour
}

CVirtualPointer::CVirtualPointer(SP<CVirtualPointerV1Resource> resource) : pointer(resource) {
if (!resource->good())
if UNLIKELY (!resource->good())
return;

listeners.destroy = pointer->events.destroy.registerListener([this](std::any d) {
Expand Down
3 changes: 3 additions & 0 deletions src/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,6 @@
namespace Aquamarine { \
class name; \
}

#define UNLIKELY(expr) (expr) [[unlikely]]
#define LIKELY(expr) (expr) [[likely]]
4 changes: 2 additions & 2 deletions src/protocols/AlphaModifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bool CAlphaModifier::good() {
void CAlphaModifier::setResource(SP<CWpAlphaModifierSurfaceV1> resource) {
m_pResource = std::move(resource);

if (!m_pResource->resource())
if UNLIKELY (!m_pResource->resource())
return;

m_pResource->setDestroy([this](CWpAlphaModifierSurfaceV1* resource) { destroy(); });
Expand Down Expand Up @@ -98,7 +98,7 @@ void CAlphaModifierProtocol::getSurface(CWpAlphaModifierV1* manager, uint32_t id
.first->second.get();
}

if (!alphaModifier->good()) {
if UNLIKELY (!alphaModifier->good()) {
manager->noMemory();
m_mAlphaModifiers.erase(surface);
}
Expand Down
8 changes: 4 additions & 4 deletions src/protocols/CTMControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "../helpers/Monitor.hpp"

CHyprlandCTMControlResource::CHyprlandCTMControlResource(SP<CHyprlandCtmControlManagerV1> resource_) : resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;

resource->setDestroy([this](CHyprlandCtmControlManagerV1* pMgr) { PROTO::ctm->destroyResource(this); });
Expand All @@ -17,12 +17,12 @@ CHyprlandCTMControlResource::CHyprlandCTMControlResource(SP<CHyprlandCtmControlM
wl_fixed_t mat5, wl_fixed_t mat6, wl_fixed_t mat7, wl_fixed_t mat8) {
const auto OUTPUTRESOURCE = CWLOutputResource::fromResource(output);

if (!OUTPUTRESOURCE)
if UNLIKELY (!OUTPUTRESOURCE)
return; // ?!

const auto PMONITOR = OUTPUTRESOURCE->monitor.lock();

if (!PMONITOR)
if UNLIKELY (!PMONITOR)
return; // ?!?!

const std::array<float, 9> MAT = {wl_fixed_to_double(mat0), wl_fixed_to_double(mat1), wl_fixed_to_double(mat2), wl_fixed_to_double(mat3), wl_fixed_to_double(mat4),
Expand Down Expand Up @@ -72,7 +72,7 @@ void CHyprlandCTMControlProtocol::bindManager(wl_client* client, void* data, uin

const auto RESOURCE = m_vManagers.emplace_back(makeShared<CHyprlandCTMControlResource>(makeShared<CHyprlandCtmControlManagerV1>(client, ver, id)));

if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
wl_client_post_no_memory(client);
m_vManagers.pop_back();
return;
Expand Down
32 changes: 16 additions & 16 deletions src/protocols/ColorManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "Compositor.hpp"

CColorManager::CColorManager(SP<CXxColorManagerV4> resource_) : resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;

resource->sendSupportedFeature(XX_COLOR_MANAGER_V4_FEATURE_PARAMETRIC);
Expand Down Expand Up @@ -38,7 +38,7 @@ CColorManager::CColorManager(SP<CXxColorManagerV4> resource_) : resource(resourc
const auto RESOURCE =
PROTO::colorManagement->m_vOutputs.emplace_back(makeShared<CColorManagementOutput>(makeShared<CXxColorManagementOutputV4>(r->client(), r->version(), id)));

if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
r->noMemory();
PROTO::colorManagement->m_vOutputs.pop_back();
return;
Expand All @@ -63,7 +63,7 @@ CColorManager::CColorManager(SP<CXxColorManagerV4> resource_) : resource(resourc

const auto RESOURCE =
PROTO::colorManagement->m_vSurfaces.emplace_back(makeShared<CColorManagementSurface>(makeShared<CXxColorManagementSurfaceV4>(r->client(), r->version(), id), SURF));
if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
r->noMemory();
PROTO::colorManagement->m_vSurfaces.pop_back();
return;
Expand All @@ -86,7 +86,7 @@ CColorManager::CColorManager(SP<CXxColorManagerV4> resource_) : resource(resourc
const auto RESOURCE = PROTO::colorManagement->m_vFeedbackSurfaces.emplace_back(
makeShared<CColorManagementFeedbackSurface>(makeShared<CXxColorManagementFeedbackSurfaceV4>(r->client(), r->version(), id), SURF));

if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
r->noMemory();
PROTO::colorManagement->m_vFeedbackSurfaces.pop_back();
return;
Expand All @@ -104,7 +104,7 @@ CColorManager::CColorManager(SP<CXxColorManagerV4> resource_) : resource(resourc
const auto RESOURCE = PROTO::colorManagement->m_vParametricCreators.emplace_back(
makeShared<CColorManagementParametricCreator>(makeShared<CXxImageDescriptionCreatorParamsV4>(r->client(), r->version(), id)));

if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
r->noMemory();
PROTO::colorManagement->m_vParametricCreators.pop_back();
return;
Expand All @@ -121,7 +121,7 @@ bool CColorManager::good() {
}

CColorManagementOutput::CColorManagementOutput(SP<CXxColorManagementOutputV4> resource_) : resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;

pClient = resource->client();
Expand All @@ -137,7 +137,7 @@ CColorManagementOutput::CColorManagementOutput(SP<CXxColorManagementOutputV4> re
const auto RESOURCE = PROTO::colorManagement->m_vImageDescriptions.emplace_back(
makeShared<CColorManagementImageDescription>(makeShared<CXxImageDescriptionV4>(r->client(), r->version(), id)));

if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
r->noMemory();
PROTO::colorManagement->m_vImageDescriptions.pop_back();
return;
Expand All @@ -160,7 +160,7 @@ CColorManagementSurface::CColorManagementSurface(SP<CWLSurfaceResource> surface_
}

CColorManagementSurface::CColorManagementSurface(SP<CXxColorManagementSurfaceV4> resource_, SP<CWLSurfaceResource> surface_) : surface(surface_), resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;

pClient = resource->client();
Expand Down Expand Up @@ -242,7 +242,7 @@ bool CColorManagementSurface::needsHdrMetadataUpdate() {

CColorManagementFeedbackSurface::CColorManagementFeedbackSurface(SP<CXxColorManagementFeedbackSurfaceV4> resource_, SP<CWLSurfaceResource> surface_) :
surface(surface_), resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;

pClient = resource->client();
Expand All @@ -269,7 +269,7 @@ CColorManagementFeedbackSurface::CColorManagementFeedbackSurface(SP<CXxColorMana
const auto RESOURCE = PROTO::colorManagement->m_vImageDescriptions.emplace_back(
makeShared<CColorManagementImageDescription>(makeShared<CXxImageDescriptionV4>(r->client(), r->version(), id), true));

if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
r->noMemory();
PROTO::colorManagement->m_vImageDescriptions.pop_back();
return;
Expand All @@ -293,7 +293,7 @@ wl_client* CColorManagementFeedbackSurface::client() {
}

CColorManagementParametricCreator::CColorManagementParametricCreator(SP<CXxImageDescriptionCreatorParamsV4> resource_) : resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;
//
pClient = resource->client();
Expand All @@ -318,7 +318,7 @@ CColorManagementParametricCreator::CColorManagementParametricCreator(SP<CXxImage
const auto RESOURCE = PROTO::colorManagement->m_vImageDescriptions.emplace_back(
makeShared<CColorManagementImageDescription>(makeShared<CXxImageDescriptionV4>(r->client(), r->version(), id)));

if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
r->noMemory();
PROTO::colorManagement->m_vImageDescriptions.pop_back();
return;
Expand Down Expand Up @@ -465,7 +465,7 @@ wl_client* CColorManagementParametricCreator::client() {

CColorManagementImageDescription::CColorManagementImageDescription(SP<CXxImageDescriptionV4> resource_, bool allowGetInformation) :
m_resource(resource_), m_allowGetInformation(allowGetInformation) {
if (!good())
if UNLIKELY (!good())
return;

pClient = m_resource->client();
Expand All @@ -482,7 +482,7 @@ CColorManagementImageDescription::CColorManagementImageDescription(SP<CXxImageDe

auto RESOURCE = makeShared<CColorManagementImageDescriptionInfo>(makeShared<CXxImageDescriptionInfoV4>(r->client(), r->version(), id), settings);

if (!RESOURCE->good())
if UNLIKELY (!RESOURCE->good())
r->noMemory();

// CColorManagementImageDescriptionInfo should send everything in the constructor and be ready for destroying at this point
Expand All @@ -504,7 +504,7 @@ SP<CXxImageDescriptionV4> CColorManagementImageDescription::resource() {

CColorManagementImageDescriptionInfo::CColorManagementImageDescriptionInfo(SP<CXxImageDescriptionInfoV4> resource_, const SImageDescription& settings_) :
m_resource(resource_), settings(settings_) {
if (!good())
if UNLIKELY (!good())
return;

pClient = m_resource->client();
Expand Down Expand Up @@ -549,7 +549,7 @@ CColorManagementProtocol::CColorManagementProtocol(const wl_interface* iface, co
void CColorManagementProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
const auto RESOURCE = m_vManagers.emplace_back(makeShared<CColorManager>(makeShared<CXxColorManagerV4>(client, ver, id)));

if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
wl_client_post_no_memory(client);
m_vManagers.pop_back();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/protocols/CursorShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void CCursorShapeProtocol::createCursorShapeDevice(CWpCursorShapeManagerV1* pMgr
}

void CCursorShapeProtocol::onSetShape(CWpCursorShapeDeviceV1* pMgr, uint32_t serial, wpCursorShapeDeviceV1Shape shape) {
if ((uint32_t)shape == 0 || (uint32_t)shape > CURSOR_SHAPE_NAMES.size()) {
if UNLIKELY ((uint32_t)shape == 0 || (uint32_t)shape > CURSOR_SHAPE_NAMES.size()) {
pMgr->error(WP_CURSOR_SHAPE_DEVICE_V1_ERROR_INVALID_SHAPE, "The shape is invalid");
return;
}
Expand Down
16 changes: 8 additions & 8 deletions src/protocols/DRMLease.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <fcntl.h>

CDRMLeaseResource::CDRMLeaseResource(SP<CWpDrmLeaseV1> resource_, SP<CDRMLeaseRequestResource> request) : resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;

resource->setOnDestroy([this](CWpDrmLeaseV1* r) { PROTO::lease->destroyResource(this); });
Expand Down Expand Up @@ -79,7 +79,7 @@ CDRMLeaseResource::~CDRMLeaseResource() {
}

CDRMLeaseRequestResource::CDRMLeaseRequestResource(SP<CWpDrmLeaseRequestV1> resource_) : resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;

resource->setOnDestroy([this](CWpDrmLeaseRequestV1* r) { PROTO::lease->destroyResource(this); });
Expand Down Expand Up @@ -109,7 +109,7 @@ CDRMLeaseRequestResource::CDRMLeaseRequestResource(SP<CWpDrmLeaseRequestV1> reso
}

auto RESOURCE = makeShared<CDRMLeaseResource>(makeShared<CWpDrmLeaseV1>(resource->client(), resource->version(), id), self.lock());
if (!RESOURCE) {
if UNLIKELY (!RESOURCE) {
resource->noMemory();
return;
}
Expand All @@ -131,7 +131,7 @@ SP<CDRMLeaseConnectorResource> CDRMLeaseConnectorResource::fromResource(wl_resou
}

CDRMLeaseConnectorResource::CDRMLeaseConnectorResource(SP<CWpDrmLeaseConnectorV1> resource_, PHLMONITOR monitor_) : monitor(monitor_), resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;

resource->setOnDestroy([this](CWpDrmLeaseConnectorV1* r) { PROTO::lease->destroyResource(this); });
Expand Down Expand Up @@ -160,15 +160,15 @@ void CDRMLeaseConnectorResource::sendData() {
}

CDRMLeaseDeviceResource::CDRMLeaseDeviceResource(SP<CWpDrmLeaseDeviceV1> resource_) : resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;

resource->setOnDestroy([this](CWpDrmLeaseDeviceV1* r) { PROTO::lease->destroyResource(this); });
resource->setRelease([this](CWpDrmLeaseDeviceV1* r) { PROTO::lease->destroyResource(this); });

resource->setCreateLeaseRequest([this](CWpDrmLeaseDeviceV1* r, uint32_t id) {
auto RESOURCE = makeShared<CDRMLeaseRequestResource>(makeShared<CWpDrmLeaseRequestV1>(resource->client(), resource->version(), id));
if (!RESOURCE) {
if UNLIKELY (!RESOURCE) {
resource->noMemory();
return;
}
Expand Down Expand Up @@ -209,7 +209,7 @@ void CDRMLeaseDeviceResource::sendConnector(PHLMONITOR monitor) {
return;

auto RESOURCE = makeShared<CDRMLeaseConnectorResource>(makeShared<CWpDrmLeaseConnectorV1>(resource->client(), resource->version(), 0), monitor);
if (!RESOURCE) {
if UNLIKELY (!RESOURCE) {
resource->noMemory();
return;
}
Expand Down Expand Up @@ -262,7 +262,7 @@ CDRMLeaseProtocol::CDRMLeaseProtocol(const wl_interface* iface, const int& ver,
void CDRMLeaseProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
const auto RESOURCE = m_vManagers.emplace_back(makeShared<CDRMLeaseDeviceResource>(makeShared<CWpDrmLeaseDeviceV1>(client, ver, id)));

if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
wl_client_post_no_memory(client);
m_vManagers.pop_back();
return;
Expand Down
18 changes: 9 additions & 9 deletions src/protocols/DRMSyncobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <fcntl.h>

CDRMSyncobjSurfaceResource::CDRMSyncobjSurfaceResource(SP<CWpLinuxDrmSyncobjSurfaceV1> resource_, SP<CWLSurfaceResource> surface_) : surface(surface_), resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;

resource->setData(this);
Expand Down Expand Up @@ -104,7 +104,7 @@ bool CDRMSyncobjSurfaceResource::good() {
}

CDRMSyncobjTimelineResource::CDRMSyncobjTimelineResource(SP<CWpLinuxDrmSyncobjTimelineV1> resource_, int fd_) : fd(fd_), resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;

resource->setData(this);
Expand Down Expand Up @@ -135,31 +135,31 @@ bool CDRMSyncobjTimelineResource::good() {
}

CDRMSyncobjManagerResource::CDRMSyncobjManagerResource(SP<CWpLinuxDrmSyncobjManagerV1> resource_) : resource(resource_) {
if (!good())
if UNLIKELY (!good())
return;

resource->setOnDestroy([this](CWpLinuxDrmSyncobjManagerV1* r) { PROTO::sync->destroyResource(this); });
resource->setDestroy([this](CWpLinuxDrmSyncobjManagerV1* r) { PROTO::sync->destroyResource(this); });

resource->setGetSurface([this](CWpLinuxDrmSyncobjManagerV1* r, uint32_t id, wl_resource* surf) {
if (!surf) {
if UNLIKELY (!surf) {
resource->error(-1, "Invalid surface");
return;
}

auto SURF = CWLSurfaceResource::fromResource(surf);
if (!SURF) {
if UNLIKELY (!SURF) {
resource->error(-1, "Invalid surface (2)");
return;
}

if (SURF->syncobj) {
if UNLIKELY (SURF->syncobj) {
resource->error(WP_LINUX_DRM_SYNCOBJ_MANAGER_V1_ERROR_SURFACE_EXISTS, "Surface already has a syncobj attached");
return;
}

auto RESOURCE = makeShared<CDRMSyncobjSurfaceResource>(makeShared<CWpLinuxDrmSyncobjSurfaceV1>(resource->client(), resource->version(), id), SURF);
if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
resource->noMemory();
return;
}
Expand All @@ -172,7 +172,7 @@ CDRMSyncobjManagerResource::CDRMSyncobjManagerResource(SP<CWpLinuxDrmSyncobjMana

resource->setImportTimeline([this](CWpLinuxDrmSyncobjManagerV1* r, uint32_t id, int32_t fd) {
auto RESOURCE = makeShared<CDRMSyncobjTimelineResource>(makeShared<CWpLinuxDrmSyncobjTimelineV1>(resource->client(), resource->version(), id), fd);
if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
resource->noMemory();
return;
}
Expand All @@ -195,7 +195,7 @@ CDRMSyncobjProtocol::CDRMSyncobjProtocol(const wl_interface* iface, const int& v
void CDRMSyncobjProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) {
const auto RESOURCE = m_vManagers.emplace_back(makeShared<CDRMSyncobjManagerResource>(makeShared<CWpLinuxDrmSyncobjManagerV1>(client, ver, id)));

if (!RESOURCE->good()) {
if UNLIKELY (!RESOURCE->good()) {
wl_client_post_no_memory(client);
m_vManagers.pop_back();
return;
Expand Down
Loading

0 comments on commit b7a3c45

Please sign in to comment.