Skip to content

Commit d9bb1fb

Browse files
authored
Merge pull request #2030 from andrew4328/more-compiler-optimizations-towards-full-gc-fdsjkdsl
All tests passing. Down to 7.8 minutes for all tests to run from 9.5 minutes previously.
2 parents cc776c7 + d27eb71 commit d9bb1fb

10 files changed

Lines changed: 3613 additions & 3537 deletions

File tree

BOOTSTRAP/cli.c

Lines changed: 3471 additions & 3461 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ LSTSFLAGS = MALLOC_CHECK_=3
88
# recommendation: ulimit -s unlimited
99

1010
dev: install-production
11-
lm tests/promises/vector/constructor.lsts
11+
lm --v23 --showastcount SRC/index.lsts
1212
#time lm --showalloc SRC/unit-type-core.lsts > out.txt
1313
#time lm --showalloc SRC/unit-tctx-core.lsts > out.txt
1414
#time lm --showalloc SRC/unit-prop-core.lsts > out.txt
1515
#time lm --showalloc SRC/unit-ascript-core.lsts > out.txt
1616
#time lm --showalloc SRC/index.lsts > out.txt
1717
#time lm --showalloc SRC/dev-index.lsts > out.txt
18-
gcc tmp.c
19-
./a.out
2018

2119
build: compile-production
2220
time env $(LSTSFLAGS) ./production --v23 --c -o deploy1.c SRC/index.lsts

