From b7a3c4526990d6601ff2cac748ff860cda6bc16d Mon Sep 17 00:00:00 2001 From: vaxerski Date: Fri, 17 Jan 2025 18:21:34 +0100 Subject: core: add LIKELY and UNLIKELY macros helps the compiler optimize --- src/protocols/core/Compositor.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'src/protocols/core/Compositor.cpp') diff --git a/src/protocols/core/Compositor.cpp b/src/protocols/core/Compositor.cpp index e1d6ef7d..93683993 100644 --- a/src/protocols/core/Compositor.cpp +++ b/src/protocols/core/Compositor.cpp @@ -34,7 +34,7 @@ void CWLCallbackResource::send(timespec* now) { } CWLRegionResource::CWLRegionResource(SP resource_) : resource(resource_) { - if (!good()) + if UNLIKELY (!good()) return; resource->setData(this); @@ -56,7 +56,7 @@ SP CWLRegionResource::fromResource(wl_resource* res) { } CWLSurfaceResource::CWLSurfaceResource(SP resource_) : resource(resource_) { - if (!good()) + if UNLIKELY (!good()) return; pClient = resource->client(); @@ -187,20 +187,20 @@ void CWLSurfaceResource::enter(PHLMONITOR monitor) { if (std::find(enteredOutputs.begin(), enteredOutputs.end(), monitor) != enteredOutputs.end()) return; - if (!PROTO::outputs.contains(monitor->szName)) { + if UNLIKELY (!PROTO::outputs.contains(monitor->szName)) { // can happen on unplug/replug LOGM(ERR, "enter() called on a non-existent output global"); return; } - if (PROTO::outputs.at(monitor->szName)->isDefunct()) { + if UNLIKELY (PROTO::outputs.at(monitor->szName)->isDefunct()) { LOGM(ERR, "enter() called on a defunct output global"); return; } auto output = PROTO::outputs.at(monitor->szName)->outputResourceFrom(pClient); - if (!output || !output->getResource() || !output->getResource()->resource()) { + if UNLIKELY (!output || !output->getResource() || !output->getResource()->resource()) { LOGM(ERR, "Cannot enter surface {:x} to {}, client hasn't bound the output", (uintptr_t)this, monitor->szName); return; } @@ -211,12 +211,12 @@ void CWLSurfaceResource::enter(PHLMONITOR monitor) { } void CWLSurfaceResource::leave(PHLMONITOR monitor) { - if (std::find(enteredOutputs.begin(), enteredOutputs.end(), monitor) == enteredOutputs.end()) + if UNLIKELY (std::find(enteredOutputs.begin(), enteredOutputs.end(), monitor) == enteredOutputs.end()) return; auto output = PROTO::outputs.at(monitor->szName)->outputResourceFrom(pClient); - if (!output) { + if UNLIKELY (!output) { LOGM(ERR, "Cannot leave surface {:x} from {}, client hasn't bound the output", (uintptr_t)this, monitor->szName); return; } @@ -332,7 +332,7 @@ uint32_t CWLSurfaceResource::id() { } void CWLSurfaceResource::map() { - if (mapped) + if UNLIKELY (mapped) return; mapped = true; @@ -346,7 +346,7 @@ void CWLSurfaceResource::map() { } void CWLSurfaceResource::unmap() { - if (!mapped) + if UNLIKELY (!mapped) return; mapped = false; @@ -385,10 +385,10 @@ CBox CWLSurfaceResource::extends() { } Vector2D CWLSurfaceResource::sourceSize() { - if (!current.texture) + if UNLIKELY (!current.texture) return {}; - if (current.viewport.hasSource) + if UNLIKELY (current.viewport.hasSource) return current.viewport.source.size(); Vector2D trc = current.transform % 2 == 1 ? Vector2D{current.bufferSize.y, current.bufferSize.x} : current.bufferSize; @@ -396,7 +396,7 @@ Vector2D CWLSurfaceResource::sourceSize() { } CRegion CWLSurfaceResource::accumulateCurrentBufferDamage() { - if (!current.texture) + if UNLIKELY (!current.texture) return {}; CRegion surfaceDamage = current.damage; @@ -495,7 +495,7 @@ void CWLSurfaceResource::commitPendingState() { void CWLSurfaceResource::updateCursorShm(CRegion damage) { auto buf = current.buffer ? current.buffer->buffer : lastBuffer; - if (!buf) + if UNLIKELY (!buf) return; auto& shmData = CCursorSurfaceRole::cursorPixelData(self.lock()); @@ -545,7 +545,7 @@ void CWLSurfaceResource::presentFeedback(timespec* when, PHLMONITOR pMonitor, bo } CWLCompositorResource::CWLCompositorResource(SP resource_) : resource(resource_) { - if (!good()) + if UNLIKELY (!good()) return; resource->setOnDestroy([this](CWlCompositor* r) { PROTO::compositor->destroyResource(this); }); @@ -553,7 +553,7 @@ CWLCompositorResource::CWLCompositorResource(SP resource_) : reso resource->setCreateSurface([](CWlCompositor* r, uint32_t id) { const auto RESOURCE = PROTO::compositor->m_vSurfaces.emplace_back(makeShared(makeShared(r->client(), r->version(), id))); - if (!RESOURCE->good()) { + if UNLIKELY (!RESOURCE->good()) { r->noMemory(); PROTO::compositor->m_vSurfaces.pop_back(); return; @@ -569,7 +569,7 @@ CWLCompositorResource::CWLCompositorResource(SP resource_) : reso resource->setCreateRegion([](CWlCompositor* r, uint32_t id) { const auto RESOURCE = PROTO::compositor->m_vRegions.emplace_back(makeShared(makeShared(r->client(), r->version(), id))); - if (!RESOURCE->good()) { + if UNLIKELY (!RESOURCE->good()) { r->noMemory(); PROTO::compositor->m_vRegions.pop_back(); return; @@ -592,7 +592,7 @@ CWLCompositorProtocol::CWLCompositorProtocol(const wl_interface* iface, const in void CWLCompositorProtocol::bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) { const auto RESOURCE = m_vManagers.emplace_back(makeShared(makeShared(client, ver, id))); - if (!RESOURCE->good()) { + if UNLIKELY (!RESOURCE->good()) { wl_client_post_no_memory(client); m_vManagers.pop_back(); return; -- cgit v1.2.3