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

proc_maps: Parse address and device without allocating #572

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 10 additions & 10 deletions proc_maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ type ProcMap struct {
// parseDevice parses the device token of a line and converts it to a dev_t
// (mkdev) like structure.
func parseDevice(s string) (uint64, error) {
toks := strings.Split(s, ":")
if len(toks) < 2 {
return 0, fmt.Errorf("%w: unexpected number of fields, expected: 2, got: %q", ErrFileParse, len(toks))
i := strings.Index(s, ":")
if i == -1 {
return 0, fmt.Errorf("%w: expected separator `:` in %s", ErrFileParse, s)
}

major, err := strconv.ParseUint(toks[0], 16, 0)
major, err := strconv.ParseUint(s[0:i], 16, 0)
if err != nil {
return 0, err
}

minor, err := strconv.ParseUint(toks[1], 16, 0)
minor, err := strconv.ParseUint(s[i+1:], 16, 0)
if err != nil {
return 0, err
}
Expand All @@ -93,17 +93,17 @@ func parseAddress(s string) (uintptr, error) {

// parseAddresses parses the start-end address.
func parseAddresses(s string) (uintptr, uintptr, error) {
toks := strings.Split(s, "-")
if len(toks) < 2 {
return 0, 0, fmt.Errorf("%w: invalid address", ErrFileParse)
idx := strings.Index(s, "-")
if idx == -1 {
return 0, 0, fmt.Errorf("%w: expected separator `-` in %s", ErrFileParse, s)
}

saddr, err := parseAddress(toks[0])
saddr, err := parseAddress(s[0:idx])
if err != nil {
return 0, 0, err
}

eaddr, err := parseAddress(toks[1])
eaddr, err := parseAddress(s[idx+1:])
if err != nil {
return 0, 0, err
}
Expand Down
37 changes: 37 additions & 0 deletions proc_maps64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,40 @@ func TestProcMaps(t *testing.T) {
}

}

var start, end uintptr

func BenchmarkParseAddress(b *testing.B) {
b.ReportAllocs()
var (
s, e uintptr
err error
)
for i := 0; i < b.N; i++ {
s, e, err = parseAddresses("7f7d7469e000-7f7d746a0000")
if err != nil {
b.Fatal(err)
}
}
// Prevent the compiler from optimizing away benchmark code.
start = s
end = e
}

var device uint64

func BenchmarkParseDevice(b *testing.B) {
b.ReportAllocs()
var (
d uint64
err error
)
for i := 0; i < b.N; i++ {
d, err = parseDevice("00:22")
if err != nil {
b.Fatal(err)
}
}
// Prevent the compiler from optimizing away benchmark code.
device = d
}