Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(logging): prevent unnecessary processing for debug logging #828

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion util/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ var (
maxValueStrLen = 150
)

// DebugLibraryEnabled returns the value of debugLibrary.
// This function is used to prevent unnecessary inline processing of the DbgPrint calls.
func DebugLibraryEnabled() bool {
return debugLibrary
}

// DebugSchemaEnabled returns the value of debugSchema.
// This function is used to prevent unnecessary inline processing of the DbgSchema calls.
func DebugSchemaEnabled() bool {
return debugSchema
}

// DbgPrint prints v if the package global variable debugLibrary is set.
// v has the same format as Printf. A trailing newline is added to the output.
func DbgPrint(v ...interface{}) {
Expand All @@ -63,7 +75,10 @@ func DbgSchema(v ...interface{}) {

// DbgErr DbgPrints err and returns it.
func DbgErr(err error) error {
DbgPrint("ERR: " + err.Error())
if debugLibrary {
DbgPrint("ERR: " + err.Error())
}

return err
}

Expand Down
42 changes: 23 additions & 19 deletions util/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,36 @@ import (

// SchemaPaths returns all the paths in the path tag.
func SchemaPaths(f reflect.StructField) ([][]string, error) {
var out [][]string
pathTag, ok := f.Tag.Lookup("path")
if !ok || pathTag == "" {
return nil, fmt.Errorf("field %s did not specify a path", f.Name)
}

// Early return for performance.
if !strings.Contains(pathTag, "|") {
return [][]string{stripModulePrefixes(strings.Split(pathTag, "/"))}, nil
}

ps := strings.Split(pathTag, "|")
for _, p := range ps {
out = append(out, stripModulePrefixes(strings.Split(p, "/")))
out := make([][]string, len(ps))
for i, p := range ps {
out[i] = stripModulePrefixes(strings.Split(p, "/"))
}
return out, nil
}

// ShadowSchemaPaths returns all the paths in the shadow-path tag. If the tag
// doesn't exist, a nil slice is returned.
func ShadowSchemaPaths(f reflect.StructField) [][]string {
var out [][]string
pathTag, ok := f.Tag.Lookup("shadow-path")
if !ok || pathTag == "" {
return nil
}

ps := strings.Split(pathTag, "|")
for _, p := range ps {
out = append(out, stripModulePrefixes(strings.Split(p, "/")))
out := make([][]string, len(ps))
for i, p := range ps {
out[i] = stripModulePrefixes(strings.Split(p, "/"))
}
return out
}
Expand Down Expand Up @@ -126,11 +131,12 @@ func relativeSchemaPath(f reflect.StructField, preferShadowPath bool) ([]string,
return nil, fmt.Errorf("field %s did not specify a shadow-path", f.Name)
}

paths := strings.Split(pathTag, "|")
if len(paths) == 1 {
if !strings.Contains(pathTag, "|") {
pathTag = strings.TrimPrefix(pathTag, "/")
return strings.Split(pathTag, "/"), nil
}

paths := strings.Split(pathTag, "|")
for _, pv := range paths {
pv = strings.TrimPrefix(pv, "/")
pe := strings.Split(pv, "/")
Expand Down Expand Up @@ -314,9 +320,9 @@ func StripModulePrefixesStr(in string) string {
// stripModulePrefixes returns "in" with each element with the format "A:B"
// changed to "B".
func stripModulePrefixes(in []string) []string {
var out []string
for _, v := range in {
out = append(out, StripModulePrefix(v))
out := make([]string, len(in))
for i, v := range in {
out[i] = StripModulePrefix(v)
}
return out
}
Expand All @@ -341,15 +347,13 @@ func stripModulePrefixWithCheck(name string) (string, error) {
// removing foo from "foo:bar". Such qualified paths are used in YANG modules
// where remote paths are referenced.
func StripModulePrefix(name string) string {
ps := strings.Split(name, ":")
switch len(ps) {
case 1:
return name
case 2:
return ps[1]
default:
return name
for i, r := range name {
if r == ':' {
return name[i+1:]
}
}

return name
}

// ReplacePathSuffix replaces the non-prefix part of a prefixed path name, or
Expand Down
4 changes: 4 additions & 0 deletions util/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,10 @@ func TestStripModulePrefix(t *testing.T) {
desc: "empty string",
inName: "",
wantName: "",
}, {
desc: "a single `:`",
inName: ":",
wantName: "",
}}

for _, tt := range tests {
Expand Down
Loading