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 {
ts := make([]*Term, len(operands)+1)
ts[0] = NewTerm(b.Ref())
for i := range operands {
ts[i+1] = operands[i]
}
return &Expr{
Terms: ts,
}
copy(ts[1:], operands)
return &Expr{Terms: ts}
}
// 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)
case Var:
if !union {
if exist := env.GetByValue(v); exist != nil {
if exist := env.GetByValue(term.Value); exist != nil {
return unifies(exist, tpe)
}
env.tree.PutOne(term.Value, tpe)
} 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
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
// their replacement via `with`; so we disregard variadic here
if types.Arity(a) == types.Arity(b) {
b := b.(*types.Function)
for i := range a.FuncArgs().Args {
if !unifies(a.FuncArgs().Arg(i), b.FuncArgs().Arg(i)) {
aArgs := a.FuncArgs()
bArgs := b.(*types.Function).FuncArgs()
for i := range aArgs.Args {
if !unifies(aArgs.Arg(i), bArgs.Arg(i)) {
return false
}
}
@@ -1195,9 +1196,7 @@ func getOneOfForNode(node *typeTreeNode) (result []Value) {
result = append(result, k)
return false
})
slices.SortFunc(result, Value.Compare)
return result
return util.SortedFunc(result, Value.Compare)
}
func getOneOfForType(tpe types.Type) (result []Value) {
@@ -1221,9 +1220,7 @@ func getOneOfForType(tpe types.Type) (result []Value) {
}
}
result = removeDuplicate(result)
slices.SortFunc(result, Value.Compare)
return result
return util.SortedFunc(removeDuplicate(result), Value.Compare)
}
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)
if !ok {
newType, err := getObjectType(ref, o, rule, dynamicAnyAny)
if err != nil {
return nil, err
}
return newType, nil
return getObjectType(ref, o, rule, dynamicAnyAny)
}
found := false
if ok {
staticProps := obj.StaticProperties()
for _, prop := range staticProps {
for _, prop := range obj.StaticProperties() {
valueCopy := prop.Value
key, err := InterfaceToValue(prop.Key)
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 {
if len(keys) == 1 {
staticProps := []*types.StaticProperty{types.NewStaticProperty(keys[0], o)}
return types.NewObject(staticProps, d)
return types.NewObject([]*types.StaticProperty{types.NewStaticProperty(keys[0], 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)
case Any:
sl1 := typeSlice(a.(Any))
sl2 := typeSlice(b.(Any))
return typeSliceCompare(sl1, sl2)
return typeSliceCompare(typeSlice(a.(Any)), typeSlice(b.(Any)))
case *Function:
fA := a.(*Function)
fB := b.(*Function)