Skip to content

Commit 7e1e45c

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Improve type annotations in typed richards
Summary: It's not enough to get pyre checking enabled for the full benchmark, but it just makes it a little cleaner. We do enable pyre-strict for the basic Static Python benchmark and pyre-unsafe for the untyped benchmark. Reviewed By: mpage Differential Revision: D96208577 fbshipit-source-id: 5bcfa1c282b012b053ebaa46069d694834a289d8
1 parent 867b80e commit 7e1e45c

3 files changed

Lines changed: 101 additions & 104 deletions

File tree

cinderx/benchmarks/richards.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22

3+
# pyre-unsafe
4+
35
"""
46
based on a Java version:
57
Based on original version written in BCPL by Dr Martin Richards
@@ -31,10 +33,8 @@
3133

3234
BUFSIZE = 4
3335

34-
BUFSIZE_RANGE = range(BUFSIZE)
35-
3636

37-
class Packet(object):
37+
class Packet:
3838
def __init__(self, l, i, k):
3939
self.link = l
4040
self.ident = i
@@ -46,20 +46,19 @@ def append_to(self, lst):
4646
self.link = None
4747
if lst is None:
4848
return self
49-
else:
50-
p = lst
49+
p = lst
50+
next = p.link
51+
while next is not None:
52+
p = next
5153
next = p.link
52-
while next is not None:
53-
p = next
54-
next = p.link
55-
p.link = self
56-
return lst
54+
p.link = self
55+
return lst
5756

5857

5958
# Task Records
6059

6160

62-
class TaskRec(object):
61+
class TaskRec:
6362
pass
6463

6564

@@ -80,12 +79,12 @@ def __init__(self):
8079
self.device_in = None
8180

8281
def workInAdd(self, p):
83-
self.work_in = p.append_to(self.work_in)
84-
return self.work_in
82+
self.work_in = work_in = p.append_to(self.work_in)
83+
return work_in
8584

8685
def deviceInAdd(self, p):
87-
self.device_in = p.append_to(self.device_in)
88-
return self.device_in
86+
self.device_in = device_in = p.append_to(self.device_in)
87+
return device_in
8988

9089

9190
class WorkerTaskRec(TaskRec):
@@ -97,7 +96,7 @@ def __init__(self):
9796
# Task
9897

9998

100-
class TaskState(object):
99+
class TaskState:
101100
def __init__(self):
102101
self.packet_pending = True
103102
self.task_waiting = False
@@ -159,7 +158,7 @@ def trace(a):
159158
TASKTABSIZE = 10
160159

161160

162-
class TaskWorkArea(object):
161+
class TaskWorkArea:
163162
def __init__(self):
164163
self.taskTab = [None] * TASKTABSIZE
165164

@@ -169,9 +168,6 @@ def __init__(self):
169168
self.qpktCount = 0
170169

171170

172-
taskWorkArea = TaskWorkArea()
173-
174-
175171
class Task(TaskState):
176172
def __init__(self, i, p, w, initialState, r):
177173
self.link = taskWorkArea.taskList
@@ -245,6 +241,9 @@ def findtcb(self, id):
245241
return t
246242

247243

244+
taskWorkArea = TaskWorkArea()
245+
246+
248247
# DeviceTask
249248

250249

@@ -353,7 +352,7 @@ def fn(self, pkt, r):
353352
return self.qpkt(pkt)
354353

355354

356-
def schedule():
355+
def schedule() -> None:
357356
t = taskWorkArea.taskList
358357
while t is not None:
359358
if tracing:
@@ -367,8 +366,8 @@ def schedule():
367366
t = t.runTask()
368367

369368

370-
class Richards(object):
371-
def run(self, iterations):
369+
class Richards:
370+
def run(self, iterations: int) -> bool:
372371
for i in range(iterations):
373372
taskWorkArea.holdCount = 0
374373
taskWorkArea.qpktCount = 0

cinderx/benchmarks/richards_static_basic_lib.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
# pyre-strict
4+
25
"""
36
based on a Java version:
47
Based on original version written in BCPL by Dr Martin Richards
@@ -16,7 +19,7 @@
1619
from __static__ import cast
1720

1821
import sys
19-
from typing import Final, List
22+
from typing import Final
2023

