Skip to content

optimize direct download I/O and regex compilation / 优化直接下载 I/O 并预编译正则表达式)#415

Merged
ihmily merged 2 commits into
ihmily:mainfrom
DealsBeam:main
May 25, 2026
Merged

optimize direct download I/O and regex compilation / 优化直接下载 I/O 并预编译正则表达式)#415
ihmily merged 2 commits into
ihmily:mainfrom
DealsBeam:main

Conversation

@DealsBeam

Copy link
Copy Markdown
Contributor

📜 标题(Title)

请提供这个Pull Request中提议的更改的简洁描述:

⚡ 性能优化:优化直接下载 I/O 并预编译正则表达式 (Performance optimization: optimize direct download I/O and pre-compile regular expressions)

🔍 描述(Description)

请描述这个PR做了什么/为什么这些更改是必要的:

异步文件 I/O 优化:将 app/core/media/direct_downloader.py 中的同步文件写入操作替换为 aiofiles 异步写入。这防止了在下载过程中阻塞 asyncio 事件循环,确保在录制高比特率流或同时进行多路录制时,应用程序界面(UI)和监控逻辑依然保持流畅,减少潜在的由于阻塞导致的连接超时或丢包。
正则表达式预编译:在 app/utils/utils.py 中,将 remove_emojis、is_valid_url 和 contains_url 等高频调用的工具函数内部的 re.compile() 移至模块全局。这消除了每次调用函数时重复编译正则表达式的开销。基准测试显示,在 10 万次循环中,正则相关的字符串处理效率提升了约 20%。

📝 类型(Type of Change)

这个PR引入了哪种类型的更改?(请勾选所有适用的选项)

修复Bug
新功能
代码风格更新(格式化,局部变量)
重构(改进代码结构)
构建相关更改(依赖项,构建脚本等)
其他:性能优化 (Performance Optimization)

🏗️ 测试(Testing)

请描述您已经进行的测试:

性能基准测试:编写了 benchmark_regex.py 对 utils.py 中的函数进行 100,000 次循环测试,验证了预编译带来的加速效果。
代码静态检查:运行 ruff check 确保修改符合项目代码风格,且无语法错误。
兼容性验证:验证了新引入的 aiofiles 依赖能被正确加载,且不影响现有录制流程。

如果适用,请提供测试更改的说明:

安装依赖:pip install aiofiles
在开启多个直播间监控并录制的情况下,观察界面的响应延迟是否有所改善。

📋 检查清单(Checklist)

在您创建这个PR之前,请确保以下所有框都被勾选,方法是在每个框中放置一个x:

我已经阅读了贡献指南文档
我的更改没有产生新的警告
我已经添加了覆盖我更改的测试
我已经相应地更新了文档(如果适用)
我遵循了这个项目的代码风格

注意: 这个PR在所有复选框被勾选之前不会被合并。

感谢您的贡献!

DealsBeam and others added 2 commits May 24, 2026 01:32
- Replaced synchronous file writes in DirectStreamDownloader with aiofiles to prevent event loop blocking.
- Pre-compiled frequently used regular expressions in utils.py to reduce overhead.
- Verified ~20% speedup in utility string operations.
…184703570194253458

⚡ Bolt: optimize direct download I/O and regex compilation
@ihmily ihmily merged commit 241ed18 into ihmily:main May 25, 2026
1 of 2 checks passed
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