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: image64Helper.tsbase64Helper.tsへリファクタリング #2080

Merged
merged 3 commits into from
May 20, 2024

Conversation

sabonerune
Copy link
Contributor

内容

現在Base64デコードにbufferを使用していますがこのためだけに使うのは無駄が多い気がします。
これをブラウザに実装されているatob()を使用した実装に置き換えます。

その他

BufferのBase64デコード処理は正規表現を使っているっぽいがatob()はブラウザネイティブの処理で若干速度が向上している?

@sabonerune sabonerune requested a review from a team as a code owner May 14, 2024 13:02
@sabonerune sabonerune requested review from Hiroshiba and removed request for a team May 14, 2024 13:02
Copy link
Member

@Hiroshiba Hiroshiba left a comment

Choose a reason for hiding this comment

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

プルリクありです!

実測してみました・・・!
(ちゃんと測定したわけではなく、ChromiumのDevパネルに出るNetworkのスクショです 🙇 )

before
image
image

after
image
image

・・・・・・微妙ですが遅くなってるかもでした!!!
原因はわかりませんが、もしかしたらatobはだいぶ早いけどforでcharCodeAtを実行しまくる部分が時間かかってるかも?

あ、ファイル名をbase64Helper.tsにして関数をまとめるのとても良いと思いました!!
提案としては、URIにする部分は元の実装にしつつ、リファクタリングを適用するのはどうでしょう? 👀

@sabonerune
Copy link
Contributor Author

sabonerune commented May 17, 2024

自分の環境ではごくわずかに速くなるという感じだったのでPC環境で変わる?
自分のPCは古くスペックも低めなので参考にならないと思うのでbufferを使用したコードします。

今回の変更でBufferがなくても正常に動作することが確認できたと思うので依存関係が不明確になりやすいwindowBufferを注入をせずにbase64Helper.tsのみでimportするように変更しました。

vite-plugin-node-polyfillsにまとめてもいいかもしれない

voicevox/vite.config.mts

Lines 72 to 74 in 3f41639

nodePolyfills({
include: ["path"],
}),

@sabonerune sabonerune changed the title perf: Base64のデコードをBuffer.fromからatobへ移行 refactor: image64Helper.tsbase64Helper.tsへリファクタリング May 20, 2024
Copy link
Member

@Hiroshiba Hiroshiba left a comment

Choose a reason for hiding this comment

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

LGTM!!

リファクタリングありがとうございます!!

自分のPCは古くスペックも低めなので参考にならないと思うのでbufferを使用したコードします。

なるほどです、すいません失礼しました!!
スペックが低い PC が早く動いた方が嬉しいかもですね!

ただまあより早いstring結合だけでURLっぽいのを作る方法があるぽいので、そっちの方がいいのかなと思いました!

今回の変更でBufferがなくても正常に動作することが確認できたと思うので依存関係が不明確になりやすいwindowにBufferを注入をせずにbase64Helper.tsのみでimportするように変更しました。

vite-plugin-node-polyfillsにまとめてもいいかもしれない

こんなとこで依存注入してたんですね!!
まとめちゃって良い気もします!

@Hiroshiba Hiroshiba merged commit 47f2e8b into VOICEVOX:main May 20, 2024
9 checks passed
@sabonerune sabonerune deleted the perf/Buffer-to-atob branch May 20, 2024 22:11
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