Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Mmap with its applications in git and libgit2
Search
Minqi Pan
January 08, 2016
Programming
210
1
Share
Mmap with its applications in git and libgit2
Minqi Pan
January 08, 2016
More Decks by Minqi Pan
See All by Minqi Pan
RubyConf 2017: Packing your Ruby application into a single executable
pmq20
1
720
Node 8 and Friends
pmq20
0
270
Moving Node.js and nodec to v8 Ignition
pmq20
0
400
QCon 2017 Beijing Observations
pmq20
0
320
Node.js Compiler: compiling your Node.js application into a single executable
pmq20
5
25k
Enclose.IO: current cutting-edges and the future work
pmq20
0
290
环境变量为何能让 Ruby 快十倍
pmq20
3
790
Introductions to Node.js internals
pmq20
0
2.1k
How we scaled GitLab for a 30k-employee company
pmq20
6
1.7k
Other Decks in Programming
See All in Programming
年間50登壇、単著出版、雑誌寄稿、Podcast出演、YouTube、CM、カンファレンス主催……全部やってみたので面白さ等を比較してみよう / I’ve tried them all, so let’s compare how interesting they are.
nrslib
4
640
一度始めたらやめられない開発効率向上術 / Findy あなたのdotfilesを教えて!
k0kubun
3
2.7k
AIコードレビューの導入・運用と AI駆動開発における「AI4QA」の取り組みについて
hagevvashi
0
580
Xdebug と IDE による デバッグ実行の仕組みを見る / Exploring-How-Debugging-Works-with-Xdebug-and-an-IDE
shin1x1
0
300
ポーリング処理廃止によるイベント駆動アーキテクチャへの移行
seitarof
3
1.3k
Mastering Event Sourcing: Your Parents Holidayed in Yugoslavia
super_marek
0
130
AI時代の脳疲弊と向き合う ~言語学としてのPHP~
sakuraikotone
1
1.8k
Codex の「自走力」を高める
yorifuji
0
1.3k
Coding as Prompting Since 2025
ragingwind
0
620
Goの型安全性で実現する複数プロダクトの権限管理
ishikawa_pro
2
1.4k
実践ハーネスエンジニアリング #MOSHTech
kajitack
7
5.3k
見せてもらおうか、 OpenSearchの性能とやらを!
shunta27
1
170
Featured
See All Featured
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
340
Marketing to machines
jonoalderson
1
5.1k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Done Done
chrislema
186
16k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
64
53k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.1k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
180
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
How Software Deployment tools have changed in the past 20 years
geshan
0
33k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.4k
SEO for Brand Visibility & Recognition
aleyda
0
4.4k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.2k
Transcript
mmap 在 git 与 libgit2 中的应⽤用 Minqi Pan
I’m Minqi Pan github.com/pmq20 twitter @psvr
mmap • ⼆二类⼿手册,系统调⽤用 • IEEE POSIX 标准 API 之⼀一 •
Linux / Mac OS X / BSD 均⽀支持
⾏行行为 • 将⽂文件或设备映射⾄至内存,起初完全不读取⽂文件、 完全不占⽤用内存,懒读取 • 进程发起对这片映射空间的访问,引发缺⻚页异常, 实现⽂文件内容到物理理内存(主存)的拷⻉贝 • 系统会周期性地⾃自动回写脏⻚页⾯面到对应的⽂文件磁 盘上,有⼀一段时间的延Ӿ
与常规⽂文件的区别 • 常规⽂文件:磁盘 -> ⻚页缓存 -> ⽤用户主存 • ⻚页缓存在内核空间中,空间不同数据不通 •
mmap:磁盘 -> ⽤用户主存
⽤用法 #include <sys/mman.h> void * // 返回被映射区域的指针 mmap(void *addr, //
被映射的⻚页从 addr 开始 size_t len, // 被映射的⻚页的最⼤大⻓长度 int prot, // ⻚页的读写跑权限保护 int flags, // 选项 int fd, // 映射源 off_t offset); // 映射源的起点偏移量量
None
构造 vm_area_struct
应⽤用 • 按需分⻚页——仅当缺⻚页(访问虚拟地址但其物理理内存未 加载)时 OS 才进⾏行行磁盘到内存的拷⻉贝 • ⻚页缓存——缓存诸如动态库等磁盘数据,通过虚拟内存 映射共享到使⽤用者的进程空间内 •
⾼高效IPC——不同进程将⾃自⾝身⽤用户空间映射到同⼀一个⽂文件 (管道和MQ需要在内核和⽤用户空间进⾏行行四次数据复制) • ⼤大数据处理理——内存空间不⾜足时使⽤用
在 git 应⽤用场景下 • pack 与 delta window 的概念念 •
delta window 与系统分⻚页⼤大⼩小的关系,避免缺⻚页 • 这就是为什什么 pack 内的对象顺序⾄至关重要
mmap 在 git 中应⽤用⼗十分⼴广泛
在 git 中的应⽤用 use_pack pack 的处理理属于⼤大数据处理理,内存空间不⾜足
在 git 中的应⽤用 map_sha1_file
在 git 中的应⽤用 git_config_set_multivar_in_file 如果进程A和进程B都映射了了区域C,当A第⼀一次读取C时通 过缺⻚页从磁盘复制⽂文件⻚页到内存中;但当B再读C的相同⻚页 ⾯面时,虽然也会产⽣生缺⻚页异常,但是不再需要从磁盘中复 制⽂文件过来,⽽而可直接使⽤用已经保存在内存中的⽂文件数据。
在 git 中的应⽤用 diff_populate_filespec While doing rename detection and pickaxe
operation, we may need to grab the data for the blob (or file) for our own in-core comparison.
在 git 中的应⽤用 do_read_index
在 git 中的应⽤用 check_packed_git_idx Open and mmap the index file
at path, perform a couple of consistency checks
在 git 中的应⽤用 do_read_index
在 git 中的应⽤用 ⽤用于⼩小⼯工具读⽂文件
在 git 中的应⽤用 ⽤用于 pack-bitmap
在 git 中的应⽤用 ewah bitmap compression
mmap 在 libgit2 中应⽤用⼗十分有限 仅⽤用于 pack 和 index
在 libgit2 中的应⽤用 new_window
在 libgit2 中的应⽤用 (indexer) parse_header
在 libgit2 中的应⽤用 (indexer) write_at
在 libgit2 中的应⽤用 pack_index_check When doing Packed Object lookup query,
Mmap the index file and do a quick run through the header to guess the index version, and to verify that the size of the index makes sense.
在 libgit2 中的应⽤用 pack_index_check
Thank you https://github.com/pmq20/