Repository navigation
Conversation
Replace key_gap clamps with ordered conditional expressions and reuse lowercase strings in gap_factor for ordinary string inputs. Keep the original string-subclass path and random sampling behavior. The benchmark-derived workload takes 22.5% less time across three paired harness runs. All 283 targeted native tests pass. Co-Authored-By: Aiden <aiden@weco.ai>
Drop the exact-type check and the second copy of the hand logic in gap_factor. Lowercase each character once and test the hand rules inline, so the private _hand helper is no longer needed. Results are identical: a key has a hand only when it is non-empty, ASCII and not whitespace, and the checks run in the same order. A comparison of 375,890 input pairs (Latin and Unicode characters, multi-character strings, non-string values) matches the previous implementation, including the exceptions raised. The key_gap benchmark still takes about 22% less time than main. Co-Authored-By: Aiden <aiden@weco.ai>
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.
TL;DR
key_gapcomputes the delay between two simulated key presses; this PR makes that computation cheaper.gap_factornow lowercases each character once and checks the hand rules inline;key_gapuses plain comparisons in place of threemaxcalls.strinputs: same delays, same random draws.Change
key_gapcallsgap_factorfor every simulated keystroke. Before,gap_factorlowercased each character twice and called the private helper_handtwice. Now it lowercases each character once and does the hand check inline. A key has a hand only when it is non-empty, ASCII and not whitespace. If either key has no hand, the function returns 1.0. Otherwise it compares left-hand set membership of the two keys._handhad no other caller, so this PR removes it together with the unusedOptionalimport.In
key_gap, three conditional expressions replace the threemaxcalls. They keep the comparison direction ofmax, so ties and NaN values give the same result as before. The log-normal sampler, the pause probability and the order of random draws do not change. This PR reduces the computation cost only, not the deliberate delays that simulate typing.Results are identical for
strinputs. There is one difference forbytearrayarguments, which no caller uses. Before,gap_factor()called_handon both arguments and raisedTypeErrorfor abytearraykey. Now it returns 1.0 first when the other argument is empty, non-ASCII or whitespace. Abytearraykey next to a plain ASCII key still raisesTypeError. Every caller ofkey_gapandgap_factorin the package passesstr.Measurement
The comparison is against commit
146f46e07d4213e54f2c1440aa20093474823371on Python 3.12.13. The benchmark followsTestHumanTiming.test_key_gap_depends_on_the_letters: three groups of 3,000key_gapdraws and their medians. It adds 968 calls that cover configuration limits, pauses, whitespace, Unicode and invalid inputs.Each run timed nine before/after pairs on an isolated core, with ten benchmark repetitions per timed batch. Each run also checked that outputs, result types, exception details, random state and configuration state are equal to the base commit.
The headline uses run 1, which has the median ratio of the three runs. These timings describe this benchmark, not end-to-end browser typing speed.
Validation
All 283 tests passed. A separate comparison of
gap_factoragainst the base commit matched on all 364,816strargument pairs, which include Unicode, multi-character and empty strings.AI assistance
This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.
Weco share link: https://dashboard.weco.ai/share/8KeTfrcxvewO8oXmmMgdfF3zlOV3CI7Q