Load auth and server modules only when used - #1517
Closed
u9g wants to merge 1 commit into
Closed
Conversation
createClient required the Microsoft auth dependency tree (prismarine-auth,
msal-node, jsonwebtoken) at module load for every consumer, though only
auth: 'microsoft' clients use it, and the package index loaded createServer
(node-rsa and the server plugins) for client-only consumers. Together these
are roughly a third of require('minecraft-protocol') time.
Require the auth modules inside their switch branches and export
createServer/Server through getters, so each loads on first use.
Member
Author
|
Closing: re-measured on an idle machine the win is only ~10ms warm / ~65ms cold-cache — not worth the churn. |
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.
Problem
createClient.jsrequires the Microsoft auth dependency tree (prismarine-auth → @azure/msal-node → jsonwebtoken, plus xboxlive-auth and node-fetch) at module load for every consumer, though onlyauth: 'microsoft'clients ever call into it, andindex.jsloadscreateServer(node-rsa and the server plugins) for client-only consumers.Profiled with a
Module._loadtiming hook: this is ~70ms of the ~275ms firstrequire('minecraft-protocol')on an M-series Mac with a cold-ish file cache (~10ms once the OS file cache is warm — msal-node is one large bundled file, so the win is concentrated where startup is already slowest). It also keeps a few MB of never-used code out of memory for offline bots.Change
createClient: requiremojangAuth/microsoftAuthinside theirswitchbranches, so they load on first authenticated connect.index.js: exportcreateServer/Serverthrough getters, so client-only consumers never load the server tree. Destructuring consumers (const { createServer } = require('minecraft-protocol')) still work — the getter fires at destructure time.No behavior change for any auth mode; the modules load at the same points they were first used before.
Verification
standard clean; packet + cycle tests pass locally; offline-mode client connect exercised against a vanilla server via the mineflayer test suite.