# 局部性原理

**局部性原理**（Locality of reference）是指程序在较长时间内将其内存引用聚集到地址空间中一小部分子集上的可观察趋势，因此最近访问过的数据，或存储在最近访问过的数据附近的数据，很可能很快会再次被需要。彼得·J·丹宁（Peter J. Denning）于 20 世纪 60 年代在麻省理工学院（MIT）研究虚拟内存时提出了这一原理，他将局部性定义为“程序在较长时间内将引用聚集到地址空间子集的趋势”<sup>[1](https://roclocality.org/2016/07/13/dennings-definition-of-locality/)</sup>。该原理是计算各个层面上缓存的基础，从处理器缓存到万维网内容分发，无不如此。<sup>[2](https://denninginstitute.com/pjd/PUBS/locality_2006.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 时间局部性 | 若程序访问了某个值，则很可能很快再次访问同一个值<sup>[3](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup> |
| 空间局部性 | 若程序访问了某个值，则很可能很快访问附近的地址（数值上接近）<sup>[3](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup> |
| 缓存块大小 | 如今大多数缓存在缺失时会取回一个 64 字节的块，而不仅仅是处理器所需的那几个字节<sup>[4](https://openstax.org/books/introduction-computer-science/pages/5-5-memory-hierarchy)</sup> |
| 平均访问时间 | \( t_{\text{avg}} = t_{\text{hit}} + r_{\text{miss}} \times t_{\text{miss}} \)；在命中时间为 1 纳秒、内存访问时间为 50 纳秒、命中率为 95% 的情况下，平均访问耗时 3.5 纳秒<sup>[3](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup> |
| 缺失分类 | 冷启动（首次访问）、冲突（相联度不足）和容量（工作集大于缓存）<sup>[3](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup> |
| 实测效果 | 在一台笔记本电脑上，单位步长访问的矩阵-向量乘积运行耗时 14 毫秒，而同一计算采用跨步访问的版本耗时 118 毫秒<sup>[5](https://www.cs.cornell.edu/courses/cs4220/2026sp/lec/2026-01-21-blas.html)</sup> |
| 软件补救 | 分块（tiling）重构循环，使较小的工作集能装入缓存；最优块大小取决于机器的缓存大小<sup>[6](https://www.cs.cornell.edu/courses/cs3410/2025sp/assignments/cacheblock/instructions.html)</sup> |

## 时间局部性与空间局部性

计算机体系结构设计师区分两种形式的局部性，二者都是关于普通程序行为的假设<sup>[3](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup>。

**时间局部性**是引用在时间上的聚集：现在使用的值很可能很快再次被使用。循环计数器或累加器变量是标准例子；循环体中的指令在每次迭代时也会被重用<sup>[3](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup><sup> • </sup><sup>[4](https://openstax.org/books/introduction-computer-science/pages/5-5-memory-hierarchy)</sup>。

**空间局部性**是引用在地址空间上的聚集：在访问一个地址之后，程序很快会访问附近的地址。逐元素地对数组求和具有很高的空间局部性，因为在加载 `a[i]` 之后，程序很快会加载 `a[i+1]`，对于 32 位整数而言仅相隔 4 个字节<sup>[3](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup>。

丹宁将这一行为归结为两个原因：时间上的聚集来自循环以及在带有私有数据的模块内执行，空间上的聚集来自相关的值被归入数组、序列和其他数据结构。他观察到，即使没有明确规划，程序也会表现出这种阶段行为，每个程序的局部性集合序列就像一种独特的声纹<sup>[2](https://denninginstitute.com/pjd/PUBS/locality_2006.pdf)</sup>。

## 缓存如何利用局部性

缓存是置于处理器与主存之间的一种小而快的存储器。它之所以有效，完全是因为局部性<sup>[3](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup>。在缺失时，缓存取回一整块连续字节，而不是所请求的单个字；大多数现代缓存使用 64 字节的块<sup>[4](https://openstax.org/books/introduction-computer-science/pages/5-5-memory-hierarchy)</sup>。这将空间局部性转化为命中：推测性取回的相邻字节正是程序接下来很可能请求的字节。为了利用时间局部性，缓存会保留最近使用过的行，直到该空间需要用于其他内容，由最近最少使用等淘汰策略决定替换什么<sup>[7](https://www.cs.cornell.edu/~bindel/nmds/00-Background/02-Performance.html)</sup>。

性能上的影响很大，因为存储层次在延迟上跨越多个数量级。在一台笔记本电脑上，L1 访问耗时 3 到 4 个周期，L2 和 L3 约 18 个周期，而主存在最坏情况下约 350 个周期<sup>[5](https://www.cs.cornell.edu/courses/cs4220/2026sp/lec/2026-01-21-blas.html)</sup>。平均内存访问时间将这些结合起来：\( t_{\text{avg}} = t_{\text{hit}} + r_{\text{miss}} \times t_{\text{miss}} \)，其中 \( r_{\text{miss}} \) 是发生缺失的访问比例。该公式显示了设计者可以调节的三个杠杆：降低命中时间、缺失率或缺失代价<sup>[3](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup>。

缺失通常分为三类：对某一行的首次访问产生的冷启动缺失或强制性缺失；在相联度有限的缓存中，过多行竞争同一组时产生的冲突缺失；以及工作集超出缓存时产生的容量缺失<sup>[3](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)</sup>。良好的空间局部性和时间局部性通常是影响程序性能的更主要因素；冲突缺失则在之后处理，例如通过填充数据或使用临时空间<sup>[8](https://www.ocw.mit.edu/courses/6-172-performance-engineering-of-software-systems-fall-2018/7d9bd2d2b309a0c7d07011d1fe1f9302_xDKnMXtZKq8.pdf)</sup>。

## 编写对局部性友好的代码

由于朴素编写的程序可能受内存限制，程序员和编译器会重构代码以暴露局部性。主要技术是循环交换——改变嵌套循环的顺序，使最内层循环以单位步长遍历内存——以及分块（tiling），即增加额外的循环来处理能装入缓存的固定大小的块<sup>[6](https://www.cs.cornell.edu/courses/cs3410/2025sp/assignments/cacheblock/instructions.html)</sup>。

访问顺序的实测效果可能接近一个数量级。在一项对 4000×4000 矩阵上两个数学上完全相同的矩阵-向量乘积的比较中，内层循环以单位步长遍历矩阵的版本耗时约 14 毫秒，而步长为一整行的版本耗时约 118 毫秒；单位步长版本每 16 个双精度数取回一条缓存行，产生的缓存缺失大约少一个数量级<sup>[5](https://www.cs.cornell.edu/courses/cs4220/2026sp/lec/2026-01-21-blas.html)</sup>。

数据布局与循环顺序同样重要。以行主序存储的矩阵有利于逐行遍历它的算法；布局的选择对访问它的算法的局部性有着康奈尔大学 CS 3410（Cornell CS 3410）课程材料所称的深远影响<sup>[6](https://www.cs.cornell.edu/courses/cs3410/2025sp/assignments/cacheblock/instructions.html)</sup>。分块之所以有效，恰恰是因为它创建了能装入缓存的较小工作集，因此最优块大小取决于机器缓存的大小<sup>[6](https://www.cs.cornell.edu/courses/cs3410/2025sp/assignments/cacheblock/instructions.html)</sup>。

## 硬件与系统支持

处理器通过预取指令向软件暴露局部性，这些指令是提示某条缓存行很快将被需要的线索。Rust 标准库的预取接口接受一个时间局部性说明符，范围从 0（表示无局部性）到 3（表示极度局部并保留在缓存中）；在 AArch64 上，这些级别分别映射为取入 L3、L2 或 L1 缓存，其中 0 级保留给仅使用一次的流式或非时间性数据<sup>[9](https://rust.docs.kernel.org/next/core/hint/fn.prefetch_read.html)</sup><sup> • </sup><sup>[10](https://rust.docs.kernel.org/src/core/hint.rs.html)</sup>。预取可以减少缺失延迟，但也可能增加带宽使用或驱逐其他行，而且硬件可以自由地忽略它<sup>[9](https://rust.docs.kernel.org/next/core/hint/fn.prefetch_read.html)</sup>。一些体系结构走得更远：PA-RISC 2.0 规范定义了一种空间局部性缓存提示，将数据取入缓冲区而不置换缓存内容，旨在用于复用很少或没有复用的大规模顺序传输，例如操作系统的块复制例程——据观察，其约 96% 的访问都发生缺失<sup>[11](https://parisc.docs.kernel.org/en/latest/_downloads/3172fe3d8ffdd58f1cdad4550f3d2395/Pa2c96.pdf)</sup>。

局部性也塑造了缓存之上的内存管理。[Linux 内核](https://www.edgepedia.org/zh/linux-kernel)的多代 LRU 负责在内存压力下管理页面回收，它将受益于空间局部性列为其设计目标之一，并在扫描页表以查找最近访问的页面时利用它<sup>[12](https://docs.kernel.org/mm/multigen_lru.html)</sup>。

## 度量局部性

局部性不仅是一种定性模式，还是一种可度量的模式。重用距离——两次连续访问同一元素之间所访问的不同数据元素的数量——能够独立于具体缓存大小预测缓存行为；[罗切斯特大学](https://www.edgepedia.org/zh/university-of-rochester)计算机科学教授陈丁（Chen Ding）与钟玉涛（Yutao Zhong）在 2003 年表明，对程序的重用距离进行剖析，可以预测其在剖析输入之外的输入上的局部性<sup>[13](https://doi.org/10.1145/780822.781159)</sup>。后来的工作形式化了标准度量之间的关系，包括访问频率、重用时间、重用距离、工作集、足迹和缓存缺失率，并给出了哪些定义等价的证明<sup>[14](https://arxiv.org/abs/1802.01254)</sup>。

## References

1. [Denning's Definition of Locality, Rochester Programming Systems Research (Chen Ding)](https://roclocality.org/2016/07/13/dennings-definition-of-locality/)
2. [Peter J. Denning, The Locality Principle](https://denninginstitute.com/pjd/PUBS/locality_2006.pdf)
3. [Caches, CS 3410 Spring 2026, Cornell University](https://www.cs.cornell.edu/courses/cs3410/2026sp/notes/caches.html)
4. [Memory Hierarchy, Introduction to Computer Science, OpenStax](https://openstax.org/books/introduction-computer-science/pages/5-5-memory-hierarchy)
5. [CS 4220: Numerical Analysis, BLAS lecture, Cornell University](https://www.cs.cornell.edu/courses/cs4220/2026sp/lec/2026-01-21-blas.html)
6. [Cache Blocking & Optimization, CS 3410, Cornell University](https://www.cs.cornell.edu/courses/cs3410/2025sp/assignments/cacheblock/instructions.html)
7. [Performance Basics, Numerical Methods for Data Science, David Bindel, Cornell University](https://www.cs.cornell.edu/~bindel/nmds/00-Background/02-Performance.html)
8. [Caching and Cache-Efficient Algorithms, MIT 6.172 Performance Engineering (Julian Shun)](https://www.ocw.mit.edu/courses/6-172-performance-engineering-of-software-systems-fall-2018/7d9bd2d2b309a0c7d07011d1fe1f9302_xDKnMXtZKq8.pdf)
9. [prefetch_read in core::hint, Rust documentation](https://rust.docs.kernel.org/next/core/hint/fn.prefetch_read.html)
10. [hint.rs source, Rust standard library](https://rust.docs.kernel.org/src/core/hint.rs.html)
11. [PA-RISC 2.0 architecture: compiler optimizations and cache hints](https://parisc.docs.kernel.org/en/latest/_downloads/3172fe3d8ffdd58f1cdad4550f3d2395/Pa2c96.pdf)
12. [Multi-Gen LRU, Linux Kernel documentation](https://docs.kernel.org/mm/multigen_lru.html)
13. [Chen Ding and Yutao Zhong (2003). Predicting whole-program locality through reuse distance analysis. ACM SIGPLAN Notices 38(5)](https://doi.org/10.1145/780822.781159)
14. [Liang Yuan, Chen Ding, Peter Denning, Yunquan Zhang (2018). A Measurement Theory of Locality. arXiv:1802.01254](https://arxiv.org/abs/1802.01254)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 计算机硬件 › Processors & processor engineering › Computer architecture theory › Memory hierarchy and caching

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/locality-of-reference

English original: https://www.edgepedia.org/locality-of-reference
