nuke-optimize
它能做什么
这项技能找出使用 Nuke 库的应用中图片加载缓慢或占用大量内存的原因。适合希望图片界面更流畅的 iPhone 和 Mac 开发者。
适用场景
- ✓当你应用中的图片加载缓慢,或缓存频繁失效的时候。
- ✓当某个显示大量照片的页面滚动时出现卡顿的时候。
- ✓当你的应用因为图片而内存占用不断增长的时候。
技能链接
https://github.com/kean/Nuke/blob/main/skills/nuke-optimize/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我们的照片列表滚动时卡顿,还占用太多内存,请找出原因并修复 Nuke 的设置。”
每周 5 个新技能,1 分钟读完。
工作原理
它开启 Nuke 的诊断日志,阅读每个图片任务的一行记录,查看时间花在哪里,然后每次只改一处,并重新测量。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Optimizing Nuke
Nuke's defaults come from benchmarks, so the gains are almost always in how the app asks for images, and rarely in its configuration. A 12 MP photo decoded for a 120-point cell costs 46 MB, and no setting makes up for that.
Measure first, change one thing, and measure again. Each change here has a cost, and some make things worse when the cause is something else. If you can't run the app, say that what you changed comes from reading the code, and that it's unmeasured. For the API in general, see llms-full.txt at the root of the package.
Measure
Launch with NUKE_DIAGNOSTICS_LOG=1, in the scheme or like this, and read one line per task:
SIMCTL_CHILD_NUKE_DIAGNOSTICS_LOG=1 xcrun simctl launch booted <bundle-id>
xcrun simctl spawn booted log stream --predicate 'subsystem == "com.github.kean.Nuke.ImagePipeline"'
ImageTask #1 · success · 78.5 ms · from network · https://example.com/a.png · 1200×800 · png · 3.7 MB in memory · 70 KB
Go through the slow screen twice: once on a fresh install, and again after relaunching.
| In the log | It means | Go to |
|---|---|---|
from network after a relaunch |
The disk cache missed | Cache keys, Disk cache |
from httpCache, and tens of ms |
URLCache asked the server whether the image changed |
Disk cache |
MB in memory over width × height × 4 of the cell, in pixels |
The image is bigger than what's displayed | Decode less |
The same URL twice, with no coalesced |
Two requests that differ, so nothing is shared | Cache keys, Prefetching |
| More lines than cells on screen, on open | Everything loads at once | Load only what's on screen |
A long list of cancelled |
Normal while scrolling fast: the cells cancel the loads they scrolled past | Nothing |
For where the time of a slow task went:
let pipeline = ImagePipeline { $0.isDiagnosticsEnabled = true }
let task = pipeline.imageTask(with: url)
_ = try await task.image
print(task.metrics!.formatted([.header, .breakdown]))
time: network 93.6 ms (66%) · process 42.5 ms (30%) · cache 0.5 ms · decode 0.3 ms · queue 0.1 ms · other 3.8 ms (3%)
| Largest share | Cause |
|---|---|
network |
The file is too big, it wasn't cached, or the app opened a new connection |
process, decode, decompress |
The image is bigger than it needs to be |
queue |
The task waited for a slot. Prefetching, or too many images at once |
isDiagnosticsEnabled costs a few microseconds a task, and it sends signposts, so the same stages show up in Instruments under os_signpost. For a task that hangs, po ImagePipeline.shared.diagnostics in the debugger prints the queues and the memory cache.
Read the code
# More than one pipeline, or one created in a view
grep -rnE 'ImagePipeline(\(| \{)' --include='*.swift' .
# Every image load: does each one know the size it's displayed at?
grep -rnE 'LazyImage\(|ImageLoader\.load|FetchImage\(|\.image\(for:|imageTask\(with:' --include='*.swift' .
# Disk I/O that blocks the caller, which is a problem on the main thread
grep -rnE 'cachedData\(|cachedImage\(|containsCachedImage|containsData\(|storeCachedImage' --include='*.swift' .
# Prefetchers, and whether they build the same request as the views
grep -rn 'ImagePrefetcher' --include='*.swift' .
# Defaults someone changed: find out why before you keep or remove them
grep -rnE 'isTaskCoalescingEnabled|isDecompressionEnabled|skipDecompression|isProgressiveDecodingEnabled|isResumableDataEnabled|TaskQueue\(|maxConcurrentTaskCount|costLimit|sizeLimit' --include='*.swift' .
# Processors of your own: is the identifier stable?
grep -rnE ': ImageProcessing\b' --include='*.swift' .
What to fix, in the order it pays
Download less
The cheapest image is the one the server resizes. If the CDN takes a size, as in ?w=360, ask for the size you display. That saves the data, the decode, and the memory, and nothing below does all three.
A decoded image has a scale of 1, so a 360-pixel file is 360 points wide. If the view shows it at its natural size, or a processor of yours draws in points, set request.scale to the scale of the screen.
Decode less
Tell the pipeline the size you display at, in the request or with .processors. A SwiftUI .frame doesn't: it scales a full-size bitmap on every draw.
LazyImage(url: url) { state in
常见问题
它如何衡量速度?
它使用 Nuke 的诊断日志,该日志会为每个图片任务输出一行,包含其耗时和来源。
如果我无法运行应用怎么办?
这项技能会要求把修改标注为仅基于代码阅读,尚未经过实际测量。
它涵盖较旧的 Nuke 版本吗?
涵盖。它支持 Nuke 14,并会说明与 13 版本的不同之处。
详情
作者的原始描述
Find out why images load slowly, scrolling stutters, memory grows, or the cache misses in an app that uses Nuke, and fix it. Use when asked to make images faster, to use less memory or data, to review how an app sets up Nuke, or to tune ImagePipeline, ImageCache, DataCache, ImagePrefetcher, or LazyImage. Covers Nuke 14, and what differs on 13.
repo: kean/Nuke
热度
- GitHub 星标
- 8.7k
- 排名
- #12333
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …