Perfect Local Mirror 本地编译缓存加速器
We received many complaints about how slow Swift Package Manager version 4 was, so here is an example of cheating sheet - to accelerate your Perfect App by caching all heavy dependencies.
我们收到大量关于Swift 4 SPM 软件包管理器速度过慢的投诉,因此本项目用于Perfect编译加速
Quick Start
Install
Open a terminal then execute the following script.
打开终端执行下列命令:
git clone https://github.com/PerfectlySoft/Perfect-LocalMirror.git && \
./Perfect-LocalMirror/install.sh
sudo privilege to perform the installation.
您可能需要管理员权限以执行上述命令。
Perfect Assistant Support
There is also a script to patch Perfect Assistant to use these mirrors:
本程序同样包括了用于软件助手的补丁。
./Perfect-LocalMirror/updatePA.sh
You can revert the patch by another script:
您可以随时卸载这个补丁:
./Perfect-LocalMirror/resetPA.sh
Usage
You will find a localized version of PerfectTemplate, the starter web server, under the folder of /private/var/perfect/PerfectTemplate:
您会发现一个本地版本的模板服务器,结构已经发生了显著变化:
import PackageDescription
let package = Package(
name: "PerfectTemplate",
targets: [],
dependencies: [
.Package(url: "/private/var/perfect/Perfect-HTTPServer", majorVersion: 3),
]
)Then the server building speed will be completely different now.
现在服务器编译速度肯定大不相同了,请尽情尝试!
Local Cache Path
Now these components are available on local repo /private/var/perfect as the above fashion:
目前可以使用上述路径的本地模块包括:
(a full example can be found on test.sh script)
(完整的调用样例可以参考 test.sh 脚本)
| PerfectLib | PerfectHTTP | PerfectHTTPServer | PerfectThread |
|---|---|---|---|
| PerfectLogger | PerfectRequestLogger | PerfectNet | LinuxBridge |
| PerfectXML | PerfectCrypto | PerfectCURL | PerfectSMTP |
| PerfectMustache | PerfectPostgreSQL | PerfectSQLite | PerfectMySQL |
| PerfectRedis | PerfectMongoDB | PerfectRepeater | PerfectNotifications |
| PerfectCouchDB | PerfectFileMaker | PerfectHadoop | PerfectWebSockets |
| PerfectWebRedirects | PerfectPython | PerfectMarkdown | PerfectLDAP |
| PerfectKafka | PerfectMosquitto | PerfectSession | PerfectSessionMySQL |
| PerfectSessionPostgreSQL | PerfectSessionRedis | PerfectSessionMongoDB | PerfectSessionSQLite |
| PerfectSessionCouchDB | PerfectTurnstileSQLite | PerfectTurnstileMySQL | PerfectTurnstilePostgreSQL |
| PerfectTurnstileMongoDB | PerfectTurnstileCouchDB | PerfectLocalAuthentication | PerfectZip |
| OAuth2 | MariaDB | SwiftMoment | SwiftRandom |
| SwiftString | JSONConfig | StORM | SQLiteStORM |
| CouchDBStORM | PostgresStORM | MySQLStORM | MongoDBStORM |
| PerfectLocalAuthentication |
** NOTE ** If any Server Side Swift component has little dependencies and free of incompressible large files such as binaries in git history, then it could be unnecessary to cache it as the above.
Magic Behind
The actual bottleneck is Swift Package Manager version 4 is using a --mirror flag of git clone, which has a good reason but may cause 8x+ slower than Swift 3 in a certain situation.
我们发现瓶颈存在于上述链接,即 2017年2月27日 之后在git clone 命令中增加了--mirror标志。这个想法是好的,但是极端情况下会导致编译速度慢八倍。
The solution is simple: build a local cache of each repo recursively and truncate unnecessary backlog history resulted by --mirror prior to swift package resolve.
解决思路其实很简单,就是在SPM解析之前,首先把经过截取不必要版本历史的简化镜像置于本地硬盘。
Only three steps it actually takes.
实际这个过程就三步:
mirror_ex RepoName VenderNamewhich will grab the latest version from github. 首先是从 github 上取得目标最新版本。- Patch Package.swift to allow local mirrors. 然后修改本地下载的Package.swift 文件以应用本地镜像。
git tagthe latest online version to the offline updates. 在修改基础上增加一个和在线版本一致的离线版本。
Take OAuth2 for example, the installation will patch all dependencies to offline versions. By default, the installation path is HUB=/private/var/perfect.
以 OAuth2 为例,安装脚本会将所有依存关系指向本地离线版本,默认安装路径是 HUB=/private/var/perfect:
mirror Perfect-OAuth2
tee Package.swift << EOF >> /dev/null
import PackageDescription
let package = Package(name: "OAuth2", targets: [],
dependencies: [
.Package(url: "$HUB/Perfect-Session", majorVersion: 3)
])
EOF
reversionYou can add as many components as possible to the script. Have fun!
您可以尽情按照上述风格自行向脚本追加需要的 Swift 组件,慢用!
Further Information
For more information on the Perfect project, please visit perfect.org.
