internal/telemetry: give Event a custom implementation of TagMap

This cuts the allocation cost of preparing an event for export from 4 down to 1.

name                old time/op    new time/op    delta
/LogIgnore-8          5.97µs ± 6%    4.18µs ± 4%  -29.87%  (p=0.000 n=20+19)
/TraceIgnore-8        13.6µs ± 4%    10.8µs ± 6%  -20.92%  (p=0.000 n=18+19)
/StatsIgnore-8        11.3µs ± 6%     7.9µs ± 6%  -30.29%  (p=0.000 n=20+20)
/Log-8                47.8µs ± 6%    43.9µs ± 6%   -8.15%  (p=0.000 n=20+19)
/Trace-8              60.5µs ±12%    54.7µs ± 4%   -9.58%  (p=0.000 n=20+19)
/Stats-8              13.8µs ± 3%    10.6µs ± 5%  -23.57%  (p=0.000 n=20+19)

name                old alloc/op   new alloc/op   delta
/LogIgnore-8          5.12kB ± 0%    3.58kB ± 0%  -30.00%  (p=0.000 n=20+20)
/TraceIgnore-8        14.6kB ± 0%    11.5kB ± 0%  -21.05%  (p=0.000 n=20+20)
/StatsIgnore-8        10.2kB ± 0%     7.2kB ± 0%  -30.00%  (p=0.000 n=20+20)
/Log-8                24.0kB ± 0%    20.9kB ± 0%  -12.81%  (p=0.000 n=18+20)
/Trace-8              31.0kB ± 0%    27.9kB ± 0%   -9.92%  (p=0.000 n=20+20)
/Stats-8              10.2kB ± 0%     7.2kB ± 0%  -30.00%  (p=0.000 n=20+20)

name                old allocs/op  new allocs/op  delta
/LogIgnore-8            64.0 ± 0%      16.0 ± 0%  -75.00%  (p=0.000 n=20+20)
/TraceIgnore-8           160 ± 0%        64 ± 0%  -60.00%  (p=0.000 n=20+20)
/StatsIgnore-8           128 ± 0%        32 ± 0%  -75.00%  (p=0.000 n=20+20)
/Log-8                   382 ± 0%       286 ± 0%  -25.13%  (p=0.000 n=20+20)
/Trace-8                 480 ± 0%       384 ± 0%  -20.00%  (p=0.000 n=20+20)
/Stats-8                 128 ± 0%        32 ± 0%  -75.00%  (p=0.000 n=20+20)

Change-Id: I76ff4ac9658b766daf3929e7cfc0d2cfcab036ed
Reviewed-on: https://go-review.googlesource.com/c/tools/+/226359
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
diff --git a/internal/telemetry/event/event.go b/internal/telemetry/event/event.go
index 23329cb..c28e9d9 100644
--- a/internal/telemetry/event/event.go
+++ b/internal/telemetry/event/event.go
@@ -41,6 +41,11 @@
 	dynamic []Tag // dynamically sized storage for remaining tags
 }
 
+// eventTagMap implements TagMap for a the tags of an Event.
+type eventTagMap struct {
+	event Event
+}
+
 func (ev Event) IsLog() bool       { return ev.typ == LogType }
 func (ev Event) IsEndSpan() bool   { return ev.typ == EndSpanType }
 func (ev Event) IsStartSpan() bool { return ev.typ == StartSpanType }
@@ -76,9 +81,21 @@
 }
 
 func (ev Event) Map() TagMap {
-	return MergeTagMaps(
-		NewTagMap(ev.static[:]...),
-		NewTagMap(ev.dynamic...))
+	return &eventTagMap{event: ev}
+}
+
+func (m *eventTagMap) Find(key interface{}) Tag {
+	for _, tag := range m.event.static {
+		if tag.Key == key {
+			return tag
+		}
+	}
+	for _, tag := range m.event.dynamic {
+		if tag.Key == key {
+			return tag
+		}
+	}
+	return Tag{}
 }
 
 func makeEvent(typ eventType, static sTags, tags []Tag) Event {