From 14001aad70f2e42ec0a1c6fabfe8786cd82e52d7 Mon Sep 17 00:00:00 2001
From: Oliver Herms <oliver.herms@exaring.de>
Date: Fri, 4 May 2018 23:07:33 +0200
Subject: [PATCH] Improving performance of bgp path manager

---
 route/bgp_path_manager.go | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/route/bgp_path_manager.go b/route/bgp_path_manager.go
index a68aa960..f846d3b6 100644
--- a/route/bgp_path_manager.go
+++ b/route/bgp_path_manager.go
@@ -23,12 +23,13 @@ func NewBGPPathManager() *BGPPathManager {
 	return m
 }
 
-func (m *BGPPathManager) pathExists(p BGPPath) bool {
-	if _, ok := m.paths[p]; !ok {
-		return false
+func (m *BGPPathManager) lookup(p BGPPath) *BGPPath {
+	pathCounter, ok := m.paths[p]
+	if !ok {
+		return nil
 	}
 
-	return true
+	return pathCounter.path
 }
 
 // AddPath adds a path to the cache if it doesn't exist. If it exist a pointer to the cached object is returned.
@@ -36,7 +37,8 @@ func (m *BGPPathManager) AddPath(p BGPPath) *BGPPath {
 	m.mu.Lock()
 	defer m.mu.Unlock()
 
-	if !m.pathExists(p) {
+	q := m.lookup(p)
+	if q == nil {
 		m.paths[p] = &BGPPathCounter{
 			path: &p,
 		}
@@ -51,7 +53,7 @@ func (m *BGPPathManager) RemovePath(p BGPPath) {
 	m.mu.Lock()
 	defer m.mu.Unlock()
 
-	if !m.pathExists(p) {
+	if m.lookup(p) == nil {
 		log.Fatalf("Tried to remove non-existent BGPPath: %v", p)
 		return
 	}
-- 
GitLab