Skip to content

Conversation

@Apricot-S
Copy link
Collaborator

@Apricot-S Apricot-S commented Oct 20, 2025

#74

The cache in HandDivider has been removed for now.

@Nihisil
Copy link
Contributor

Nihisil commented Nov 4, 2025

sorry, I have missed your PR

The cache in HandDivider has been removed for now.

do we have plans to return it back? the main reason why it was added because we had a lot of calculations for the same hand during the real game loop

it is not clear should cache to be the part of the lib or the part of the code that is using lib, but probably having it in lib helps

@Apricot-S
Copy link
Collaborator Author

Apricot-S commented Nov 4, 2025

@Nihisil
Thank you for the review and your helpful feedback.

I tried introducing @lru_cache, but the arguments are not hashable, so it could not be applied as-is.
If we convert the arguments to tuples inside the function and delegate to an _impl method decorated with @lru_cache, it would work, but in that case we would need to add a custom __eq__() implementation to Meld.
Do you have any ideas on this?
If not, I’m considering reverting only HandDivider.divide_hand() back to an instance method.

@Apricot-S
Copy link
Collaborator Author

As a side note, I'm aware that the current divide approach is not very efficient. For reference, I've collected four more efficient algorithms in a repository here: https://github.com/Apricot-S/block-decomposition. This might be useful background material for future improvements.

@Apricot-S
Copy link
Collaborator Author

I've reverted HandDivider.divide_hand() and HandCalculator.estimate_hand_value() back to instance methods based on the feedback.
Regarding converting them into static methods, I'd like to consider that separately in another PR, since the impact is a bit broader.

For now, I plan to wrap up this PR in its current form.

@Apricot-S
Copy link
Collaborator Author

Apricot-S commented Jan 18, 2026

I have revised the algorithm in HandDivider, and by restructuring it, the method is now compatible with lru_cache.
As a result, HandDivider.divide_hand() has been converted into a staticmethod.

Since HandCalculator internally relies on HandDivider.divide_hand(),
HandCalculator.estimate_hand_value() has also been updated to a staticmethod.

Additionally, because several methods that existed for the previous algorithm were removed,
HandDivider.divide_hand() introduces a breaking change.

Given the scale of these modifications,
I plan to open a separate PR containing these changes after this PR is merged.

The work is currently available in the following branch:
https://github.com/Apricot-S/mahjong/tree/feat/divider

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.

2 participants