Skip to content

Commit dc0aa79

Browse files
committed
Speedups: auto-build on import and build during setup
1 parent c8f6831 commit dc0aa79

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

NetUtils.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import typing
44
import enum
5+
import warnings
56
from json import JSONEncoder, JSONDecoder
67

78
import websockets
@@ -362,7 +363,8 @@ def get_for_player(self, slot: int) -> typing.Dict[int, typing.Set[int]]:
362363
all_locations[source_slot].add(location_id)
363364
return all_locations
364365

365-
def get_checked(self, state: typing.Dict[typing.Tuple[int, int], typing.Set[int]], team: int, slot: int):
366+
def get_checked(self, state: typing.Dict[typing.Tuple[int, int], typing.Set[int]], team: int, slot: int
367+
) -> typing.List[int]:
366368
checked = state[team, slot]
367369
if not checked:
368370
# This optimizes the case where everyone connects to a fresh game at the same time.
@@ -371,7 +373,8 @@ def get_checked(self, state: typing.Dict[typing.Tuple[int, int], typing.Set[int]
371373
location_id in self[slot] if
372374
location_id in checked]
373375

374-
def get_missing(self, state: typing.Dict[typing.Tuple[int, int], typing.Set[int]], team: int, slot: int):
376+
def get_missing(self, state: typing.Dict[typing.Tuple[int, int], typing.Set[int]], team: int, slot: int
377+
) -> typing.List[int]:
375378
checked = state[team, slot]
376379
if not checked:
377380
# This optimizes the case where everyone connects to a fresh game at the same time.
@@ -380,7 +383,8 @@ def get_missing(self, state: typing.Dict[typing.Tuple[int, int], typing.Set[int]
380383
location_id in self[slot] if
381384
location_id not in checked]
382385

383-
def get_remaining(self, state: typing.Dict[typing.Tuple[int, int], typing.Set[int]], team: int, slot: int):
386+
def get_remaining(self, state: typing.Dict[typing.Tuple[int, int], typing.Set[int]], team: int, slot: int
387+
) -> typing.List[int]:
384388
checked = state[team, slot]
385389
player_locations = self[slot]
386390
return sorted([player_locations[location_id][0] for
@@ -391,7 +395,14 @@ def get_remaining(self, state: typing.Dict[typing.Tuple[int, int], typing.Set[in
391395
if typing.TYPE_CHECKING: # type-check with pure python implementation until we have a typing stub
392396
LocationStore = _LocationStore
393397
else:
398+
try:
399+
import pyximport
400+
pyximport.install()
401+
except ImportError:
402+
pyximport = None
394403
try:
395404
from _speedups import LocationStore
396405
except ImportError:
406+
warnings.warn("_speedups not available. Falling back to pure python LocationStore. "
407+
"Install a matching C++ compiler for your platform to compile _speedups.")
397408
LocationStore = _LocationStore

_speedups.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# cython: language_level=3
2-
# distutils: language = c++
1+
#cython: language_level=3
2+
#distutils: language = c++
33

44
"""
55
Provides faster implementation of some core parts.

setup.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
from worlds.LauncherComponents import components, icon_paths
5959
from Utils import version_tuple, is_windows, is_linux
60+
from Cython.Build import cythonize
6061

6162

6263
# On Python < 3.10 LogicMixin is not currently supported.
@@ -293,13 +294,18 @@ def run(self):
293294
sni_thread = threading.Thread(target=download_SNI, name="SNI Downloader")
294295
sni_thread.start()
295296

296-
# pre build steps
297+
# pre-build steps
297298
print(f"Outputting to: {self.buildfolder}")
298299
os.makedirs(self.buildfolder, exist_ok=True)
299300
import ModuleUpdate
300301
ModuleUpdate.requirements_files.add(os.path.join("WebHostLib", "requirements.txt"))
301302
ModuleUpdate.update(yes=self.yes)
302303

304+
# auto-build cython modules
305+
build_ext = self.distribution.get_command_obj("build_ext")
306+
build_ext.inplace = True
307+
self.run_command("build_ext")
308+
303309
# regular cx build
304310
self.buildtime = datetime.datetime.utcnow()
305311
super().run()
@@ -586,10 +592,10 @@ def find_lib(lib, arch, libc):
586592
version=f"{version_tuple.major}.{version_tuple.minor}.{version_tuple.build}",
587593
description="Archipelago",
588594
executables=exes,
589-
ext_modules=[], # required to disable auto-discovery with setuptools>=61
595+
ext_modules=cythonize("_speedups.pyx"),
590596
options={
591597
"build_exe": {
592-
"packages": ["websockets", "worlds", "kivy"],
598+
"packages": ["websockets", "worlds", "kivy", "_speedups", "cymem"],
593599
"includes": [],
594600
"excludes": ["numpy", "Cython", "PySide2", "PIL",
595601
"pandas"],

0 commit comments

Comments
 (0)