locality
Computer Systems A Programmer's Perspective Second Edition
Well-written computer programs tend to exhibit good
locality
. That is, they tend
to reference data items that are near other recently referenced data items, or
that were recently referenced themselves. This tendency, known as the
principle
of locality
, is an enduring concept that has enormous impact on the design and
performance of hardware and software systems.
Section 6.2 Locality
587
Locality is typically described as having two distinct forms:
temporal locality
and
spatial locality
. In a program with good temporal locality, a memory location
that is referenced once is likely to be referenced again multiple times in the near
future. In a program with good spatial locality, if a memory location is referenced
once, then the program is likely to reference a nearby memory location in the near
future.
Programmers should understand the principle of locality because, in general,
programs with good locality run faster than programs with poor locality
. All levels
of modern computer systems, from the hardware, to the operating system, to
application programs, are designed to exploit locality. At the hardware level, the
principle of locality allows computer designers to speed up main memory accesses
by introducing small fast memories known as
cache memories
that hold blocks of
the most recently referenced instructions and data items. At the operating system
level, the principle of locality allows the system to use the main memory as a cache
of the most recently referenced chunks of the virtual address space. Similarly, the
operating system uses main memory to cache the most recently used disk blocks in
the disk file system. The principle of locality also plays a crucial role in the design
of application programs. For example, Web browsers exploit temporal locality by
caching recently referenced documents on a local disk. High-volume Web servers
hold recently requested documents in front-end disk caches that satisfy requests
for these documents without requiring any intervention from the serve.