Skip to content

Conversation

lukema95
Copy link
Contributor

Issue Description

The updateFilterMapsHeads() function at eth/backend.go:511 uses time.After() inside a for-select loop. This creates a new timer object on every loop iteration. While Go 1.23+ can garbage collect these timers, this pattern is inefficient and goes against Go best practices for timer usage in loops.

Affected Code:

// File: eth/backend.go
// Line: 505-517

for {
    select {
    case ev := <-headEventCh:
        setHead(ev.Header)
    case blockProc := <-blockProcCh:
        s.filterMaps.SetBlockProcessing(blockProc)
    case <-time.After(time.Second * 10):  // ⚠️ INEFFICIENT: Creates new timer on each iteration
        setHead(s.blockchain.CurrentBlock())
    case ch := <-s.closeFilterMaps:
        close(ch)
        return
    }
}

Optimized code:

func (s *Ethereum) updateFilterMapsHeads() {
	// ... same setup ...

	// ✅ Refactor: Use timer with reset 
	timer := time.NewTimer(time.Second * 10)
	defer timer.Stop()

	for {
		select {
		case ev := <-headEventCh:
			setHead(ev.Header)
			timer.Reset(time.Second * 10)  // ✅ guarantees no stale values
			
		case blockProc := <-blockProcCh:
			s.filterMaps.SetBlockProcessing(blockProc)
			timer.Reset(time.Second * 10)  // ✅ guarantees no stale values
			
		case <-timer.C:
			setHead(s.blockchain.CurrentBlock())
			timer.Reset(time.Second * 10) // ✅ guarantees no stale values
			
		case ch := <-s.closeFilterMaps:
			close(ch)
			return
		}
	}
}

@jwasinger
Copy link
Contributor

The current code is cleaner and the time interval is long enough that this can't effectively make a difference.

@jwasinger jwasinger closed this Oct 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants