diff --git a/message.go b/message.go index 19400ab..fb6fb18 100644 --- a/message.go +++ b/message.go @@ -11,19 +11,23 @@ type Tag struct { Value string } -// Tags is an key-order-preserving, last-insert-wins slice +// Tags is an key-order-preserving, last-insert-wins "set" of Tag type Tags struct { + kv map[string]int // quick lookups for inserts tags []Tag } func (t *Tags) Insert(new Tag) { - for i := range t.tags { - if t.tags[i].Key == new.Key { - t.tags[i] = new - return - } + if t.kv == nil { + t.kv = map[string]int{} + } + i, ok := t.kv[new.Key] + if ok { + t.tags[i] = new + } else { + t.kv[new.Key] = len(t.tags) + t.tags = append(t.tags, new) } - t.tags = append(t.tags, new) } func (t *Tags) GetSlice() []Tag {