Skip to content

Commit

Permalink
perf: use strings.Cut, also strings.IndexByte more
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Oct 13, 2024
1 parent 780d68d commit 980a251
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
23 changes: 10 additions & 13 deletions cht/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ type logInfo struct {

// cut field between start and end, trimming space.
//
// E.g. cut("[ 12345 ]", "[", "]") == "12345".
func cut(s, start, end string) string {
if s == "" || start == "" || end == "" {
return ""
}
left := strings.Index(s, start)
// E.g. cut("[ 12345 ]", '[', ']') == "12345".
func cut(s string, start, end byte) string {
left := strings.IndexByte(s, start)
if left < 0 {
return ""
}
s = s[left+1:]
right := strings.Index(s, end)
right := strings.IndexByte(s, end)
if right < 0 {
return ""
}
Expand Down Expand Up @@ -61,18 +58,18 @@ func (e LogEntry) Level() zapcore.Level {

func parseLog(s string) LogEntry {
s = strings.TrimSpace(s)
tid, _ := strconv.ParseUint(cut(s, "[", "]"), 10, 64)
tid, _ := strconv.ParseUint(cut(s, '[', ']'), 10, 64)
var textStart int
if idx := strings.Index(s, "}"); idx > 0 {
textStart = strings.Index(s[idx:], ":") + idx + 1
if idx := strings.IndexByte(s, '}'); idx > 0 {
textStart = strings.IndexByte(s[idx:], ':') + idx + 1
}
if textStart-1 > len(s) {
textStart = 0
}
return LogEntry{
QueryID: cut(s, "{", "}"),
Severity: cut(s, "<", ">"),
Name: cut(s, ">", ":"),
QueryID: cut(s, '{', '}'),
Severity: cut(s, '<', '>'),
Name: cut(s, '>', ':'),
Message: strings.TrimSpace(s[textStart:]),
ThreadID: tid,
}
Expand Down
13 changes: 6 additions & 7 deletions proto/col_datetime64.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ func (c *ColDateTime64) Infer(t ColumnType) error {
if elem == "" {
return errors.Errorf("invalid DateTime64: no elements in %q", t)
}
elems := strings.SplitN(elem, ",", 2)
for i := range elems {
elems[i] = strings.Trim(elems[i], `' `)
}
n, err := strconv.ParseUint(elems[0], 10, 8)
p_str, loc_str, hasloc := strings.Cut(elem, ",")
p_str = strings.Trim(p_str, `' `)
loc_str = strings.Trim(loc_str, `' `)
n, err := strconv.ParseUint(p_str, 10, 8)
if err != nil {
return errors.Wrap(err, "parse precision")
}
Expand All @@ -75,8 +74,8 @@ func (c *ColDateTime64) Infer(t ColumnType) error {
}
c.Precision = p
c.PrecisionSet = true
if len(elems) > 1 {
loc, err := time.LoadLocation(elems[1])
if hasloc {
loc, err := time.LoadLocation(loc_str)
if err != nil {
return errors.Wrap(err, "invalid location")
}
Expand Down
10 changes: 4 additions & 6 deletions proto/col_enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ func (e *ColEnum) parse(t ColumnType) error {
for _, elem := range strings.Split(elements, ",") {
def := strings.TrimSpace(elem)
// 'hello' = 1
parts := strings.SplitN(def, "=", 2)
if len(parts) != 2 {
left, right, hascomma := strings.Cut(def, "=")
if !hascomma {
return errors.Errorf("bad enum definition %q", def)
}
var (
left = strings.TrimSpace(parts[0]) // 'hello'
right = strings.TrimSpace(parts[1]) // 1
)
left = strings.TrimSpace(left) // 'hello'
right = strings.TrimSpace(right) // 1
idx, err := strconv.Atoi(right)
if err != nil {
return errors.Errorf("bad right side of definition %q", right)
Expand Down
8 changes: 4 additions & 4 deletions proto/col_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,18 @@ func (c ColMap[K, V]) Prepare() error {

// Infer ensures Inferable column propagation.
func (c *ColMap[K, V]) Infer(t ColumnType) error {
elems := strings.Split(string(t.Elem()), ",")
if len(elems) != 2 {
keytype, valtype, hascomma := strings.Cut(string(t.Elem()), ",")
if !hascomma || strings.ContainsRune(valtype, ',') {
return errors.New("invalid map type")
}
if v, ok := c.Keys.(Inferable); ok {
ct := ColumnType(strings.TrimSpace(elems[0]))
ct := ColumnType(strings.TrimSpace(keytype))
if err := v.Infer(ct); err != nil {
return errors.Wrap(err, "infer data")
}
}
if v, ok := c.Values.(Inferable); ok {
ct := ColumnType(strings.TrimSpace(elems[1]))
ct := ColumnType(strings.TrimSpace(valtype))
if err := v.Infer(ct); err != nil {
return errors.Wrap(err, "infer data")
}
Expand Down
8 changes: 4 additions & 4 deletions proto/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (c ColumnType) Base() ColumnType {
}
var (
v = string(c)
start = strings.Index(v, "(")
end = strings.LastIndex(v, ")")
start = strings.IndexByte(v, '(')
end = strings.LastIndexByte(v, ')')
)
if start <= 0 || end <= 0 || end < start {
return c
Expand Down Expand Up @@ -150,8 +150,8 @@ func (c ColumnType) Elem() ColumnType {
}
var (
v = string(c)
start = strings.Index(v, "(")
end = strings.LastIndex(v, ")")
start = strings.IndexByte(v, '(')
end = strings.LastIndexByte(v, ')')
)
if start <= 0 || end <= 0 || end < start {
// No element.
Expand Down

0 comments on commit 980a251

Please sign in to comment.