mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-08 04:53:42 +00:00
Optimize/eliminate bounds checking
TODO: do better at it by tracking min/max values of integers. The
following straightforward code doesn't have its bounds checks removed:
for _, n := range slice {
println(n)
}
This commit is contained in:
@@ -66,6 +66,7 @@ func hashmapMake(keySize, valueSize uint8) *hashmap {
|
||||
}
|
||||
|
||||
// Set a specified key to a given value. Grow the map if necessary.
|
||||
//go:nobounds
|
||||
func hashmapSet(m *hashmap, key unsafe.Pointer, value unsafe.Pointer, hash uint32, keyEqual func(x, y unsafe.Pointer, n uintptr) bool) {
|
||||
numBuckets := uintptr(1) << m.bucketBits
|
||||
bucketNumber := (uintptr(hash) & (numBuckets - 1))
|
||||
@@ -114,6 +115,7 @@ func hashmapSet(m *hashmap, key unsafe.Pointer, value unsafe.Pointer, hash uint3
|
||||
}
|
||||
|
||||
// Get the value of a specified key, or zero the value if not found.
|
||||
//go:nobounds
|
||||
func hashmapGet(m *hashmap, key unsafe.Pointer, value unsafe.Pointer, hash uint32, keyEqual func(x, y unsafe.Pointer, n uintptr) bool) {
|
||||
numBuckets := uintptr(1) << m.bucketBits
|
||||
bucketNumber := (uintptr(hash) & (numBuckets - 1))
|
||||
|
||||
@@ -44,6 +44,7 @@ var (
|
||||
|
||||
// Get the function pointer for the method on the interface.
|
||||
// This is a compiler intrinsic.
|
||||
//go:nobounds
|
||||
func interfaceMethod(itf _interface, method uint16) *uint8 {
|
||||
// This function doesn't do bounds checking as the supplied method must be
|
||||
// in the list of signatures. The compiler will only emit
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
//go:nobounds
|
||||
func printstring(s string) {
|
||||
for i := 0; i < len(s); i++ {
|
||||
putchar(s[i])
|
||||
@@ -42,6 +43,7 @@ func printint16(n uint16) {
|
||||
printint32(int32(n))
|
||||
}
|
||||
|
||||
//go:nobounds
|
||||
func printuint32(n uint32) {
|
||||
digits := [10]byte{} // enough to hold (2^32)-1
|
||||
// Fill in all 10 digits.
|
||||
|
||||
@@ -56,7 +56,7 @@ func _panic(message interface{}) {
|
||||
abort()
|
||||
}
|
||||
|
||||
// Check for bounds in *ssa.IndexAddr and *ssa.Lookup.
|
||||
// Check for bounds in *ssa.Index, *ssa.IndexAddr and *ssa.Lookup.
|
||||
func lookupBoundsCheck(length, index int) {
|
||||
if index < 0 || index >= length {
|
||||
// printstring() here is safe as this function is excluded from bounds
|
||||
|
||||
@@ -13,6 +13,7 @@ type _string struct {
|
||||
}
|
||||
|
||||
// Return true iff the strings match.
|
||||
//go:nobounds
|
||||
func stringEqual(x, y string) bool {
|
||||
if len(x) != len(y) {
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user