Skip to content

Commit

Permalink
Speedups: auto-build on import and build during setup
Browse files Browse the repository at this point in the history
  • Loading branch information
black-sliver committed Jul 2, 2023
1 parent c8f6831 commit e8b0732
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
17 changes: 14 additions & 3 deletions NetUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import typing
import enum
import warnings
from json import JSONEncoder, JSONDecoder

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

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

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

def get_remaining(self, state: typing.Dict[typing.Tuple[int, int], typing.Set[int]], team: int, slot: int):
def get_remaining(self, state: typing.Dict[typing.Tuple[int, int], typing.Set[int]], team: int, slot: int
) -> typing.List[int]:
checked = state[team, slot]
player_locations = self[slot]
return sorted([player_locations[location_id][0] for
Expand All @@ -391,7 +395,14 @@ def get_remaining(self, state: typing.Dict[typing.Tuple[int, int], typing.Set[in
if typing.TYPE_CHECKING: # type-check with pure python implementation until we have a typing stub
LocationStore = _LocationStore
else:
try:
import pyximport
pyximport.install()
except ImportError:
pyximport = None
try:
from _speedups import LocationStore
except ImportError:
warnings.warn("_speedups not available. Falling back to pure python LocationStore. "
"Install a matching C++ compiler for your platform to compile _speedups.")
LocationStore = _LocationStore
4 changes: 2 additions & 2 deletions _speedups.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# cython: language_level=3
# distutils: language = c++
#cython: language_level=3
#distutils: language = c++

"""
Provides faster implementation of some core parts.
Expand Down
12 changes: 9 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

from worlds.LauncherComponents import components, icon_paths
from Utils import version_tuple, is_windows, is_linux
from Cython.Build import cythonize


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

# pre build steps
# pre-build steps
print(f"Outputting to: {self.buildfolder}")
os.makedirs(self.buildfolder, exist_ok=True)
import ModuleUpdate
ModuleUpdate.requirements_files.add(os.path.join("WebHostLib", "requirements.txt"))
ModuleUpdate.update(yes=self.yes)

# auto-build cython modules
build_ext = self.distribution.get_command_obj("build_ext")
build_ext.inplace = True
self.run_command("build_ext")

# regular cx build
self.buildtime = datetime.datetime.utcnow()
super().run()
Expand Down Expand Up @@ -586,10 +592,10 @@ def find_lib(lib, arch, libc):
version=f"{version_tuple.major}.{version_tuple.minor}.{version_tuple.build}",
description="Archipelago",
executables=exes,
ext_modules=[], # required to disable auto-discovery with setuptools>=61
ext_modules=cythonize("_speedups.pyx"),
options={
"build_exe": {
"packages": ["websockets", "worlds", "kivy"],
"packages": ["worlds", "kivy", "_speedups", "cymem"],
"includes": [],
"excludes": ["numpy", "Cython", "PySide2", "PIL",
"pandas"],
Expand Down

0 comments on commit e8b0732

Please sign in to comment.