From fef0dc08b13a1f5794ced4d69569abab2def6cf7 Mon Sep 17 00:00:00 2001 From: Jacalz Date: Fri, 28 Feb 2025 11:14:20 +0100 Subject: [PATCH 1/2] Use efficient map delete pattern intead of allocating new map --- internal/async/map_migratedfynedo.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/async/map_migratedfynedo.go b/internal/async/map_migratedfynedo.go index 07aa48bccd..6a2fe1bda8 100644 --- a/internal/async/map_migratedfynedo.go +++ b/internal/async/map_migratedfynedo.go @@ -69,5 +69,7 @@ func (m *Map[K, V]) Store(key K, value V) { // Clear removes all entries from the map. func (m *Map[K, V]) Clear() { - m.m = make(map[any]V) + for k := range m.m { + delete(m.m, k) + } } From 3f4b9bf6ea8ed689c39e9d1d9202de18bd2f0301 Mon Sep 17 00:00:00 2001 From: Jacalz Date: Fri, 28 Feb 2025 18:53:40 +0100 Subject: [PATCH 2/2] Add comment about efficient map clear --- internal/async/map_migratedfynedo.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/internal/async/map_migratedfynedo.go b/internal/async/map_migratedfynedo.go index 6a2fe1bda8..bd7fa39b9d 100644 --- a/internal/async/map_migratedfynedo.go +++ b/internal/async/map_migratedfynedo.go @@ -4,9 +4,8 @@ package async // Map is a generic wrapper around [sync.Map]. type Map[K any, V any] struct { - // once go1.20 is base, can use map[K]V - // with K being Comparable instead of any - // (CanvasObject, etc aren't Comparable for go1.19) + // Use "comparable" as type constraint and map[K]V as the inner type + // once Go 1.20 is our minimum version so interfaces can be used as keys. m map[any]V } @@ -69,7 +68,5 @@ func (m *Map[K, V]) Store(key K, value V) { // Clear removes all entries from the map. func (m *Map[K, V]) Clear() { - for k := range m.m { - delete(m.m, k) - } + m.m = make(map[any]V) // Use range-and-delete loop once Go 1.20 is the minimum version. }