Pre-compute spatial graph statistics during Data initialization
#65
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.
Context
The spatial entropy calculation uses graph statistics (average degree and starting positions) to compute password strength for keyboard patterns. Previously, these statistics were calculated on-demand for every spatial match by iterating through the adjacency graph data, performing map/compact/inject operations each time.
Since adjacency graphs are immutable after loading, these statistics can be computed once during initialisation and reused, eliminating redundant calculations.
Changes
Data class (
lib/zxcvbn/data.rb):compute_graph_statsprivate method that pre-computes average degree and starting positions for all adjacency graphs during initialisation@graph_statshash with structure:{ graph_name => { average_degree:, starting_positions: } }graph_statsreaderMath module (
lib/zxcvbn/math.rb):average_degree_for_graphto retrieve pre-computed value fromdata.graph_statsstarting_positions_for_graphto retrieve pre-computed value fromdata.graph_statsPerformance
Benchmark results (1000 iterations across 10 passwords with spatial patterns):
The improvement comes from eliminating repeated iterations over adjacency graph data during entropy calculations. All 262 tests pass, confirming correctness is maintained.