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

allow using binary.NativeEndian #1384

Closed
wants to merge 1 commit into from
Closed

allow using binary.NativeEndian #1384

wants to merge 1 commit into from

Conversation

lmb
Copy link
Collaborator

@lmb lmb commented Mar 20, 2024

use binary.NativeEndian

Remove the internal implementation of NativeEndian in favour of 
binary.NativeEndian. There is one hiccup: it's not possible to use a simple
comparison to figure out whether NativeEndian is the same as for example
LittleEndian. The IsNativeEndian helper takes care of this.

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>

@lmb lmb force-pushed the native-endian branch 4 times, most recently from 485a6c5 to f528c20 Compare March 21, 2024 09:24
@lmb lmb marked this pull request as ready for review March 21, 2024 09:43
@lmb lmb requested a review from a team as a code owner March 21, 2024 09:43
@lmb lmb marked this pull request as draft March 22, 2024 10:02
Remove the internal implementation of NativeEndian in favour of
binary.NativeEndian. There is one hiccup: it's not possible to
use a simple comparison to figure out whether NativeEndian is
the same as for example LittleEndian. The IsNativeEndian helper
takes care of this.

See golang/go#57237 (comment)

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
@lmb
Copy link
Collaborator Author

lmb commented Mar 22, 2024

Discussed with Timo: seems like a lot of pain for little gain. We'll discuss next week.

@@ -935,14 +935,19 @@ func (iter *InstructionIterator) Next() bool {
type bpfRegisters uint8

func newBPFRegisters(dst, src Register, bo binary.ByteOrder) (bpfRegisters, error) {
buf := make([]byte, 2)
val := uint16(dst&0x0F)<<8 | uint16(src&0x0F)
switch bo {
case binary.LittleEndian:
Copy link
Contributor

Choose a reason for hiding this comment

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

why not replacing this whole switch with bo.PutUint16 directly ?

Copy link
Collaborator

@ti-mo ti-mo Mar 22, 2024

Choose a reason for hiding this comment

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

That's the annoying bit: calling an interface method cannot be inlined, plus passing a pointer to an interface method causes it to escape. This function is called a ton, so it starts adding up.

We have some amount of discovery to do, because now binary.NativeEndian is a thing, we can probably eliminate these kind of switches/branches and replace them with binary.NativeEndian.PutUint16, since marshaling bpf insns is usually done before loading into the host kernel, and we'd almost always do that in native endianness.

Instruction.Marshal() is exported API, though, so we should have a look at public callers on Sourcegraph to see if anyone depends on marshaling asm.Instructions in arbitrary endiannesses (e.g. for converting ELFs from LE to BE).

Copy link
Contributor

Choose a reason for hiding this comment

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

ah yeah, that's too bad

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Instruction.Marshal() is exported API, though, so we should have a look at public callers on Sourcegraph to see if anyone depends on marshaling asm.Instructions in arbitrary endiannesses

I'd rather not get into the habit of hardcoding NativeEndian, it leads to not thinking about endianness at callsites and that tends to cause subtle bugs. Compared to that the type switch isn't bad at all imo.

@ti-mo
Copy link
Collaborator

ti-mo commented May 22, 2024

Coming back to this, I find our internal.NativeEndian implementation more elegant than what they did with binary.NativeEndian. Shall we keep things as-is for now?

@lmb
Copy link
Collaborator Author

lmb commented May 22, 2024

yeah makes sense. i think the real solution is to stop using binary.ByteOrder to represent endianness in Collection, etc. We should have made up our own constants.

@lmb lmb closed this May 22, 2024
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.

None yet

3 participants