summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-mutexes/pool.go
diff options
context:
space:
mode:
authorLibravatar kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com>2022-03-08 11:56:53 +0000
committerLibravatar GitHub <noreply@github.com>2022-03-08 12:56:53 +0100
commitb8879ac68a30e8bccd1c96cc4630da791d8996c4 (patch)
tree77adeeaf2456610b771d9df8dc38207014215aea /vendor/codeberg.org/gruf/go-mutexes/pool.go
parent[performance] Database optimizations (#419) (diff)
downloadgotosocial-b8879ac68a30e8bccd1c96cc4630da791d8996c4.tar.xz
[dependencies] update go-store, go-mutexes (#422)
* update go-store, go-mutexes Signed-off-by: kim <grufwub@gmail.com> * update vendored code Signed-off-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-mutexes/pool.go')
-rw-r--r--vendor/codeberg.org/gruf/go-mutexes/pool.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-mutexes/pool.go b/vendor/codeberg.org/gruf/go-mutexes/pool.go
new file mode 100644
index 000000000..135e2c117
--- /dev/null
+++ b/vendor/codeberg.org/gruf/go-mutexes/pool.go
@@ -0,0 +1,40 @@
+package mutexes
+
+// pool is a very simply memory pool.
+type pool struct {
+ current []interface{}
+ victim []interface{}
+ alloc func() interface{}
+}
+
+// Acquire will returns a sync.RWMutex from pool (or alloc new).
+func (p *pool) Acquire() interface{} {
+ // First try the current queue
+ if l := len(p.current) - 1; l >= 0 {
+ v := p.current[l]
+ p.current = p.current[:l]
+ return v
+ }
+
+ // Next try the victim queue.
+ if l := len(p.victim) - 1; l >= 0 {
+ v := p.victim[l]
+ p.victim = p.victim[:l]
+ return v
+ }
+
+ // Lastly, alloc new.
+ return p.alloc()
+}
+
+// Release places a sync.RWMutex back in the pool.
+func (p *pool) Release(v interface{}) {
+ p.current = append(p.current, v)
+}
+
+// GC will clear out unused entries from the pool.
+func (p *pool) GC() {
+ current := p.current
+ p.current = nil
+ p.victim = current
+}