Everything2
Near Matches
Ignore Exact
Full Text
Everything2

locality of reference

created by Recluse

(idea) by Recluse (4.2 y) (print)   ?   (I like it!) Wed Jul 18 2001 at 2:28:37

A rule of thumb in computing is that a program will spend 90% of its execution time in 10% of the code. One impact this has is the use of caching to contain those instructions which have been used recently, have been used often, and are near the current instruction 'physicially'.

Two types of locality have been noted: temporal locality and spatial locality. A data structure which uses locality of reference to achieve better long-term performace is the splay tree (a binary search tree with extra rules).


(idea) by ariels (2.7 d) (print)   ?   (I like it!) Mon Feb 11 2002 at 14:15:54

The claim that access to storage when computing is typically highly localized, i.e. that accesses which occur within a short time of each other tend to refer to memory addresses which are close to each other.

Since it is (usually, somewhat, hopefully, maybe, often, ...) true, much of computer architecture and systems architecture is about exploiting locality of reference. Caches are used (for both instructions and data). The cache is much smaller but faster than main memory. But locality of reference means cache misses are relatively rare, making caches attractive in improving performance. Operating systems typically employ virtual memory -- using main memory as a cache for disk storage. Again, this works because the desired page is almost always in memory, thanks to locality. Good OSes also try to ensure that the files themselves are stored contiguously on disk -- locality of reference will mean access to the files is faster, since reading nearby sectors is generally faster. (Interleaving doesn't come into this; think of "nearby sectors" as refering not to "geographic" layout but to the interleaved layout).

A modern CPU typically accesses memory for both instructions (code, program) and for data. Codes which contain few branches will obviously exhibit high locality of reference: unless a branch is taken, the next instruction fetched is adjacent to the current instruction! Even most branches are typically to near locations (loops with a small- to medium-sized body generally account for a large proportion of execution time). So locality of reference for instruction fetching is generally assured. (Note, however, that overly structured codes can spend a large portion of their time jumping between subroutines in different modules, and exhibit correspondingly poor locality of reference).

Data access is also often highly localized. In cases where this is not so (and if performance matters!), improvements to algorithms can help. After all, architectures are today optimized for codes with locality of reference --- so it makes sense to force locality onto your code!

For example, suppose we store matrices with adjacent elements in row major order. There are two ways to write a routine to add matrices: one can add all pairs of elements in row-major order, or in column-major order. The row-major loop will exhibit excellent locality of reference, while the column-major one will exhibit terrible locality of reference. For large matrices, the difference will be significant. Obviously, one should pick the row-major loop.

What about matrix multiplication? Here, the usual algorithm traverses one matrix in row-major order and the other in column-major order! Obviously, we cannot traverse both matrices efficiently. A change of algorithm is called for; various "blocking" strategies produce an end result by working on smaller blocks in the matrices, which exhibits better locality.


printable version
chaos

Ninety-Ninety Rule splay tree Cache-oblivious algorithms Sturgeon's Law
Amdahl's law interleaving Fully-associative cache instruction set
matrix multiplication temporal Computer Architecture cache
Rotating a String solution CPU contiguous February 17, 2002
spec Reference locality quantum non-locality
cache miss instruction virtual memory spatial
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Little presents from the Node Fairy:
We don't write poetry because it's cute
Jacó
Vaclav Havel's address to the US Congress, 21 February 1990
Watching Robyn Hitchcock
A Public Execution Is No Picnic
I reserve the right to club you and eat your bones
Cries and Whispers
False Memory Syndrome Foundation
Celtic Mythology
Death of a Yuppie
Lesbian conspiracy
node
Goth vs. Gothic
New Writeups
antigravpussy
One fly amongst many(person)
sam512
Moon Base Shackleton, 1978(fiction)
Pavlovna
toy boy(person)
XWiz
tear jerker(review)
Heitah
Anarchy is Order(idea)
jessicaj
July 26, 2008(dream)
Berek
ABBA(person)
devolution
k-hole(place)
Nadine_2
The Sound Of Madness(review)
SwimmingMonkey
Conversations with Fo Fo, the Loneliest dog in Purgatory(fiction)
locke baron
lynx(thing)
Simulacron3
Reality, Dimensions and the Natural Ontology(essay)
SubSane
Making Love to a 9-Foot Woman(person)
Ouzo
Thoughts(idea)
antigravpussy
I fall silent, listening. The breadcrumbs are talking about us(person)
This page courtesy of The Everything Development Company