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

Fix btf.FindType to avoid copy #424

Merged
merged 4 commits into from
Sep 17, 2021

Conversation

pippolo84
Copy link
Member

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

pippolo84 and others added 2 commits September 17, 2021 00:46
Signed-off-by: Fabio Falzoi <fabio.falzoi84@gmail.com>
Signed-off-by: Fabio Falzoi <fabio.falzoi84@gmail.com>
@@ -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())
Copy link
Collaborator

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?

Suggested change
typPtr.Set(reflect.Indirect(reflect.ValueOf(candidate)).Addr())
typPtr.Set(reflect.ValueOf(candidate))

return fmt.Errorf("%T is not a pointer", typ)
}

typPtr := reflect.Indirect(typValue)
Copy link
Collaborator

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

Struct{},
&Struct{},
} {
if err := spec.FindType("iphdr", nil); err == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if err := spec.FindType("iphdr", nil); err == nil {
if err := spec.FindType("iphdr", typ); err == nil {

Copy link
Member Author

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{},
} {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add something like **int?

Copy link
Member Author

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.

@lmb
Copy link
Collaborator

lmb commented Sep 17, 2021

Looks good, thanks for the quick turn around!

Fabio Falzoi added 2 commits September 17, 2021 11:39
Signed-off-by: Fabio Falzoi <fabio.falzoi84@gmail.com>
Signed-off-by: Fabio Falzoi <fabio.falzoi84@gmail.com>
@pippolo84 pippolo84 requested a review from lmb September 17, 2021 09:47
Copy link
Collaborator

@lmb lmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@lmb lmb merged commit fc2955d into cilium:master Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

btf.Spec.FindType copies type
2 participants