Skip to content

refactor: improve performance for hand calculations further#192

Merged
Apricot-S merged 1 commit intomasterfrom
optimize-hand-calculations
Feb 7, 2026
Merged

refactor: improve performance for hand calculations further#192
Apricot-S merged 1 commit intomasterfrom
optimize-hand-calculations

Conversation

@Nihisil
Copy link
Contributor

@Nihisil Nihisil commented Feb 7, 2026

Closes #171

@Apricot-S fixed hand divider, so now it produces empty output if hand is not agari.

With that improvement we can simplify hand calculation and remove additional is_agari check from there.

All 2.1kk validation set hands pass with this change.

Benchmark results

Benchmark was run with n=5 and limit=200000.

Old

  • Throughput: 31165 hands/sec (based on median)
  • Avg per hand: 0.032ms (based on median)

New

  • Throughput: 37634 hands/sec (based on median)
  • Avg per hand: 0.027ms (based on median)

Overall profile

Metric Old New Change
Total profile time 13.870s 12.384s -10.7%
Total profile calls 56,573,351 54,714,452 -3.3%

estimate_hand_value function

Metric Old New Change
Own time 2.404s 2.274s -5.4%
Cumulative time 13.828s 12.347s -10.7%
Callee calls 10,221,010 10,017,929 -2.0%
Callee tottime 4.484s 3.847s -14.2%
Callee cumtime 11.424s 10.073s -11.8%

@Nihisil Nihisil added this to the v2.0.0 milestone Feb 7, 2026
@Nihisil Nihisil requested a review from Apricot-S February 7, 2026 00:52

if not calculated_hands:
return HandResponse(error=HandCalculator.ERR_HAND_NOT_CORRECT)
return HandResponse(error=HandCalculator.ERR_HAND_NOT_WINNING)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No breaking changes are required if we do the following:

hand_options = HandDivider.divide_hand(tiles_34, melds)
is_kokushi = not is_open_hand and config.yaku.kokushi.is_condition_met(None, tiles_34)

if not hand_options and not is_kokushi:
    return HandResponse(error=HandCalculator.ERR_HAND_NOT_WINNING)

...

if not calculated_hands:
    return HandResponse(error=HandCalculator.ERR_HAND_NOT_CORRECT)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it is worth to keep comparability here? It was really rare case and hack with additional error response. Now we have a chance to fix it properly

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood.

@Nihisil Nihisil requested a review from Apricot-S February 7, 2026 09:11
@Apricot-S Apricot-S merged commit 210d13d into master Feb 7, 2026
8 checks passed
@Apricot-S Apricot-S deleted the optimize-hand-calculations branch February 7, 2026 09:16
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.

[Feature proposal] Simplify hand calculation checks

2 participants