Browse Source

Use integer value cache for values from -256 to -1. Positive values (0-255) are covered by the Go runtime since 1.15.

Dmitry Panov 2 years ago
parent
commit
cc4634e76e
2 changed files with 4 additions and 5 deletions
  1. 1 2
      value.go
  2. 3 3
      vm.go

+ 1 - 2
value.go

@@ -1172,7 +1172,6 @@ func typeErrorResult(throw bool, args ...interface{}) {
 
 
 func init() {
 func init() {
 	for i := 0; i < 256; i++ {
 	for i := 0; i < 256; i++ {
-		intCache[i] = valueInt(i - 128)
+		intCache[i] = valueInt(i - 256)
 	}
 	}
-	_positiveZero = intToValue(0)
 }
 }

+ 3 - 3
vm.go

@@ -340,10 +340,10 @@ type instruction interface {
 }
 }
 
 
 func intToValue(i int64) Value {
 func intToValue(i int64) Value {
+	if idx := 256 + i; idx >= 0 && idx < 256 {
+		return intCache[idx]
+	}
 	if i >= -maxInt && i <= maxInt {
 	if i >= -maxInt && i <= maxInt {
-		if i >= -128 && i <= 127 {
-			return intCache[i+128]
-		}
 		return valueInt(i)
 		return valueInt(i)
 	}
 	}
 	return valueFloat(i)
 	return valueFloat(i)