Skip to content

cmd/compile: ssa: LoweredNilCheck downgrade perfomance of binary search in array #30366

Closed
@covrom

Description

@covrom

What version of Go are you using (go version)?

go version go1.11.5 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
GOARCH="amd64"
GOOS="linux"

What did you do?

Create a binary search in an sorted array int32 without checking boundaries, using unsafe.

func unsafeBinSearch(a []uint32, x uint32) uint32 {
	p := (*[1 << 31]uint32)(unsafe.Pointer(&a[0]))
	n := uint32(len(a))
	i, j := uint32(0), n
	for i < j {
		h := (i + j) >> 1
		if p[h] < x {
			i = h + 1
		} else {
			j = h
		}
	}
	return i
}

What did you expect to see?

Maximum performance of compiled code without checking the boundaries inside the loop.

What did you see instead?

Compiler hidden error is possible, that downgrade perfomance, which may occur in other cases.

The result of compiling the above code:

0x001f 00031 (intunsafe.go:9)	XORL	BX, BX
0x0021 00033 (intunsafe.go:9)	CMPL	BX, AX
0x0023 00035 (intunsafe.go:9)	JCC	60

** 0x0025 00037 (intunsafe.go:11)	TESTB	AL, (DX) **

0x0027 00039 (intunsafe.go:10)	LEAL	(BX)(AX*1), SI
0x002a 00042 (intunsafe.go:10)	SHRL	$1, SI
0x002c 00044 (intunsafe.go:11)	MOVL	(DX)(SI*4), DI
0x002f 00047 (intunsafe.go:11)	CMPL	DI, CX
0x0031 00049 (intunsafe.go:11)	JCC	56
0x0033 00051 (intunsafe.go:12)	LEAL	1(SI), BX
0x0036 00054 (intunsafe.go:12)	JMP	33
0x0038 00056 (intunsafe.go:9)	MOVL	SI, AX
0x003a 00058 (intunsafe.go:14)	JMP	33

The compiler creates an additional instruction TESTB AL, (DX) for comparing two numbers, the result of which is not used further, because is replaced by the next CMPL DI, CX.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions