Skip to content
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

add ReadOnly mode and change IsoCopy behavior #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yihuang
Copy link

@yihuang yihuang commented Feb 15, 2024

Context

I'm experimenting with the optimistic concurrency control pattern using the IsoCopy feature, it should be useful in read-heavy scenarios, this patch is some necessary changes to enable this pattern.

type BTree[T any] struct {
	atomic.Pointer[btree.BTreeG[T]]
}

func NewBTree[T any](less func(a, b T) bool) *BTree[T] {
	tree := btree.NewBTreeGOptions[T](less, btree.Options{
		NoLocks:  true,
		ReadOnly: true,
	})
	t := &BTree[T]{}
	t.Store(tree)
	return t
}

func (bt *BTree[T]) Get(item T) (result T, ok bool) {
	return bt.Load().Get(item)
}

func (bt *BTree[T]) Set(item T) (prev T, ok bool) {
	for {
		t := bt.Load()
		c := t.Copy()
		prev, ok = c.Set(item)
		c.Freeze()
		if bt.CompareAndSwap(t, c) {
			return
		}
	}
}

The performance difference of different concurrency control strategies differs a lot based on the workload, for my local testing scenario, the CAS is faster than Mutex in some cases, but slower in some other extreme cases, but both are much faster than RWMutex.

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.

1 participant