PLUGINS/FRONTEND/LSTS/lsts-parse.lsts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,13 +1882,13 @@ let lsts-parse-atom-without-tail(tokens: List<Token>): Tuple<AST,List<Token>> =
18821882
tokens = rhs-rest.second;
18831883
lsts-parse-expect(c"}", tokens); tokens = tail(tokens);
18841884
term = mk-app(
1885-
mk-app(
1886-
Var( c"while", with-location(mk-token("while"),loc) ),
1887-
c-rest.first
1888-
),
1889-
mk-app(
1890-
Var( c"scope", with-location(mk-token("scope"),loc) ),
1891-
rhs-rest.first
1885+
Var( c"macro::while", with-location(mk-token("macro::while"),loc) ),
1886+
mk-cons(
1887+
c-rest.first,
1888+
mk-app(
1889+
Var( c"scope", with-location(mk-token("scope"),loc) ),
1890+
rhs-rest.first
1891+
)
18921892
)
18931893
);
18941894
} else if lsts-parse-head(tokens)==c"match2" {

SRC/ast-constructor.lsts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11

2+
let ast-count = 0_sz;
3+
24
let mk-app(f: AST, a: AST): AST = (
5+
ast-count = ast-count + 1;
36
App( close(f), close(a) )
47
);
58

69
let mk-cons-or-app(is-cons: Bool, f: AST, a: AST): AST = (
10+
ast-count = ast-count + 1;
711
App( is-cons, close(f), close(a) )
812
);
913

1014
let mk-cons(f: AST, a: AST): AST = (
15+
ast-count = ast-count + 1;
1116
App( true, close(f), close(a) )
1217
);
1318

1419
let mk-glb(k: Token, v: AST): AST = (
20+
ast-count = ast-count + 1;
1521
Glb( k, close(v) )
1622
);
1723

1824
let mk-seq(): AST = (
25+
ast-count = ast-count + 1;
1926
Seq( mk-vector(type(AST)) )
2027
);
2128

2229
let mk-typedef(loc: SourceLocation, lhs-type: Type): AST = (
30+
ast-count = ast-count + 1;
2331
Typedef( loc, lhs-type, mk-vector(type(Type)), mk-vector(type(Type)),
2432
ta, ta, ta, mk-vector(type((CString,Vector<(CString,Type)>))), ta, ta )
2533
);
@@ -185,10 +193,12 @@ let .with-implied-phi(term: AST, implied-phi: Type): AST = (
185193
);
186194

187195
let mk-abs(l: AST, r: AST, t: Type): AST = (
196+
ast-count = ast-count + 1;
188197
Abs( close(l), close(r), t )
189198
);
190199

191200
let mk-meta(l: AST): AST = (
201+
ast-count = ast-count + 1;
192202
Meta( close(l) )
193203
);
194204

@@ -204,34 +214,47 @@ let .is-ascript(t: AST): Bool = (
204214
);
205215

206216
let mk-var(val: CString): AST = (
217+
ast-count = ast-count + 1;
207218
Var( val, mk-token(val) )
208219
);
209220

210221
let mk-var(val: String): AST = (
222+
ast-count = ast-count + 1;
211223
Var( val.into(type(CString)), mk-token(val) )
212224
);
213225

214226
let mk-var(val: Token): AST = (
227+
ast-count = ast-count + 1;
215228
Var( val.key, val )
216229
);
217230

218-
let mk-var(v: CString, vtk: Token): AST = Var(v,vtk);
231+
let mk-var(v: CString, vtk: Token): AST = (
232+
ast-count = ast-count + 1;
233+
Var(v,vtk);
234+
);
219235

220236
let mk-lit(val: CString): AST = (
237+
ast-count = ast-count + 1;
221238
Lit( val, mk-token(val) )
222239
);
223240

224241
let mk-lit(val: String): AST = (
242+
ast-count = ast-count + 1;
225243
Lit( val.into(type(CString)), mk-token(val) )
226244
);
227245

228246
let mk-lit(val: Token): AST = (
247+
ast-count = ast-count + 1;
229248
Lit( val.key, val )
230249
);
231250

232-
let mk-lit(v: CString, vtk: Token): AST = Lit(v,vtk);
251+
let mk-lit(v: CString, vtk: Token): AST = (
252+
ast-count = ast-count + 1;
253+
Lit(v,vtk);
254+
);
233255

234256
let mk-atype(tt: Type): AST = (
257+
ast-count = ast-count + 1;
235258
AType(tt)
236259
);
237260

SRC/type-constructor.lsts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ let t2(tag: CString, p1: Type, p2: Type): Type = TGround(tag, mk-vector(type(Typ
2020
# new allocations = 0
2121
let tv(name: CString): Type = TVar(name);
2222

23+
# new allocations = 0
24+
let type-any-arrow = t2(c"Arrow", t0(c"Any"), t0(c"Any"));
25+
2326
# new allocations = 0 if either argument is ?
2427
# | 1
2528
let $"&&"(lt: Type, rt: Type): Type = (

SRC/typecheck-infer-expr.lsts

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,19 @@ let std-infer-expr(tctx: Maybe<TypeContext>, term: AST, is-scoped: Bool, used: I
8585
add-concrete-type-instance(tt, term);
8686
(tctx, let new-t) = std-infer-expr(tctx, t, false, Tail, tt);
8787
if not(is(t,new-t)) then { t = new-t; term = mk-app(asc, mk-app(t, mk-atype(tt))); };
88+
let is-nullary = false;
8889
if typeof-term(t).is-arrow and not(tt.is-arrow) and non-zero(var-name-if-var-or-lit(t)) {
90+
# This case exists for nullary constructors like TAny : Type
8991
let ftype = tctx.find-callable( var-name-if-var-or-lit(t), t0(c"Nil"), term, tt ).direct-type;
9092
tctx = tctx.ascript(term, ftype);
93+
is-nullary = true;
94+
} else if typeof-term(t) == type-any-arrow {
95+
tctx = tctx.ascript(term, tt);
9196
} else {
9297
tctx = tctx.ascript(term, typeof-term(t));
9398
};
9499
let direct-tctx = tctx.get-or(mk-tctx());
95-
if not(direct-tctx.is-blob or typeof-term(t).is-arrow) {
100+
if not(direct-tctx.is-blob or is-nullary) {
96101
# TODO: remove the special exception for "Arrows" here, which is a workaround for raw constructors
97102
# frontend is producing ((None : TContext?)()) instead of (None() : TContext?)
98103
# the return hint stuff is working, so all the hard work is done. I think only the frontend needs to change
@@ -225,29 +230,55 @@ let std-infer-expr(tctx: Maybe<TypeContext>, term: AST, is-scoped: Bool, used: I
225230
};
226231
);
227232
Var{key2=key, token=token} => (
228-
(tctx, let vt) = typeof-var(term, tctx, key2, hint.is-t(c"MustNotFresh",0) or hint.is-t(c"TailPosition",0));
229-
vt = tctx.with-phi(vt, term);
230-
mark-var-to-def-todo(tctx, key2, ta, term);
231-
# TailPosition LocalVariables don't need to be retained because +1/-1 retain/release cancels itself out
232-
if not(hint.is-t(c"TailPosition",0) and vt.is-t(c"LocalVariable",0))
233-
and not(hint.is-t(c"MustNotRetain",0))
234-
and not(tctx.get-or(mk-tctx()).is-blob) {
235-
tctx = tctx.ascript(term, vt);
236-
(tctx, term) = maybe-retain(tctx, term);
237-
} else {
238-
tctx = tctx.ascript(term, vt);
233+
let rewritten = false;
234+
if not non-zero(hint) {
235+
for list Tuple{sfxs1=first, sfxtt1=second} in parse-suffixes {
236+
if key2.has-suffix(sfxs1) {
237+
let lpfx1 = key2.remove-suffix(sfxs1).get-or(c"");
238+
term = mk-app( mk-lit(c":",with-key(token,c":")), mk-app( mk-lit(lpfx1,with-key(token,lpfx1)), mk-atype(sfxtt1) ) );
239+
(tctx, term) = std-infer-expr(tctx, term, is-scoped, used, ta);
240+
rewritten = true;
241+
}
242+
}
243+
};
244+
if not rewritten {
245+
(tctx, let vt) = typeof-var(term, tctx, key2, hint.is-t(c"MustNotFresh",0) or hint.is-t(c"TailPosition",0));
246+
vt = tctx.with-phi(vt, term);
247+
mark-var-to-def-todo(tctx, key2, ta, term);
248+
# TailPosition LocalVariables don't need to be retained because +1/-1 retain/release cancels itself out
249+
if not(hint.is-t(c"TailPosition",0) and vt.is-t(c"LocalVariable",0))
250+
and not(hint.is-t(c"MustNotRetain",0))
251+
and not(tctx.get-or(mk-tctx()).is-blob) {
252+
tctx = tctx.ascript(term, vt);
253+
(tctx, term) = maybe-retain(tctx, term);
254+
} else {
255+
tctx = tctx.ascript(term, vt);
256+
};
239257
};
240258
);
241259
Lit{key3=key, token=token} => (
242-
if hint.is-t(c"Literal",0) {
243-
tctx = tctx.ascript(term,hint);
244-
} else if not(non-zero(typeof-term(term))) {
245-
if hint.is-t(c"HashtableEq",2) and hint.is-datatype and key3==c"HashtableEqEOF"
246-
then {
247-
(tctx, let lit-tt) = tctx.apply-callable(key3, t0(c"Nil"), term, hint);
248-
tctx = tctx.ascript(term, lit-tt);
249-
} else {
250-
tctx = tctx.ascript(term, t2(c"Arrow",t0(c"Any"),t0(c"Any")));
260+
let rewritten = false;
261+
if not non-zero(hint) {
262+
for list Tuple{sfxs2=first, sfxtt2=second} in parse-suffixes {
263+
if key3.has-suffix(sfxs2) {
264+
let lpfx2 = key3.remove-suffix(sfxs2).get-or(c"");
265+
term = mk-app( mk-lit(c":",with-key(token,c":")), mk-app( mk-lit(lpfx2,with-key(token,lpfx2)), mk-atype(sfxtt2) ) );
266+
(tctx, term) = std-infer-expr(tctx, term, is-scoped, used, ta);
267+
rewritten = true;
268+
}
269+
}
270+
};
271+
if not rewritten {
272+
if hint.is-t(c"Literal",0) {
273+
tctx = tctx.ascript(term,hint);
274+
} else if not(non-zero(typeof-term(term))) {
275+
if hint.is-t(c"HashtableEq",2) and hint.is-datatype and key3==c"HashtableEqEOF"
276+
then {
277+
(tctx, let lit-tt) = tctx.apply-callable(key3, t0(c"Nil"), term, hint);
278+
tctx = tctx.ascript(term, lit-tt);
279+
} else {
280+
tctx = tctx.ascript(term, type-any-arrow);
281+
}
251282
}
252283
}
253284
);

SRC/typecheck-std-apply-macro.lsts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ let std-apply-macro-location(tctx: Maybe<TypeContext>, mname: CString, margs: AS
5858

5959
let std-apply-macro(tctx: Maybe<TypeContext>, mname: CString, margs: AST, used: IsUsed, strong: Bool): (TypeContext?, AST) = (
6060
let result = mk-eof();
61+
let peeped-type = ta;
6162
if mname==c"macro::concat" then (tctx, result) = std-apply-macro-concat(tctx, mname, margs)
6263
else if mname==c"macro::location" then (tctx, result) = std-apply-macro-location(tctx, mname, margs)
6364
else if mname==c"macro::variable" then (tctx, result) = std-apply-macro-variable(tctx, mname, margs)
@@ -76,7 +77,7 @@ let std-apply-macro(tctx: Maybe<TypeContext>, mname: CString, margs: AST, used:
7677
peeped = mtype;
7778
};
7879
};
79-
(tctx, let peeped-type, margs) = std-infer-peeped-arguments(tctx, margs, peep-holes);
80+
(tctx, peeped-type, margs) = std-infer-peeped-arguments(tctx, margs, peep-holes);
8081

8182
let matched = [] : List<(Type,AST)>;
8283
for list Tuple{mtype=first, mterm=third} in row {
@@ -102,7 +103,7 @@ let std-apply-macro(tctx: Maybe<TypeContext>, mname: CString, margs: AST, used:
102103
(tctx, result) = std-apply-macro-candidates(tctx, mname, margs, candidates);
103104
};
104105

105-
if strong and not(non-zero(result)) then exit-error("Failed to Apply Macro: \{mname}\nArgs: \{margs}\n", margs);
106+
if strong and not(non-zero(result)) then exit-error("Failed to Apply Macro: \{mname}\nArgs: \{margs} : \{peeped-type}\n", margs);
106107
if strong {
107108
(tctx, result) = std-infer-expr(tctx, result, false, used, ta);
108109
mark-free-and-seen(result);

SRC/unit-main-core.lsts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ let print-toks-json(fp: CString): Nil = (
3131
let show-alloc-count = false;
3232
let show-alloc-count-in-program = false;
3333

34+
let show-ast-count = false;
35+
let ast-parsed-count = 0_sz;
36+
let ast-typed-count = 0_sz;
37+
3438
let main(argc: C_int, argv: CString[]): Nil = (
3539
config-v23 = false;
3640
let argi = 1_sz;
@@ -61,6 +65,7 @@ let main(argc: C_int, argv: CString[]): Nil = (
6165
c"--stripdebug" => config-strip-debug = true;
6266
c"--showalloc" => show-alloc-count = true;
6367
c"--showallocgen" => show-alloc-count-in-program = true;
68+
c"--showastcount" => show-ast-count = true;
6469
c"--profile-ast" => config-profile-ast = true;
6570
c"-o" => (
6671
argi = argi + 1;
@@ -128,10 +133,12 @@ let main(argc: C_int, argv: CString[]): Nil = (
128133
)
129134
);
130135
};
136+
ast-parsed-count = ast-count;
131137
match config-mode {
132138
ModeTypecheck{} => (preprocess(); typecheck(););
133139
ModeCompile{} => (preprocess(); typecheck(); plugin-current-backend(); );
134140
};
141+
ast-typed-count = ast-count;
135142
);
136143
};
137144
if show-alloc-count then {
@@ -140,6 +147,10 @@ let main(argc: C_int, argv: CString[]): Nil = (
140147
if config-profile-ast then {
141148
profile-print-ast()
142149
};
150+
if show-ast-count then {
151+
print("AST Count After Parse = \{ast-parsed-count}\n");
152+
print("AST Count After Typecheck = \{ast-typed-count}\n");
153+
};
143154
#print("Worse Phi Length \{worst-phi-length}\n");
144155
#print-allocation-counts()
145156
);

0 commit comments

Comments
 (0)