Skip to content

perf(keyboard): reduce key_gap overhead -- 23.0% less time - #628

Open
dexhunter wants to merge 2 commits into
CloakHQ:mainfrom
dexhunter:perf/keyboard-gap-overhead
Open

dexhunter wants to merge 2 commits into
CloakHQ:mainfrom
dexhunter:perf/keyboard-gap-overhead

Conversation

@dexhunter

@dexhunter dexhunter commented Oct 26, 2026 •

Copy link
Copy Markdown

TL;DR

  • key_gap computes the delay between two simulated key presses; this PR makes that computation cheaper.
  • gap_factor now lowercases each character once and checks the hand rules inline; key_gap uses plain comparisons in place of three max calls.
  • Benchmark: 112.736 ms -> 86.623 ms per batch, 23.0% less time.
  • Results are identical for str inputs: same delays, same random draws.
  • Tested with the native suite: 283 tests passed.

Change

key_gap calls gap_factor for every simulated keystroke. Before, gap_factor lowercased each character twice and called the private helper _hand twice. 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. _hand had no other caller, so this PR removes it together with the unused Optional import.

In key_gap, three conditional expressions replace the three max calls. They keep the comparison direction of max, 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 str inputs. There is one difference for bytearray arguments, which no caller uses. Before, gap_factor() called _hand on both arguments and raised TypeError for a bytearray key. Now it returns 1.0 first when the other argument is empty, non-ASCII or whitespace. A bytearray key next to a plain ASCII key still raises TypeError. Every caller of key_gap and gap_factor in the package passes str.

Measurement

The comparison is against commit 146f46e07d4213e54f2c1440aa20093474823371 on Python 3.12.13. The benchmark follows TestHumanTiming.test_key_gap_depends_on_the_letters: three groups of 3,000 key_gap draws 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.

Run Before (batch median) After (batch median) After / before
1 112.736 ms 86.623 ms 0.769890
2 112.028 ms 86.746 ms 0.776521
3 112.369 ms 86.132 ms 0.767549

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

xvfb-run -a python -m pytest tests/test_update.py tests/test_humanize_unit.py tests/test_license.py tests/test_humanize_engine.py --timeout=90

All 283 tests passed. A separate comparison of gap_factor against the base commit matched on all 364,816 str argument 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

dexhunter and others added 2 commits October 26, 2026 04:47
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>
@dexhunter dexhunter changed the title perf(keyboard): reduce key_gap overhead -- 22.5% less time perf(keyboard): reduce key_gap overhead -- 23.0% less time Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant