Skip to content

Commit d7a5578

Browse files
committed
Lower core instance checks to type assertions
1 parent 9976726 commit d7a5578

5 files changed

Lines changed: 1063 additions & 1041 deletions

File tree

pkg/runtime/codegen.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,9 +1964,15 @@ func (g *Generator) generateInvokeDefault(invokeNode *ast.InvokeNode) string {
19641964

19651965
// Generate the arguments
19661966
var argExprs []string
1967-
skipInstanceType := externalTarget != nil &&
1968-
externalTarget.directLinked &&
1969-
externalTarget.intrinsic == "instance?"
1967+
_, staticInstance := g.staticInstanceCall(
1968+
invokeNode,
1969+
[]string{"", "value"},
1970+
)
1971+
skipInstanceType := staticInstance &&
1972+
((aotTarget != nil && aotTarget.directLinked) ||
1973+
(externalTarget != nil &&
1974+
externalTarget.directLinked &&
1975+
externalTarget.intrinsic == "instance?"))
19701976
for index, arg := range invokeNode.Args {
19711977
if skipInstanceType && index == 0 {
19721978
argExprs = append(argExprs, "")
@@ -2004,6 +2010,10 @@ func (g *Generator) generateInvokeDefault(invokeNode *ast.InvokeNode) string {
20042010
}
20052011
if aotTarget != nil {
20062012
if aotTarget.directLinked {
2013+
if instanceCall, ok := g.staticInstanceCall(invokeNode, argExprs); ok {
2014+
g.writef("%s := %s\n", resultVar, instanceCall)
2015+
return resultVar
2016+
}
20072017
if slot := aotTarget.directArityVars[len(argExprs)]; slot != "" {
20082018
g.writef("%s := %s(%s)\n",
20092019
resultVar,
@@ -3732,7 +3742,10 @@ func (g *Generator) allocKWVar(kw string) string {
37323742

37333743
var (
37343744
runtimeOwnedVars = map[string]bool{
3735-
"in-ns": true,
3745+
// NewEnvironment supplies these roots outside the stdlib loader.
3746+
"defrecord": true,
3747+
"in-ns": true,
3748+
"record?": true,
37363749
}
37373750

37383751
wellKnownFunctions = map[uintptr]string{

pkg/runtime/codegen_internal_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,52 @@ func TestGenerateStaticInstanceCheck(t *testing.T) {
165165
}
166166
}
167167

168+
func TestGenerateStaticInstanceCheckForSameNamespaceDirectLink(t *testing.T) {
169+
instanceVar := lang.NSCore.FindInternedVar(lang.NewSymbol("instance?"))
170+
if instanceVar == nil {
171+
t.Fatal("clojure.core/instance? is not interned")
172+
}
173+
174+
var output bytes.Buffer
175+
generator := newGenerator(&output, true)
176+
generator.addImport("github.com/glojurelang/glojure/pkg/lang")
177+
generator.currentWriter = &output
178+
generator.aotNamespace = lang.NSCore
179+
target := &aotSpecializationTarget{
180+
vr: instanceVar,
181+
directLinked: true,
182+
directFnVar: "aotDirectFn0",
183+
}
184+
target.directArities[2] = true
185+
generator.aotCallTargets[instanceVar] = target
186+
187+
vectorType := reflect.TypeOf((*lang.IPersistentVector)(nil)).Elem()
188+
invoke := &ast.InvokeNode{
189+
Fn: &ast.Node{
190+
Op: ast.OpVar,
191+
Sub: &ast.VarNode{Var: instanceVar},
192+
},
193+
Args: []*ast.Node{
194+
{Op: ast.OpConst, Sub: &ast.ConstNode{Value: vectorType}},
195+
{Op: ast.OpConst, Sub: &ast.ConstNode{Value: nil}},
196+
},
197+
}
198+
generator.generateInvokeDefault(invoke)
199+
200+
generated := output.String()
201+
if !strings.Contains(
202+
generated,
203+
"lang.IsInstance[lang.IPersistentVector](nil)",
204+
) {
205+
t.Fatalf("same-namespace instance? did not use a type assertion:\n%s",
206+
generated)
207+
}
208+
if strings.Contains(generated, "aotDirectFn0") {
209+
t.Fatalf("same-namespace instance? retained function dispatch:\n%s",
210+
generated)
211+
}
212+
}
213+
168214
func TestGenerateDirectCallsForKnownFunctionArities(t *testing.T) {
169215
ns := lang.FindOrCreateNamespace(lang.NewSymbol("codegen.direct-known-arities"))
170216
ns.ReferAllSnapshot(lang.NSCore, nil)

pkg/runtime/defrecord_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !glj_aot_runtime
2+
13
package runtime
24

35
import (

0 commit comments

Comments
 (0)