mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	reduce allocations in switch
This commit is contained in:
		
							parent
							
								
									522ed147b1
								
							
						
					
					
						commit
						9c01947b1c
					
				
					 1 changed files with 16 additions and 10 deletions
				
			
		| 
						 | 
					@ -577,23 +577,28 @@ func (t *switchTable) start() error {
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type closerInfo struct {
 | 
				
			||||||
 | 
						port switchPort
 | 
				
			||||||
 | 
						dist int
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Return a map of ports onto distance, keeping only ports closer to the destination than this node
 | 
					// Return a map of ports onto distance, keeping only ports closer to the destination than this node
 | 
				
			||||||
// If the map is empty (or nil), then no peer is closer
 | 
					// If the map is empty (or nil), then no peer is closer
 | 
				
			||||||
func (t *switchTable) getCloser(dest []byte) map[switchPort]int {
 | 
					func (t *switchTable) getCloser(dest []byte) []closerInfo {
 | 
				
			||||||
	table := t.getTable()
 | 
						table := t.getTable()
 | 
				
			||||||
	myDist := table.self.dist(dest)
 | 
						myDist := table.self.dist(dest)
 | 
				
			||||||
	if myDist == 0 {
 | 
						if myDist == 0 {
 | 
				
			||||||
		// Skip the iteration step if it's impossible to be closer
 | 
							// Skip the iteration step if it's impossible to be closer
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	closer := make(map[switchPort]int, len(table.elems))
 | 
						t.queues.closer = t.queues.closer[:0]
 | 
				
			||||||
	for _, info := range table.elems {
 | 
						for _, info := range table.elems {
 | 
				
			||||||
		dist := info.locator.dist(dest)
 | 
							dist := info.locator.dist(dest)
 | 
				
			||||||
		if dist < myDist {
 | 
							if dist < myDist {
 | 
				
			||||||
			closer[info.port] = dist
 | 
								t.queues.closer = append(t.queues.closer, closerInfo{info.port, dist})
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return closer
 | 
						return t.queues.closer
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Returns true if the peer is closer to the destination than ourself
 | 
					// Returns true if the peer is closer to the destination than ourself
 | 
				
			||||||
| 
						 | 
					@ -656,9 +661,9 @@ func (t *switchTable) handleIn(packet []byte, idle map[switchPort]time.Time) boo
 | 
				
			||||||
	var bestDist int
 | 
						var bestDist int
 | 
				
			||||||
	var bestTime time.Time
 | 
						var bestTime time.Time
 | 
				
			||||||
	ports := t.core.peers.getPorts()
 | 
						ports := t.core.peers.getPorts()
 | 
				
			||||||
	for port, dist := range closer {
 | 
						for _, cinfo := range closer {
 | 
				
			||||||
		to := ports[port]
 | 
							to := ports[cinfo.port]
 | 
				
			||||||
		thisTime, isIdle := idle[port]
 | 
							thisTime, isIdle := idle[cinfo.port]
 | 
				
			||||||
		var update bool
 | 
							var update bool
 | 
				
			||||||
		switch {
 | 
							switch {
 | 
				
			||||||
		case to == nil:
 | 
							case to == nil:
 | 
				
			||||||
| 
						 | 
					@ -667,9 +672,9 @@ func (t *switchTable) handleIn(packet []byte, idle map[switchPort]time.Time) boo
 | 
				
			||||||
			//nothing
 | 
								//nothing
 | 
				
			||||||
		case best == nil:
 | 
							case best == nil:
 | 
				
			||||||
			update = true
 | 
								update = true
 | 
				
			||||||
		case dist < bestDist:
 | 
							case cinfo.dist < bestDist:
 | 
				
			||||||
			update = true
 | 
								update = true
 | 
				
			||||||
		case dist > bestDist:
 | 
							case cinfo.dist > bestDist:
 | 
				
			||||||
			//nothing
 | 
								//nothing
 | 
				
			||||||
		case thisTime.Before(bestTime):
 | 
							case thisTime.Before(bestTime):
 | 
				
			||||||
			update = true
 | 
								update = true
 | 
				
			||||||
| 
						 | 
					@ -678,7 +683,7 @@ func (t *switchTable) handleIn(packet []byte, idle map[switchPort]time.Time) boo
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if update {
 | 
							if update {
 | 
				
			||||||
			best = to
 | 
								best = to
 | 
				
			||||||
			bestDist = dist
 | 
								bestDist = cinfo.dist
 | 
				
			||||||
			bestTime = thisTime
 | 
								bestTime = thisTime
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -711,6 +716,7 @@ type switch_buffers struct {
 | 
				
			||||||
	size        uint64                   // Total size of all buffers, in bytes
 | 
						size        uint64                   // Total size of all buffers, in bytes
 | 
				
			||||||
	maxbufs     int
 | 
						maxbufs     int
 | 
				
			||||||
	maxsize     uint64
 | 
						maxsize     uint64
 | 
				
			||||||
 | 
						closer      []closerInfo // Scratch space
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (b *switch_buffers) cleanup(t *switchTable) {
 | 
					func (b *switch_buffers) cleanup(t *switchTable) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue