Skip to content

Commit 3b7385e

Browse files
try faster appends
1 parent 6a16a1f commit 3b7385e

1 file changed

Lines changed: 72 additions & 56 deletions

File tree

datadog/serializer.go

Lines changed: 72 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -213,80 +213,96 @@ func isTrim(b byte) bool { return b == '.' || b == '_' || b == '-' }
213213
// appendSanitizedMetricName converts *any* string into something that StatsD / Graphite
214214
// accepts without complaints.
215215
func appendSanitizedMetricName(dst []byte, raw string) []byte {
216-
nameLen := 0
217-
orig := len(dst)
218216
if raw == "" {
219217
if len(dst) == 0 {
220218
return append(dst, "_unnamed_"...)
221219
}
222220
return dst
223221
}
224-
// ── 1. accent folding (creates one temporary ↴)
225-
// tmp := stripUnicodeAccents([]byte(raw))
226-
227-
// ── 2. run the same ASCII sanitizer, but write into dst
228-
lastWasRepl := false
229-
for i := 0; i < len(raw); i++ {
230-
c := byte(raw[i])
231-
232-
if c < 128 && valid[c] {
233-
// ASCII valid chars
234-
dst = append(dst, c)
235-
nameLen++
236-
lastWasRepl = false
237-
} else if c >= 0xC2 && c <= 0xC3 && i+1 < len(raw) {
238-
// Check for 2-byte UTF-8 sequences that are common accented letters
239-
c2 := byte(raw[i+1])
240-
if c2 >= 0x80 && c2 <= 0xBF { // Valid second byte
241-
// Decode the 2-byte sequence
242-
codepoint := uint16(c&0x1F)<<6 | uint16(c2&0x3F)
243-
244-
// Map common accented characters (U+00C0-U+00FF range)
245-
if codepoint >= 0xC0 && codepoint <= 0xFF {
246-
mapped := accentMap[codepoint]
247-
if valid[mapped] {
222+
orig := len(dst)
223+
224+
// Pre-grow
225+
need := len(raw)
226+
if need > maxLen {
227+
need = maxLen
228+
}
229+
if cap(dst)-len(dst) < need {
230+
nd := make([]byte, len(dst), len(dst)+need)
231+
copy(nd, dst)
232+
dst = nd
233+
}
234+
235+
n := len(raw)
236+
i := 0
237+
lastWasReplacement := false
238+
239+
// Skip leading trim while building
240+
for i < n {
241+
c := raw[i]
242+
if !(c == '.' || c == '_' || c == '-') {
243+
break
244+
}
245+
i++
246+
}
247+
248+
for i < n && (len(dst)-orig) < maxLen {
249+
// Batch ASCII-valid run
250+
remaining := maxLen - (len(dst) - orig)
251+
j := i
252+
limit := i + remaining
253+
if limit > n {
254+
limit = n
255+
}
256+
for j < limit {
257+
c := raw[j]
258+
if c >= 128 || !valid[c] {
259+
break
260+
}
261+
j++
262+
}
263+
if j > i {
264+
dst = append(dst, raw[i:j]...)
265+
lastWasReplacement = false
266+
i = j
267+
continue
268+
}
269+
270+
// 2-byte common accent folding
271+
c0 := raw[i]
272+
if c0 >= 0xC2 && c0 <= 0xC3 && i+1 < n {
273+
c1 := raw[i+1]
274+
if c1 >= 0x80 && c1 <= 0xBF {
275+
code := uint16(c0&0x1F)<<6 | uint16(c1&0x3F)
276+
if code >= 0xC0 && code <= 0xFF {
277+
mapped := accentMap[code]
278+
if valid[mapped] && (len(dst)-orig) < maxLen {
248279
dst = append(dst, mapped)
249-
nameLen++
250-
lastWasRepl = false
251-
i++ // Skip the second byte
280+
lastWasReplacement = false
281+
i += 2
252282
continue
253283
}
254284
}
255285
}
256-
// If we get here, treat as invalid
257-
if !lastWasRepl {
258-
dst = append(dst, replacement)
259-
nameLen++
260-
lastWasRepl = true
261-
}
262-
} else if !lastWasRepl {
263-
// Everything else (3-byte, 4-byte sequences, invalid chars)
264-
dst = append(dst, replacement)
265-
nameLen++
266-
lastWasRepl = true
267286
}
268287

269-
if nameLen >= maxLen {
270-
break
288+
// Replacement for everything else
289+
if !lastWasReplacement && len(dst) > orig && (len(dst)-orig) < maxLen {
290+
dst = append(dst, replacement)
291+
lastWasReplacement = true
271292
}
293+
i++
272294
}
273295

274-
// 3. trim leading / trailing '.', '_' or '-'
275-
start, end := orig, len(dst)
276-
for start < end && isTrim(dst[start]) {
277-
start++
278-
}
279-
for end > start && isTrim(dst[end-1]) {
280-
end--
281-
}
282-
283-
// 4. compact if we trimmed something
284-
if start > orig || end < len(dst) {
285-
copy(dst[orig:], dst[start:end])
286-
dst = dst[:orig+(end-start)]
296+
// Trim trailing '.' '_' '-'
297+
for l := len(dst); l > orig; {
298+
c := dst[l-1]
299+
if c != '.' && c != '_' && c != '-' {
300+
break
301+
}
302+
l--
303+
dst = dst[:l]
287304
}
288305

289-
// 5. fallback if everything vanished
290306
if len(dst) == orig {
291307
return append(dst, "_truncated_"...)
292308
}

0 commit comments

Comments
 (0)