2124
# Task IDs
2225
I_IDLE: Final[int] = 1
@@ -34,35 +37,32 @@
3437

3538
BUFSIZE: Final[int] = 4
3639

37-
BUFSIZE_RANGE: List[int] = list(range(BUFSIZE))
38-
3940

40-
class Packet(object):
41+
class Packet:
4142
def __init__(self, l: Packet | None, i: int, k: int) -> None:
4243
self.link: Packet | None = l
4344
self.ident: int = i
4445
self.kind: int = k
4546
self.datum: int = 0
46-
self.data: List[int] = [0] * BUFSIZE
47+
self.data: list[int] = [0] * BUFSIZE
4748

4849
def append_to(self, lst: Packet | None) -> Packet:
4950
self.link = None
5051
if lst is None:
5152
return self
52-
else:
53-
p = lst
53+
p = lst
54+
next = p.link
55+
while next is not None:
56+
p = next
5457
next = p.link
55-
while next is not None:
56-
p = next
57-
next = p.link
58-
p.link = self
59-
return lst
58+
p.link = self
59+
return lst
6060

6161

6262
# Task Records
6363

6464

65-
class TaskRec(object):
65+
class TaskRec:
6666
pass
6767

6868

@@ -82,13 +82,13 @@ def __init__(self) -> None:
8282
self.work_in: Packet | None = None
8383
self.device_in: Packet | None = None
8484

85-
def workInAdd(self, p: Packet) -> Packet | None:
86-
self.work_in = p.append_to(self.work_in)
87-
return self.work_in
85+
def workInAdd(self, p: Packet) -> Packet:
86+
self.work_in = work_in = p.append_to(self.work_in)
87+
return work_in
8888

89-
def deviceInAdd(self, p: Packet) -> Packet | None:
90-
self.device_in = p.append_to(self.device_in)
91-
return self.device_in
89+
def deviceInAdd(self, p: Packet) -> Packet:
90+
self.device_in = device_in = p.append_to(self.device_in)
91+
return device_in
9292

9393

9494
class WorkerTaskRec(TaskRec):
@@ -100,7 +100,7 @@ def __init__(self) -> None:
100100
# Task
101101

102102

103-
class TaskState(object):
103+
class TaskState:
104104
def __init__(self) -> None:
105105
self.packet_pending: bool = True
106106
self.task_waiting: bool = False
@@ -150,7 +150,7 @@ def isWaitingWithPacket(self) -> bool:
150150
layout: int = 0
151151

152152

153-
def trace(a):
153+
def trace(a: object) -> None:
154154
global layout
155155
layout -= 1
156156
if layout <= 0:
@@ -162,20 +162,17 @@ def trace(a):
162162
TASKTABSIZE: Final[int] = 10
163163

164164

165-
class TaskWorkArea(object):
165+
class TaskWorkArea:
166166
def __init__(self) -> None:
167-
# pyre-ignore[8]: Pyre confused on list[Task | None] vs list[None].
168-
self.taskTab: List[Task | None] = [None] * TASKTABSIZE
167+
none_task = cast(Task | None, None)
168+
self.taskTab: list[Task | None] = [none_task] * TASKTABSIZE
169169

170170
self.taskList: Task | None = None
171171

172172
self.holdCount: int = 0
173173
self.qpktCount: int = 0
174174

175175

176-
taskWorkArea: Final[TaskWorkArea] = TaskWorkArea()
177-
178-
179176
class Task(TaskState):
180177
def __init__(
181178
self, i: int, p: int, w: Packet | None, initialState: TaskState, r: TaskRec
@@ -251,6 +248,9 @@ def findtcb(self, id: int) -> Task:
251248
return t
252249

253250

251+
taskWorkArea: Final[TaskWorkArea] = TaskWorkArea()
252+
253+
254254
# DeviceTask
255255

256256

@@ -379,7 +379,7 @@ def schedule() -> None:
379379
t = t.runTask()
380380

381381

382-
class Richards(object):
382+
class Richards:
383383
def run(self, iterations: int) -> bool:
384384
for i in range(iterations):
385385
taskWorkArea.holdCount = 0

0 commit comments

Comments
 (0)