Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Optimize pre-calculation map structure #23

Merged
merged 2 commits into from
Dec 20, 2024
Merged

Conversation

Apricot-S
Copy link
Owner

概要

事前計算した部分置換数と有効牌の表の値を圧縮して保存するように変更する。
この変更により表の容量が 1 要素あたり 8 + 8 + 16 + 16 = 48 bit から 32 bit と 2 / 3 に削減される。

また、変更前は置換数の計算の際、部分置換数に配列のタプルと 2 段階でアクセスしていたが、
変更後は配列のみの 1 段階のアクセスになった。
この変更により計算速度が 25 % ~ 29 % 減少している (約 1.3 ~ 1.4 倍高速化している)。

変更前

// map_value[x].0 : replacement number
// map_value[x].1 : necesaary tiles
// [0] : 0 pairs, 0 melds
// [1] : 0 pairs, 1 melds
// [2] : 0 pairs, 2 melds
// [3] : 0 pairs, 3 melds
// [4] : 0 pairs, 4 melds
// [5] : 1 pairs, 0 melds
// [6] : 1 pairs, 1 melds
// [7] : 1 pairs, 2 melds
// [8] : 1 pairs, 3 melds
// [9] : 1 pairs, 4 melds
pub type MapValue = [(u8, u16); 10];
        let mut r = min(lhs[i].0 + rhs[0].0, lhs[0].0 + rhs[i].0);

変更後

// Each element contains the following structure:
//
// [Bits 0-3]: Replacement number without a pair
// [Bits 4-12]: Necessary tiles without a pair
// [Bits 13-15]: Unused
// [Bits 16-19]: Replacement number with a pair
// [Bits 20-28]: Necessary tiles with a pair
// [Bits 29-31]: Unused
//
// Index:
// [0]: 0 melds
// [1]: 1 melds
// [2]: 2 melds
// [3]: 3 melds
// [4]: 4 melds
pub type MapValue = [u32; 5];
        let mut r = min(lhs[i] + rhs[0], lhs[0] + rhs[i]);

@Apricot-S Apricot-S merged commit 84220a6 into develop Dec 20, 2024
@Apricot-S Apricot-S deleted the refactor/pack branch December 20, 2024 16:33
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