Remove a few unnecessary allocations (#9009)

Gotta catch all those allocation Pokémons. Plus a few style things.

Signed-off-by: Anders Eknert <anders.eknert@apple.com>
This commit is contained in:
Anders Eknert
2026-08-12 20:22:38 +02:00
committed by GitHub
parent 8a6000dd8c
commit 1683dd6338
3 changed files with 14 additions and 29 deletions
+2 -6
View File
@@ -3678,12 +3678,8 @@ func (b *Builtin) IsNondeterministic() bool {
func (b *Builtin) Expr(operands ...*Term) *Expr { func (b *Builtin) Expr(operands ...*Term) *Expr {
ts := make([]*Term, len(operands)+1) ts := make([]*Term, len(operands)+1)
ts[0] = NewTerm(b.Ref()) ts[0] = NewTerm(b.Ref())
for i := range operands { copy(ts[1:], operands)
ts[i+1] = operands[i] return &Expr{Terms: ts}
}
return &Expr{
Terms: ts,
}
} }
// Call creates a new term for the built-in with the given operands. // Call creates a new term for the built-in with the given operands.
+11 -20
View File
@@ -655,12 +655,12 @@ func unify1(env *TypeEnv, term *Term, tpe types.Type, union bool) bool {
return unifies(env.GetByValue(v), tpe) return unifies(env.GetByValue(v), tpe)
case Var: case Var:
if !union { if !union {
if exist := env.GetByValue(v); exist != nil { if exist := env.GetByValue(term.Value); exist != nil {
return unifies(exist, tpe) return unifies(exist, tpe)
} }
env.tree.PutOne(term.Value, tpe) env.tree.PutOne(term.Value, tpe)
} else { } else {
env.tree.PutOne(term.Value, types.Or(env.GetByValue(v), tpe)) env.tree.PutOne(term.Value, types.Or(env.GetByValue(term.Value), tpe))
} }
return true return true
default: default:
@@ -955,9 +955,10 @@ func unifies(a, b types.Type) bool {
// NOTE(sr): variadic functions can only be internal ones, and we've forbidden // NOTE(sr): variadic functions can only be internal ones, and we've forbidden
// their replacement via `with`; so we disregard variadic here // their replacement via `with`; so we disregard variadic here
if types.Arity(a) == types.Arity(b) { if types.Arity(a) == types.Arity(b) {
b := b.(*types.Function) aArgs := a.FuncArgs()
for i := range a.FuncArgs().Args { bArgs := b.(*types.Function).FuncArgs()
if !unifies(a.FuncArgs().Arg(i), b.FuncArgs().Arg(i)) { for i := range aArgs.Args {
if !unifies(aArgs.Arg(i), bArgs.Arg(i)) {
return false return false
} }
} }
@@ -1195,9 +1196,7 @@ func getOneOfForNode(node *typeTreeNode) (result []Value) {
result = append(result, k) result = append(result, k)
return false return false
}) })
return util.SortedFunc(result, Value.Compare)
slices.SortFunc(result, Value.Compare)
return result
} }
func getOneOfForType(tpe types.Type) (result []Value) { func getOneOfForType(tpe types.Type) (result []Value) {
@@ -1221,9 +1220,7 @@ func getOneOfForType(tpe types.Type) (result []Value) {
} }
} }
result = removeDuplicate(result) return util.SortedFunc(removeDuplicate(result), Value.Compare)
slices.SortFunc(result, Value.Compare)
return result
} }
func removeDuplicate(list []Value) []Value { func removeDuplicate(list []Value) []Value {
@@ -1273,16 +1270,11 @@ func override(ref Ref, t types.Type, o types.Type, rule *Rule) (types.Type, *Err
} }
obj, ok := t.(*types.Object) obj, ok := t.(*types.Object)
if !ok { if !ok {
newType, err := getObjectType(ref, o, rule, dynamicAnyAny) return getObjectType(ref, o, rule, dynamicAnyAny)
if err != nil {
return nil, err
}
return newType, nil
} }
found := false found := false
if ok { if ok {
staticProps := obj.StaticProperties() for _, prop := range obj.StaticProperties() {
for _, prop := range staticProps {
valueCopy := prop.Value valueCopy := prop.Value
key, err := InterfaceToValue(prop.Key) key, err := InterfaceToValue(prop.Key)
if err != nil { if err != nil {
@@ -1329,8 +1321,7 @@ func getKeys(ref Ref, rule *Rule) ([]any, *Error) {
func getObjectTypeRec(keys []any, o types.Type, d *types.DynamicProperty) *types.Object { func getObjectTypeRec(keys []any, o types.Type, d *types.DynamicProperty) *types.Object {
if len(keys) == 1 { if len(keys) == 1 {
staticProps := []*types.StaticProperty{types.NewStaticProperty(keys[0], o)} return types.NewObject([]*types.StaticProperty{types.NewStaticProperty(keys[0], o)}, d)
return types.NewObject(staticProps, d)
} }
staticProps := []*types.StaticProperty{types.NewStaticProperty(keys[0], getObjectTypeRec(keys[1:], o, d))} staticProps := []*types.StaticProperty{types.NewStaticProperty(keys[0], getObjectTypeRec(keys[1:], o, d))}
+1 -3
View File
@@ -964,9 +964,7 @@ func Compare(a, b Type) int {
} }
return Compare(setA.of, setB.of) return Compare(setA.of, setB.of)
case Any: case Any:
sl1 := typeSlice(a.(Any)) return typeSliceCompare(typeSlice(a.(Any)), typeSlice(b.(Any)))
sl2 := typeSlice(b.(Any))
return typeSliceCompare(sl1, sl2)
case *Function: case *Function:
fA := a.(*Function) fA := a.(*Function)
fB := b.(*Function) fB := b.(*Function)