Skip to content

perf: mux - consolidate wire, sc, mu slices into muxwire struct #869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2025

Conversation

arbhalerao
Copy link
Contributor

fixes: #862

@arbhalerao
Copy link
Contributor Author

Hi @rueian, any update on this PR? Thanks!

@rueian
Copy link
Collaborator

rueian commented Jul 5, 2025

Hi @arbhalerao, the change looks good to me. However, I haven't had a chance to make sure there is no performane degration.

@rueian
Copy link
Collaborator

rueian commented Jul 5, 2025

Or could you help doing a simulated benchmark on this critical path:

rueidis/mux.go

Lines 155 to 158 in dad42a2

func (m *mux) _pipe(ctx context.Context, i uint16) (w wire, err error) {
if w = m.wire[i].Load().(wire); w != m.init {
return w, nil
}

to show that it won't degrade with your change?

@arbhalerao
Copy link
Contributor Author

Hi @rueian,
Absolutely, I'll put together a benchmark for that path and follow up soon with the results.

@rueian
Copy link
Collaborator

rueian commented Jul 7, 2025

Thanks @arbhalerao. A simulated benchmark on similar structures is enough. I don't need a real benchmark on the real mux structure.

@arbhalerao
Copy link
Contributor Author

Hi @rueian,

I've created a benchmark testing the critical path performance as requested. The benchmark compares:

  • Current: m.wire[i].Load()
  • Proposed: m.muxwires[i].wire.Load()

Results:

BenchmarkCurrentWireLoad-22         	861622592	         1.350 ns/op	       0 B/op	       0 allocs/op
BenchmarkProposedWireLoad-22        	879522333	         1.385 ns/op	       0 B/op	       0 allocs/op

BenchmarkCurrentCriticalPath-22     	442859290	         2.755 ns/op	       0 B/op	       0 allocs/op
BenchmarkProposedCriticalPath-22    	439302198	         2.728 ns/op	       0 B/op	       0 allocs/op
package main

import (
	"sync"
	"sync/atomic"
	"testing"
)

type wire interface {
	ID() int
}

type mockWire struct {
	id int
}

func (m *mockWire) ID() int {
	return m.id
}

type singleconnect struct {
	w wire
	e error
	g sync.WaitGroup
}

var initWire = &mockWire{id: -1}

type currentMux struct {
	wire []atomic.Value
	sc   []*singleconnect
	mu   []sync.Mutex
}

type muxwire struct {
	wire atomic.Value
	sc   *singleconnect
	mu   sync.Mutex
}

type proposedMux struct {
	muxwires []muxwire
}

func newCurrentMux(size int) *currentMux {
	m := &currentMux{
		wire: make([]atomic.Value, size),
		mu:   make([]sync.Mutex, size),
		sc:   make([]*singleconnect, size),
	}
	for i := 0; i < size; i++ {
		m.wire[i].Store(initWire)
	}
	return m
}

func newProposedMux(size int) *proposedMux {
	m := &proposedMux{
		muxwires: make([]muxwire, size),
	}
	for i := 0; i < size; i++ {
		m.muxwires[i].wire.Store(initWire)
	}
	return m
}

func (m *currentMux) wireLoad(i uint16) wire {
	return m.wire[i].Load().(wire)
}

func (m *proposedMux) wireLoad(i uint16) wire {
	return m.muxwires[i].wire.Load().(wire)
}

func BenchmarkCurrentWireLoad(b *testing.B) {
	m := newCurrentMux(16)
	for i := 0; i < b.N; i++ {
		m.wireLoad(uint16(i % 16))
	}
}

func BenchmarkProposedWireLoad(b *testing.B) {
	m := newProposedMux(16)
	for i := 0; i < b.N; i++ {
		m.wireLoad(uint16(i % 16))
	}
}

func BenchmarkCurrentCriticalPath(b *testing.B) {
	m := newCurrentMux(16)
	for i := 0; i < b.N; i++ {
		slot := uint16(i % 16)
		if w := m.wireLoad(slot); w != initWire {
			continue
		}
	}
}

func BenchmarkProposedCriticalPath(b *testing.B) {
	m := newProposedMux(16)
	for i := 0; i < b.N; i++ {
		slot := uint16(i % 16)
		if w := m.wireLoad(slot); w != initWire {
			continue
		}
	}
}

Copy link
Collaborator

@rueian rueian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @arbhalerao

@rueian rueian merged commit 37dfd47 into redis:main Jul 7, 2025
34 checks passed
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.

perf: reduce mux sturct size by merging slices
2 participants