Repository navigation
Import game controller code on first use - #2964
Merged
Merged
Conversation
pyglet lists the input devices when pyglet.input is imported, which took about 1.4 of the 2.7 seconds of `import arcade` on Windows. Arcade's controller functions are now imported on first use through a module __getattr__, and the modules that only used pyglet's Controller for type hints import it for type checkers only. Sound still loads at import so the first sound doesn't stutter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1159. Related to #2402: this PR covers import time only, not which names should be top-level.
Why
import arcadewas slowMeasured with
python -X importtimeon Windows:pyglet.input(viaarcade.joysticks)pyglet.media(viaarcade.sound)Change
get_controllers,ControllerManager,get_joysticks,get_game_controllers, and thearcade.controller/arcade.joysticksmodules are loaded on first use, through a module__getattr__.TYPE_CHECKINGimports.from arcade import ...andfrom arcade import *work as before.gamecontrollerdb.txt) moved fromarcade/__init__.pytoarcade/controller.py.arcade/input/manager.py,arcade/gui/ui_manager.pyandarcade/experimental/controller_window.pyonly used pyglet'sControllerfor type hints. They now import it for type checkers only.ControllerWindowimportsControllerManagerwhen it's created.input_manager_exampleusesarcade.ControllerManager()instead of pyglet's, so it keeps Arcade's mappings.Sound is unchanged. It still loads at import. Making it lazy would cut another 0.5 s, but then the first
load_soundcould make the game stutter if it happens mid-game. A pause when connecting controllers is fine, because that normally happens at startup.Results (Windows)
import arcadeload_sound/play()get_controllers()Behavior change
Code that calls
pyglet.input.get_controllers()directly, without using Arcade's controller functions first, now gets pyglet's 302 built-in mappings instead of Arcade's 2147. The changelog says so.Testing
InputManagerexamples, and the GUI controller examples (ControllerWindow), run without errors.🤖 Generated with Claude Code