From 1aa19dab1817fec39eacc5aa1f5129f789c3ae4e Mon Sep 17 00:00:00 2001
From: Florian Schmaus <flow@cs.fau.de>
Date: Wed, 9 Dec 2020 12:51:30 +0100
Subject: [PATCH] Re-use the result of Runtime::getWorkerId() when possible

Runtime::getWorkerId() may be an expensive operation, hence re-use the
result of the method when possible.
---
 emper/MemoryManager.hpp             | 3 ++-
 emper/strategies/ws/WsScheduler.cpp | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/emper/MemoryManager.hpp b/emper/MemoryManager.hpp
index c25d0ab8..d9572eb7 100644
--- a/emper/MemoryManager.hpp
+++ b/emper/MemoryManager.hpp
@@ -40,12 +40,13 @@ class MemoryManager {
 		if (likely(poped)) return memory;
 
 		// Fallback to memory-stealing.
+		const workerid_t myWorkerId = Runtime::getWorkerId();
 		const workerid_t startWorkerId = Runtime::rand() % workerCount;
 		for (workerid_t i = 0; i < workerCount; ++i) {
 			workerid_t victim = (startWorkerId + i) % workerCount;
 
 			// Don't steal from ourselves.
-			if (unlikely(victim == Runtime::getWorkerId())) continue;
+			if (unlikely(victim == myWorkerId)) continue;
 
 			poped = queues[victim]->popTop(&memory);
 			if (poped) return memory;
diff --git a/emper/strategies/ws/WsScheduler.cpp b/emper/strategies/ws/WsScheduler.cpp
index 38636e34..2cb8a1d1 100644
--- a/emper/strategies/ws/WsScheduler.cpp
+++ b/emper/strategies/ws/WsScheduler.cpp
@@ -54,6 +54,7 @@ auto WsScheduler::nextFiber() -> Fiber* {
 		return fiber;
 	}
 
+	const workerid_t myWorkerId = Runtime::getWorkerId();
 	const workerid_t workerCount = runtime.getWorkerCount();
 	workerid_t startWorkerId = Runtime::rand() % workerCount;
 	// TODO: See how reducing the loop bound affects things.
@@ -61,7 +62,7 @@ auto WsScheduler::nextFiber() -> Fiber* {
 		workerid_t victim = (startWorkerId + i) % workerCount;
 
 		// Don't steal from ourselves.
-		if (unlikely(victim == Runtime::getWorkerId())) continue;
+		if (unlikely(victim == myWorkerId)) continue;
 
 		poped = queues[victim]->popTop(&fiber);
 		if (poped) {
-- 
GitLab