Skip to content

net/netip: Addr.AsSlice should be inlineable #56136

Closed
@dsnet

Description

@dsnet

This method always allocates because it returns a slice that escapes to the return value.

If the method were inlineable, the compiler may prove in some cases that the return value does not escape and can be stack allocated.

Currently, the method is a cost of 222; way over the budget of 80.

One attempt to optimize this was:

func (ip Addr) AsSlice() []byte {
	ret := ip.As16()
	switch ip.z {
	case z0:
		return nil
	case z4:
		return ret[12:]
	default:
		return ret[:]
	}
}

However, this is a function cost of 83. Just slightly over the budget.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions