Skip to content

Commit cc9b892

Browse files
committed
Improves python performance.
1 parent 4888d86 commit cc9b892

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3879
-2192
lines changed

src/generators/python.ml

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ let uoperator (op : uoperator) =
7878

7979
let rec print_exp (e : exp) =
8080
match e.e with
81-
| EEmptyValue -> Pla.string "{}"
81+
| EEmptyValue -> (
82+
(* Create class instance based on type *)
83+
match e.t.t with
84+
| TStruct { path; _ } -> {%pla|<#path#s>()|}
85+
| _ -> Pla.string "None")
8286
| EUnit -> Pla.string "None"
8387
| EBool v ->
8488
Pla.string
@@ -201,18 +205,19 @@ let rec print_exp (e : exp) =
201205
{%pla|[<#l#>]|}
202206
| EMember (e, m) ->
203207
let e = print_exp e in
204-
{%pla|<#e#>["<#m#s>"]|}
208+
{%pla|<#e#>.<#m#s>|}
205209
| ETMember (e, i) ->
206210
let e = print_exp e in
207211
let m = i in
208212
{%pla|<#e#>[<#m#i>]|}
209-
| ERecord { elems; _ } ->
213+
| ERecord { path; elems } ->
214+
(* Generate class instantiation with keyword arguments *)
210215
let printElem (n, v) =
211216
let v = print_exp v in
212-
{%pla|"<#n#s>": <#v#>|}
217+
{%pla|<#n#s>=<#v#>|}
213218
in
214219
let elems = Pla.map_sep Pla.commaspace printElem elems in
215-
{%pla|{<#elems#>}|}
220+
{%pla|<#path#s>(<#elems#>)|}
216221

217222

218223
let rec print_lexp (e : lexp) =
@@ -221,7 +226,7 @@ let rec print_lexp (e : lexp) =
221226
| LId s -> Pla.string s
222227
| LMember (e, m) ->
223228
let e = print_lexp e in
224-
{%pla|<#e#>["<#m#s>"]|}
229+
{%pla|<#e#>.<#m#s>|}
225230
| LIndex { e; index = { e = EInt i; _ } } ->
226231
let e = print_lexp e in
227232
let index = i in
@@ -241,14 +246,14 @@ let print_dexp (e : dexp) =
241246

242247
let rec print_stmt (s : stmt) =
243248
match s.s with
244-
(* if the name is _ctx, initialize as empty dict *)
245-
| StmtDecl (({ d = DId ("_ctx", _); t = { t = TStruct _; _ }; _ } as lhs), None) ->
249+
(* if the name is _ctx, create a class instance *)
250+
| StmtDecl (({ d = DId ("_ctx", _); t = { t = TStruct { path; _ }; _ }; _ } as lhs), None) ->
246251
let lhs = print_dexp lhs in
247-
{%pla|<#lhs#> = {}|}
248-
(* needs allocation - call allocator function *)
252+
{%pla|<#lhs#> = <#path#s>()|}
253+
(* needs allocation - create class instance directly *)
249254
| StmtDecl (({ t = { t = TStruct { path; _ }; _ }; _ } as lhs), None) ->
250255
let lhs = print_dexp lhs in
251-
{%pla|<#lhs#> = <#path#s>_alloc()|}
256+
{%pla|<#lhs#> = <#path#s>()|}
252257
(* declaration without value - initialize to None *)
253258
| StmtDecl (lhs, None) ->
254259
let lhs = print_dexp lhs in
@@ -332,7 +337,43 @@ let print_top_stmt (args : Util.Args.args) (t : top_stmt) =
332337
let body = print_body body in
333338
{%pla|<#def#><#body#><#><#>|}
334339
| TopExternal _ -> Pla.unit
335-
| TopType _ -> Pla.unit
340+
| TopType { path; members } ->
341+
(* Generate a class with __slots__ for better performance *)
342+
let member_names = CCList.map (fun (name, _, _, _) -> {%pla|'<#name#s>'|}) members |> Pla.join_sep Pla.commaspace in
343+
let getDefaultValue (t : type_) =
344+
match t.t with
345+
| TInt -> "0"
346+
| TInt16 -> "0"
347+
| TReal -> "0.0"
348+
| TFix16 -> "0.0"
349+
| TBool -> "False"
350+
| TString -> "\"\""
351+
| TStruct { path; _ } -> path ^ "()"
352+
| TArray (size_opt, _) -> (
353+
match size_opt with
354+
| Some size -> Printf.sprintf "[0.0] * %d" size
355+
| None -> "[]")
356+
| TList _ -> "[]"
357+
| _ -> "None"
358+
in
359+
(* Generate __init__ with keyword arguments for record literals *)
360+
let init_params =
361+
CCList.map
362+
(fun (name, t, _, _) ->
363+
let default = getDefaultValue t in
364+
{%pla|<#name#s>=<#default#s>|})
365+
members
366+
|> Pla.join_sep Pla.commaspace
367+
in
368+
let init_assignments =
369+
CCList.map (fun (name, _, _, _) -> {%pla| self.<#name#s> = <#name#s>|}) members |> Pla.join_sep Pla.newline
370+
in
371+
{%pla|class <#path#s>:
372+
__slots__ = [<#member_names#>]
373+
def __init__(self, <#init_params#>):
374+
<#init_assignments#>
375+
376+
|}
336377
| TopAlias _ -> Pla.unit
337378
| TopConstant (name, _, _, _, _) when args.test_mode -> {%pla|<#name#s> = {}<#>|}
338379
| TopConstant (name, _, _, rhs, _) ->

test/code/ConstSpecialization.py.base

Lines changed: 83 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,60 +18,108 @@ def float_to_int(i):
1818
def initializeArray(v, size):
1919
return [v for _ in range(size)]
2020

21+
class ConstSpecialization_accumulate_type:
22+
__slots__ = ['sum']
23+
def __init__(self, sum=0.0):
24+
self.sum = sum
25+
26+
class ConstSpecialization_accumulate_real_2_type:
27+
__slots__ = ['sum']
28+
def __init__(self, sum=0.0):
29+
self.sum = sum
30+
31+
class ConstSpecialization_test_accumulate_spec_type:
32+
__slots__ = ['inst_10ec4']
33+
def __init__(self, inst_10ec4=ConstSpecialization_accumulate_real_2_type()):
34+
self.inst_10ec4 = inst_10ec4
35+
36+
class ConstSpecialization_test_accumulate_nonspec_type:
37+
__slots__ = ['inst_17a3f']
38+
def __init__(self, inst_17a3f=ConstSpecialization_accumulate_type()):
39+
self.inst_17a3f = inst_17a3f
40+
41+
class ConstSpecialization_scale_typed_real_2_type:
42+
__slots__ = ['last']
43+
def __init__(self, last=0.0):
44+
self.last = last
45+
46+
class ConstSpecialization_scale_typed_int_2_type:
47+
__slots__ = ['last']
48+
def __init__(self, last=0):
49+
self.last = last
50+
51+
class ConstSpecialization_test_typed_int_type:
52+
__slots__ = ['inst_1c235']
53+
def __init__(self, inst_1c235=ConstSpecialization_scale_typed_int_2_type()):
54+
self.inst_1c235 = inst_1c235
55+
56+
class ConstSpecialization_test_typed_real_type:
57+
__slots__ = ['inst_15f7d']
58+
def __init__(self, inst_15f7d=ConstSpecialization_scale_typed_real_2_type()):
59+
self.inst_15f7d = inst_15f7d
60+
61+
class ConstSpecialization_main_type:
62+
__slots__ = ['inst_13dfa', 'inst_201fa', 'inst_3e2fa', 'inst_477fa']
63+
def __init__(self, inst_13dfa=ConstSpecialization_test_accumulate_spec_type(), inst_201fa=ConstSpecialization_test_accumulate_nonspec_type(), inst_3e2fa=ConstSpecialization_test_typed_int_type(), inst_477fa=ConstSpecialization_test_typed_real_type()):
64+
self.inst_13dfa = inst_13dfa
65+
self.inst_201fa = inst_201fa
66+
self.inst_3e2fa = inst_3e2fa
67+
self.inst_477fa = inst_477fa
68+
2169
def ConstSpecialization_accumulate_type_alloc():
22-
_ctx = {}
23-
_ctx["sum"] = 0.0
70+
_ctx = ConstSpecialization_accumulate_type()
71+
_ctx.sum = 0.0
2472
return _ctx
2573

2674

2775
def ConstSpecialization_accumulate_real_2_type_alloc():
28-
_ctx = {}
29-
_ctx["sum"] = 0.0
76+
_ctx = ConstSpecialization_accumulate_real_2_type()
77+
_ctx.sum = 0.0
3078
return _ctx
3179

3280

3381
def ConstSpecialization_test_accumulate_spec_type_alloc():
34-
_ctx = {}
35-
_ctx["inst_10ec4"] = ConstSpecialization_accumulate_real_2_type_alloc()
82+
_ctx = ConstSpecialization_test_accumulate_spec_type()
83+
_ctx.inst_10ec4 = ConstSpecialization_accumulate_real_2_type_alloc()
3684
return _ctx
3785

3886

3987
def ConstSpecialization_test_accumulate_nonspec_type_alloc():
40-
_ctx = {}
41-
_ctx["inst_17a3f"] = ConstSpecialization_accumulate_type_alloc()
88+
_ctx = ConstSpecialization_test_accumulate_nonspec_type()
89+
_ctx.inst_17a3f = ConstSpecialization_accumulate_type_alloc()
4290
return _ctx
4391

4492

4593
def ConstSpecialization_scale_typed_real_2_type_alloc():
46-
_ctx = {}
47-
_ctx["last"] = 0.0
94+
_ctx = ConstSpecialization_scale_typed_real_2_type()
95+
_ctx.last = 0.0
4896
return _ctx
4997

5098

5199
def ConstSpecialization_scale_typed_int_2_type_alloc():
52-
_ctx = {}
53-
_ctx["last"] = 0
100+
_ctx = ConstSpecialization_scale_typed_int_2_type()
101+
_ctx.last = 0
54102
return _ctx
55103

56104

57105
def ConstSpecialization_test_typed_int_type_alloc():
58-
_ctx = {}
59-
_ctx["inst_1c235"] = ConstSpecialization_scale_typed_int_2_type_alloc()
106+
_ctx = ConstSpecialization_test_typed_int_type()
107+
_ctx.inst_1c235 = ConstSpecialization_scale_typed_int_2_type_alloc()
60108
return _ctx
61109

62110

63111
def ConstSpecialization_test_typed_real_type_alloc():
64-
_ctx = {}
65-
_ctx["inst_15f7d"] = ConstSpecialization_scale_typed_real_2_type_alloc()
112+
_ctx = ConstSpecialization_test_typed_real_type()
113+
_ctx.inst_15f7d = ConstSpecialization_scale_typed_real_2_type_alloc()
66114
return _ctx
67115

68116

69117
def ConstSpecialization_main_type_alloc():
70-
_ctx = {}
71-
_ctx["inst_13dfa"] = ConstSpecialization_test_accumulate_spec_type_alloc()
72-
_ctx["inst_201fa"] = ConstSpecialization_test_accumulate_nonspec_type_alloc()
73-
_ctx["inst_3e2fa"] = ConstSpecialization_test_typed_int_type_alloc()
74-
_ctx["inst_477fa"] = ConstSpecialization_test_typed_real_type_alloc()
118+
_ctx = ConstSpecialization_main_type()
119+
_ctx.inst_13dfa = ConstSpecialization_test_accumulate_spec_type_alloc()
120+
_ctx.inst_201fa = ConstSpecialization_test_accumulate_nonspec_type_alloc()
121+
_ctx.inst_3e2fa = ConstSpecialization_test_typed_int_type_alloc()
122+
_ctx.inst_477fa = ConstSpecialization_test_typed_real_type_alloc()
75123
return _ctx
76124

77125

@@ -118,36 +166,36 @@ def ConstSpecialization_test_interleaved_nonspec(a):
118166
return ConstSpecialization_interleaved(5.0, a, 4.0, 3.0)
119167

120168
def ConstSpecialization_accumulate_real_2(_ctx, x):
121-
_ctx["sum"] = ((2.0 * x) + _ctx["sum"])
122-
return _ctx["sum"]
169+
_ctx.sum = ((2.0 * x) + _ctx.sum)
170+
return _ctx.sum
123171

124172

125173
def ConstSpecialization_accumulate(_ctx, scale, x):
126-
_ctx["sum"] = ((scale * x) + _ctx["sum"])
127-
return _ctx["sum"]
174+
_ctx.sum = ((scale * x) + _ctx.sum)
175+
return _ctx.sum
128176

129177

130178
def ConstSpecialization_test_accumulate_spec(_ctx):
131-
return ConstSpecialization_accumulate_real_2(_ctx["inst_10ec4"], 5.0)
179+
return ConstSpecialization_accumulate_real_2(_ctx.inst_10ec4, 5.0)
132180

133181
def ConstSpecialization_test_accumulate_nonspec(_ctx, s, x):
134-
return ConstSpecialization_accumulate(_ctx["inst_17a3f"], s, x)
182+
return ConstSpecialization_accumulate(_ctx.inst_17a3f, s, x)
135183

136184
def ConstSpecialization_scale_typed_int_2(_ctx, x):
137-
_ctx["last"] = x
185+
_ctx.last = x
138186
return x
139187

140188

141189
def ConstSpecialization_scale_typed_real_2(_ctx, x):
142-
_ctx["last"] = x
190+
_ctx.last = x
143191
return x
144192

145193

146194
def ConstSpecialization_test_typed_int(_ctx):
147-
return ConstSpecialization_scale_typed_int_2(_ctx["inst_1c235"], 42)
195+
return ConstSpecialization_scale_typed_int_2(_ctx.inst_1c235, 42)
148196

149197
def ConstSpecialization_test_typed_real(_ctx):
150-
return ConstSpecialization_scale_typed_real_2(_ctx["inst_15f7d"], 3.14)
198+
return ConstSpecialization_scale_typed_real_2(_ctx.inst_15f7d, 3.14)
151199

152200
def ConstSpecialization_setArrayElement_array_of_real_size_4_real_1(m, value):
153201
m[1] = value
@@ -198,10 +246,10 @@ def ConstSpecialization_main(_ctx):
198246
r5 = ConstSpecialization_test_weighted_nonspec(0.5)
199247
r6 = ConstSpecialization_test_interleaved_spec()
200248
r7 = ConstSpecialization_test_interleaved_nonspec(1.5)
201-
r8 = ConstSpecialization_test_accumulate_spec(_ctx["inst_13dfa"])
202-
r9 = ConstSpecialization_test_accumulate_nonspec(_ctx["inst_201fa"], 3.0, 4.0)
203-
r10 = ConstSpecialization_test_typed_int(_ctx["inst_3e2fa"])
204-
r11 = ConstSpecialization_test_typed_real(_ctx["inst_477fa"])
249+
r8 = ConstSpecialization_test_accumulate_spec(_ctx.inst_13dfa)
250+
r9 = ConstSpecialization_test_accumulate_nonspec(_ctx.inst_201fa, 3.0, 4.0)
251+
r10 = ConstSpecialization_test_typed_int(_ctx.inst_3e2fa)
252+
r11 = ConstSpecialization_test_typed_real(_ctx.inst_477fa)
205253
inputs = initializeArray(0.0, 2)
206254
inputs[0] = 1.0
207255
inputs[1] = 2.0

test/code/DataTypes.py.base

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,53 @@ def float_to_int(i):
1818
def initializeArray(v, size):
1919
return [v for _ in range(size)]
2020

21+
class DataTypes_point:
22+
__slots__ = ['x', 'y']
23+
def __init__(self, x=0.0, y=0.0):
24+
self.x = x
25+
self.y = y
26+
27+
class DataTypes_test2_type:
28+
__slots__ = ['p2', 'result']
29+
def __init__(self, p2=DataTypes_point(), result=DataTypes_point()):
30+
self.p2 = p2
31+
self.result = result
32+
2133
def DataTypes_point_alloc():
22-
_ctx = {}
23-
_ctx["x"] = 0.0
24-
_ctx["y"] = 0.0
34+
_ctx = DataTypes_point()
35+
_ctx.x = 0.0
36+
_ctx.y = 0.0
2537
return _ctx
2638

2739

2840
def DataTypes_test2_type_alloc():
29-
_ctx = {}
30-
_ctx["p2"] = DataTypes_point_alloc()
31-
_ctx["result"] = DataTypes_point_alloc()
41+
_ctx = DataTypes_test2_type()
42+
_ctx.p2 = DataTypes_point_alloc()
43+
_ctx.result = DataTypes_point_alloc()
3244
return _ctx
3345

3446

3547
def DataTypes_addPoint1(p1, p2):
36-
p3 = DataTypes_point_alloc()
37-
p3["x"] = (p1["x"] + p2["x"])
38-
p3["y"] = (p1["y"] + p2["y"])
48+
p3 = DataTypes_point()
49+
p3.x = (p1.x + p2.x)
50+
p3.y = (p1.y + p2.y)
3951
return p3
4052

4153

4254
def DataTypes_addPoint2(p1, p2, result):
43-
result["x"] = (p1["x"] + p2["x"])
44-
result["y"] = (p1["y"] + p2["y"])
55+
result.x = (p1.x + p2.x)
56+
result.y = (p1.y + p2.y)
4557

4658

4759
def DataTypes_test1():
48-
p1 = DataTypes_point_alloc()
49-
p2 = DataTypes_point_alloc()
60+
p1 = DataTypes_point()
61+
p2 = DataTypes_point()
5062
result = DataTypes_addPoint1(p1, p2)
5163
_ = DataTypes_addPoint2(p1, p2, result)
5264

5365

5466
def DataTypes_test2(_ctx):
55-
p1 = DataTypes_point_alloc()
56-
_ctx["result"] = DataTypes_addPoint1(p1, _ctx["p2"])
67+
p1 = DataTypes_point()
68+
_ctx.result = DataTypes_addPoint1(p1, _ctx.p2)
5769

5870

0 commit comments

Comments
 (0)