mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-14 10:10:42 -07:00
Using a goto based loop makes it become a leaf function which can be inlined, making us get a slight performance increase in the fast path. See: https://github.com/golang/go/issues/14768
71 lines
1.2 KiB
Go
71 lines
1.2 KiB
Go
// Copyright 2012-2017 Apcera Inc. All rights reserved.
|
|
|
|
package server
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/nats-io/nuid"
|
|
)
|
|
|
|
// Use nuid.
|
|
func genID() string {
|
|
return nuid.Next()
|
|
}
|
|
|
|
// Ascii numbers 0-9
|
|
const (
|
|
asciiZero = 48
|
|
asciiNine = 57
|
|
)
|
|
|
|
// parseSize expects decimal positive numbers. We
|
|
// return -1 to signal error.
|
|
func parseSize(d []byte) (n int) {
|
|
l := len(d)
|
|
if l == 0 {
|
|
return -1
|
|
}
|
|
var (
|
|
i int
|
|
dec byte
|
|
)
|
|
|
|
// Note: Use `goto` here to avoid for loop in order
|
|
// to have the function be inlined.
|
|
// See: https://github.com/golang/go/issues/14768
|
|
loop:
|
|
dec = d[i]
|
|
if dec < asciiZero || dec > asciiNine {
|
|
return -1
|
|
}
|
|
n = n*10 + (int(dec) - asciiZero)
|
|
|
|
i++
|
|
if i < l {
|
|
goto loop
|
|
}
|
|
return n
|
|
}
|
|
|
|
// parseInt64 expects decimal positive numbers. We
|
|
// return -1 to signal error
|
|
func parseInt64(d []byte) (n int64) {
|
|
if len(d) == 0 {
|
|
return -1
|
|
}
|
|
for _, dec := range d {
|
|
if dec < asciiZero || dec > asciiNine {
|
|
return -1
|
|
}
|
|
n = n*10 + (int64(dec) - asciiZero)
|
|
}
|
|
return n
|
|
}
|
|
|
|
// Helper to move from float seconds to time.Duration
|
|
func secondsToDuration(seconds float64) time.Duration {
|
|
ttl := seconds * float64(time.Second)
|
|
return time.Duration(ttl)
|
|
}
|