-
Notifications
You must be signed in to change notification settings - Fork 685
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
Fix btf.FindType to avoid copy #424
Conversation
Signed-off-by: Fabio Falzoi <fabio.falzoi84@gmail.com>
Signed-off-by: Fabio Falzoi <fabio.falzoi84@gmail.com>
internal/btf/btf.go
Outdated
@@ -498,8 +510,8 @@ func (s *Spec) FindType(name string, typ Type) error { | |||
return fmt.Errorf("type %s: %w", name, ErrNotFound) | |||
} | |||
|
|||
value := reflect.Indirect(reflect.ValueOf(candidate)) | |||
reflect.Indirect(reflect.ValueOf(typ)).Set(value) | |||
typPtr.Set(reflect.Indirect(reflect.ValueOf(candidate)).Addr()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indirect() removes unwraps a Ptr, Addr returns a pointer to v. I think you can skip both?
typPtr.Set(reflect.Indirect(reflect.ValueOf(candidate)).Addr()) | |
typPtr.Set(reflect.ValueOf(candidate)) |
internal/btf/btf.go
Outdated
return fmt.Errorf("%T is not a pointer", typ) | ||
} | ||
|
||
typPtr := reflect.Indirect(typValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be typValue.Elem(). We know that typValue is a Ptr, so no need for Indirect
internal/btf/btf_test.go
Outdated
Struct{}, | ||
&Struct{}, | ||
} { | ||
if err := spec.FindType("iphdr", nil); err == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if err := spec.FindType("iphdr", nil); err == nil { | |
if err := spec.FindType("iphdr", typ); err == nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's what happen when coding at late night 😅
nil, | ||
Struct{}, | ||
&Struct{}, | ||
} { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add something like **int
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, and now that I think about it, we should also check something like slices and maps as "strange" types.
Looks good, thanks for the quick turn around! |
Signed-off-by: Fabio Falzoi <fabio.falzoi84@gmail.com>
Signed-off-by: Fabio Falzoi <fabio.falzoi84@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Fix
btf.FindType
to return a pointer to the matching BTF type.This will allow to get exactly the same type (same address) for multiple calls requesting the same type name.
Fixes #416