Fix VM memory allocation optimizations by reducing what we allocate on the heap
This commit is contained in:
@@ -70,13 +70,13 @@ func (e *encoder) WriteObjects(objs ...object.Object) (err error) {
|
||||
err = errors.Join(err, e.WriteValue(obj.Type()))
|
||||
|
||||
switch o := obj.(type) {
|
||||
case *object.Null:
|
||||
case object.Null:
|
||||
break
|
||||
case *object.Boolean:
|
||||
case object.Boolean:
|
||||
err = errors.Join(err, e.WriteValue(o.Value))
|
||||
case *object.Integer:
|
||||
case object.Integer:
|
||||
err = errors.Join(err, e.WriteValue(o.Value))
|
||||
case *object.String:
|
||||
case object.String:
|
||||
err = errors.Join(err, e.WriteValue(len(o.Value)))
|
||||
err = errors.Join(err, e.WriteValue(o.Value))
|
||||
case *object.CompiledFunction:
|
||||
@@ -146,13 +146,13 @@ func (d *decoder) Objects(len int) (o []object.Object) {
|
||||
for i := 0; i < len; i++ {
|
||||
switch t := object.Type(d.Int()); t {
|
||||
case object.NullType:
|
||||
o = append(o, &object.Null{})
|
||||
o = append(o, object.Null{})
|
||||
case object.BooleanType:
|
||||
o = append(o, &object.Boolean{Value: d.Byte() == 1})
|
||||
o = append(o, object.Boolean{Value: d.Byte() == 1})
|
||||
case object.IntegerType:
|
||||
o = append(o, &object.Integer{Value: d.Int64()})
|
||||
o = append(o, object.Integer{Value: d.Int64()})
|
||||
case object.StringType:
|
||||
o = append(o, &object.String{Value: d.String(d.Int())})
|
||||
o = append(o, object.String{Value: d.String(d.Int())})
|
||||
case object.CFunctionType:
|
||||
// The order of the fields has to reflect the data layout in the encoded bytecode.
|
||||
o = append(o, &object.CompiledFunction{
|
||||
|
||||
@@ -173,7 +173,7 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
}
|
||||
|
||||
case *ast.IntegerLiteral:
|
||||
integer := &object.Integer{Value: node.Value}
|
||||
integer := object.Integer{Value: node.Value}
|
||||
c.emit(code.OpConstant, c.addConstant(integer))
|
||||
|
||||
case *ast.Null:
|
||||
@@ -356,7 +356,7 @@ func (c *Compiler) Compile(node ast.Node) error {
|
||||
c.loadSymbol(symbol)
|
||||
|
||||
case *ast.StringLiteral:
|
||||
str := &object.String{Value: node.Value}
|
||||
str := object.String{Value: node.Value}
|
||||
c.emit(code.OpConstant, c.addConstant(str))
|
||||
|
||||
case *ast.ArrayLiteral:
|
||||
|
||||
@@ -1220,16 +1220,16 @@ func testConstants2(t *testing.T, expected []interface{}, actual []object.Object
|
||||
)
|
||||
|
||||
case string:
|
||||
assert.Equal(constant, actual[i].(*object.String).Value)
|
||||
assert.Equal(constant, actual[i].(object.String).Value)
|
||||
|
||||
case int:
|
||||
assert.Equal(int64(constant), actual[i].(*object.Integer).Value)
|
||||
assert.Equal(int64(constant), actual[i].(object.Integer).Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testIntegerObject(expected int64, actual object.Object) interface{} {
|
||||
result, ok := actual.(*object.Integer)
|
||||
result, ok := actual.(object.Integer)
|
||||
if !ok {
|
||||
return fmt.Errorf("object is not Integer. got=%T (%+v", actual, actual)
|
||||
}
|
||||
@@ -1242,7 +1242,7 @@ func testIntegerObject(expected int64, actual object.Object) interface{} {
|
||||
}
|
||||
|
||||
func testStringObject(expected string, actual object.Object) error {
|
||||
result, ok := actual.(*object.String)
|
||||
result, ok := actual.(object.String)
|
||||
if !ok {
|
||||
return fmt.Errorf("object is not String. got %T (%+v", actual, actual)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user