Skip to content

RustのブロッキングAPIを実装 - #702

Merged
Hiroshiba merged 4 commits into
VOICEVOX:mainfrom
qryxip:add-rust-blocking-api
Dec 4, 2023
Merged

Hiroshiba merged 4 commits into
VOICEVOX:mainfrom
qryxip:add-rust-blocking-api

Conversation

@qryxip

@qryxip qryxip commented Dec 2, 2023 •

Copy link
Copy Markdown
Member

内容

Rust APIにブロッキング版を用意し、Synthesizerなどを

  • voicevox_core::blocking::{Synthesizer, …}
  • voicevox_core::tokio::{Synthesizer, …}

に分けて公開するようにします。

関連 Issue

#662

その他

以下の2つはfuture workとします。

  • C APIとJava APIをブロッキング版に依存させる
  • Python APIでもこのPRのようにブロッキング版を用意し、voicevox_core.blockingとvoicevox_core.asyncioに分ける

@qryxip qryxip Dec 2, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ほぼ再実装。ブロッキング版はasync_zipの代わりに、zipをrayonで振り回す。

async版がブロッキング版に依存しているわけではないので、ブロッキング版は現状一切テストされていない状態。例えば"manifest.json"とかをtypoしててもCIには通るはず。

(追記) 言ったそばから間違えてた
d166174

Comment thread crates/voicevox_core/src/voice_model.rs Resolved

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ブロッキング版に切り替えるのは後で。
(Java APIも同様)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

後でこのPRと同じようにvoicevox_core.asyncioとvoicevox_core.blockingを作る。

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

設計としてはtokioをfeature-gateできるようにしてある。ブロッキング版はasync版に依存しない。

@Hiroshiba Hiroshiba left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

tokioをディレクトリ名にすることに関して @qryxip さんと少し議論しました!
コアの依存先がtokioではなくなった時、このRust APIの利用者にとってはAPI名が変わることになると思います。
API名が変わるのは一般的に結構強い破壊的変更だと思うけど、依存先をtokioから変更することは、API名称変えるくらい強い破壊的変更だととらえて良さそうか考えていたのですが、非同期ランタイムを変更するのは強い変更なのでそうすべきだという結論になりまた。
以下放送内コメントの引用です。

非同期ランタイムの変更となったら、それはもう「移住」みたいな感じかと
例えば、WAFは非同期ランタイムに紐付いているかと (例えばaxumは github.com/tokio-rs/axum として開発されている)

Comment on lines +104 to +106
pub fn with_words<R>(&self, f: impl FnOnce(&IndexMap<Uuid, UserDictWord>) -> R) -> R {
self.0.with_words(f)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ふと気になったのですが、impl FnOnce(&IndexMap<Uuid, UserDictWord>などは型推論してくれない感じでしょうか?
わりとコピペコードになるので書かなくていいのからもとかちょっと思いました。

こんな感じとかできないですかね。。

Suggested change
pub fn with_words<R>(&self, f: impl FnOnce(&IndexMap<Uuid, UserDictWord>) -> R) -> R {
self.0.with_words(f)
}
pub fn with_words<R>(&self, f) {
self.0.with_words<R>(f)
}

みたいな・・・。

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

一言で言うと無理ですね。
ScalaやHaskellならできそうですが、それらの言語でも完全に明記することの推奨はされてたと思います。

Comment on lines +81 to +89
pub async fn use_user_dict(
&self,
user_dict: &crate::tokio::UserDict,
) -> crate::result::Result<()> {
let inner = self.0 .0.clone();
let words = user_dict.to_mecab_format();
crate::task::asyncify(move || inner.use_user_dict(&words)).await
}
}

@Hiroshiba Hiroshiba Dec 3, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

この辺りblockingバージョンとtokioバージョンが入り組んでいて難しいなと感じました!
ちょっとよくわかってないのですが、innerを崩せたりする・・・・・・・?
(このプルリクエストで変えてほしいというよりは、後々にそうできればいいのかな、というニュアンスです!)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

UserDictの中身のHashMapをArcを包めば崩せそうではあると思います。
ただ少々影響範囲があるので、できるだけ変更範囲を狭めたいという考えからこうしています。

Comment thread crates/voicevox_core/src/engine/mod.rs Resolved

@Hiroshiba Hiroshiba left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!!!!!!!

コメントを追加してほしい点と、あと名称を変えて欲しかった点が変更されているので良さそうという判断です!
色々教えてくださってありがとうございました!!

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