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

Speed up ExpressionKey's comparison. #2129

Merged
merged 4 commits into from
Aug 15, 2023
Merged

Conversation

Santarh
Copy link
Contributor

@Santarh Santarh commented Aug 15, 2023

毎フレーム呼び出される Vrm10RuntimeExpression 中の処理で ExpressionKey の Dictionary Key としての比較処理がそこそこ負荷が高い。

これを public な挙動を変更せずに改善する。

13 % くらいは高速化した、かも。

@Santarh Santarh changed the title [WIP] Speed up ExpressionKey's comparison. Speed up ExpressionKey's comparison. Aug 15, 2023
@Santarh Santarh requested a review from notargs August 15, 2023 07:58
@@ -145,19 +146,24 @@ public static ExpressionKey CreateFromPreset(ExpressionPreset preset)

public override string ToString()
{
return _id.Replace(UnknownPresetPrefix, "");
return Name;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The behaviour has not changed.


if (Preset != other.Preset) return false;
if (Preset != ExpressionPreset.custom) return true;
return Name == other.Name;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fast comparison by early pruning via _hashCode
and
Accurate equality comparison when _hashCodes are collided.

m_clipMap = expressions.Clips.ToDictionary(
x => expressions.CreateKey(x.Clip),
x => x.Clip,
ExpressionKey.Comparer
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pass IEqualityComparer<ExpressionKey>

// {
// return this.Equals(CreateFromClip(clip));
// }

public int CompareTo(ExpressionKey other)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

実は CompareTo バグってね?と気づいたけど、いったんここでは破壊的変更はしないということでスルー

@@ -192,5 +193,20 @@ public SubAssetKey SubAssetKey
return new SubAssetKey(typeof(VRM10Expression), this.ToString());
}
}

public static IEqualityComparer<ExpressionKey> Comparer { get; } = new EqualityComparer();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

IEqualityComparer<ExpressionKey> はユーザにも公開されていると嬉しいので public で公開する

Copy link
Contributor

@ousttrue ousttrue left a comment

Choose a reason for hiding this comment

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

👍


namespace UniVRM10.Test
{
public sealed class ExpressionKeyTests
Copy link
Contributor Author

Choose a reason for hiding this comment

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

無かったのでテスト追加

@Santarh Santarh merged commit bbb4b5c into vrm-c:master Aug 15, 2023
1 check passed
@Santarh Santarh deleted the fastExpressionKey branch August 15, 2023 08:35
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.

None yet

2 participants