~eliasnaur/gio-patches

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
2 2

[PATCH gio] gpu: optimize resourceCache

Details
Message ID
<165677908279.8884.12396645700854755605-0@git.sr.ht>
DKIM signature
missing
Download raw message
Patch: +49 -19
From: Egon Elbre <egonelbre@gmail.com>

By keeping all the information in a single map, we avoid multiple
lookups and can switch between frames more easily.

Before ~35us, after ~20us for adding 50 new+old and switching
the frame.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
---
 gpu/caches.go      | 44 +++++++++++++++++++++++++-------------------
 gpu/caches_test.go | 24 ++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 19 deletions(-)
 create mode 100644 gpu/caches_test.go

diff --git a/gpu/caches.go b/gpu/caches.go
index 762f3e77..b44f7ead 100644
--- a/gpu/caches.go
+++ b/gpu/caches.go
@@ -9,8 +9,12 @@ import (
)

type resourceCache struct {
	res    map[interface{}]resource
	newRes map[interface{}]resource
	res map[interface{}]resourceCacheValue
}

type resourceCacheValue struct {
	used     bool
	resource resource
}

// opCache is like a resourceCache but using concrete types and a
@@ -35,46 +39,48 @@ type opCacheValue struct {

func newResourceCache() *resourceCache {
	return &resourceCache{
		res:    make(map[interface{}]resource),
		newRes: make(map[interface{}]resource),
		res: make(map[interface{}]resourceCacheValue),
	}
}

func (r *resourceCache) get(key interface{}) (resource, bool) {
	v, exists := r.res[key]
	if exists {
		r.newRes[key] = v
	if !exists {
		return nil, false
	}
	return v, exists
	if !v.used {
		v.used = true
		r.res[key] = v
	}
	return v.resource, exists
}

func (r *resourceCache) put(key interface{}, val resource) {
	if _, exists := r.newRes[key]; exists {
	v, exists := r.res[key]
	if exists && v.used {
		panic(fmt.Errorf("key exists, %p", key))
	}
	r.res[key] = val
	r.newRes[key] = val
	v.used = true
	v.resource = val
	r.res[key] = v
}

func (r *resourceCache) frame() {
	for k, v := range r.res {
		if _, exists := r.newRes[k]; !exists {
		if v.used {
			v.used = false
			r.res[k] = v
		} else {
			delete(r.res, k)
			v.release()
			v.resource.release()
		}
	}
	for k, v := range r.newRes {
		delete(r.newRes, k)
		r.res[k] = v
	}
}

func (r *resourceCache) release() {
	r.frame()
	for _, v := range r.res {
		v.release()
		v.resource.release()
	}
	r.newRes = nil
	r.res = nil
}

diff --git a/gpu/caches_test.go b/gpu/caches_test.go
new file mode 100644
index 00000000..411de5af
--- /dev/null
+++ b/gpu/caches_test.go
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Unlicense OR MIT

package gpu

import "testing"

func BenchmarkResourceCache(b *testing.B) {
	offset := 0
	const N = 100

	cache := newResourceCache()
	for i := 0; i < b.N; i++ {
		// half are the same and half updated
		for k := 0; k < N; k++ {
			cache.put(offset+k, nullResource{})
		}
		cache.frame()
		offset += N / 2
	}
}

type nullResource struct{}

func (nullResource) release() {}
-- 
2.34.2

[gio/patches] build success

builds.sr.ht <builds@sr.ht>
Details
Message ID
<CL5BMFAVGMCW.1652L6N7POFII@cirno2>
In-Reply-To
<165677908279.8884.12396645700854755605-0@git.sr.ht> (view parent)
DKIM signature
missing
Download raw message
gio/patches: SUCCESS in 21m17s

[gpu: optimize resourceCache][0] from [~egonelbre][1]

[0]: https://lists.sr.ht/~eliasnaur/gio-patches/patches/33503
[1]: egonelbre@gmail.com

✓ #792903 SUCCESS gio/patches/linux.yml   https://builds.sr.ht/~eliasnaur/job/792903
✓ #792902 SUCCESS gio/patches/freebsd.yml https://builds.sr.ht/~eliasnaur/job/792902
✓ #792904 SUCCESS gio/patches/openbsd.yml https://builds.sr.ht/~eliasnaur/job/792904
✓ #792901 SUCCESS gio/patches/apple.yml   https://builds.sr.ht/~eliasnaur/job/792901
Details
Message ID
<CL5DM2ITIH5S.1N19KCOH9S60Z@macbog.local>
In-Reply-To
<165677908279.8884.12396645700854755605-0@git.sr.ht> (view parent)
DKIM signature
pass
Download raw message
Thanks, merged.

Elias
Reply to thread Export thread (mbox)