summaryrefslogtreecommitdiff
path: root/internal/ap/util.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/ap/util.go')
-rw-r--r--internal/ap/util.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/internal/ap/util.go b/internal/ap/util.go
index c810b7985..967a1659d 100644
--- a/internal/ap/util.go
+++ b/internal/ap/util.go
@@ -19,10 +19,39 @@ package ap
import (
"net/url"
+ "sync"
"github.com/superseriousbusiness/activity/streams/vocab"
)
+const mapmax = 256
+
+// mapPool is a memory pool
+// of maps for JSON decoding.
+var mapPool sync.Pool
+
+// getMap acquires a map from memory pool.
+func getMap() map[string]any {
+ v := mapPool.Get()
+ if v == nil {
+ // preallocate map of max-size.
+ m := make(map[string]any, mapmax)
+ v = m
+ }
+ return v.(map[string]any) //nolint
+}
+
+// putMap clears and places map back in pool.
+func putMap(m map[string]any) {
+ if len(m) > mapmax {
+ // drop maps beyond
+ // our maximum size.
+ return
+ }
+ clear(m)
+ mapPool.Put(m)
+}
+
// _TypeOrIRI wraps a vocab.Type to implement TypeOrIRI.
type _TypeOrIRI struct {
vocab.Type