23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

one we are only address<strong>in</strong>g now <strong>in</strong> the f<strong>in</strong>al chapter—but there are good reasons why<br />

it is actually a reasonable assumption to make.<br />

One justification for this assumption is that it is often necessary to assume that all<br />

memory accesses take the same amount of time, s<strong>in</strong>ce specific device-dependent<br />

<strong>in</strong>formation about memory sizes is often hard to come by. In fact, <strong>in</strong>formation about<br />

memory size may be impossible to get. For example, a <strong>Java</strong> program that is designed<br />

to run on many different computer platforms cannot be def<strong>in</strong>ed <strong>in</strong> terms of a specific<br />

computer architecture configuration. We can certa<strong>in</strong>ly use architecture-specific<br />

<strong>in</strong>formation, if we have it (<strong>and</strong> we will show how to exploit such <strong>in</strong>formation later <strong>in</strong><br />

this chapter). But once we have optimized our software for a certa<strong>in</strong> architecture<br />

configuration, our software will no longer be device-<strong>in</strong>dependent. Fortunately, such<br />

optimizations are not always necessary, primarily because of the second justification<br />

for the equal-time memory-access assumption.<br />

Cach<strong>in</strong>g <strong>and</strong> Block<strong>in</strong>g<br />

Another justification for the memory-access equality assumption is that operat<strong>in</strong>g<br />

system designers have developed general mechanisms that allow for most memory<br />

accesses to be fast. These mechanisms are based on two important locality-ofreference<br />

properties that most software possesses:<br />

• Temporal locality: If a program accesses a certa<strong>in</strong> memory location, then<br />

it is likely to access this location aga<strong>in</strong> <strong>in</strong> the near future. For example, it is quite<br />

common to use the value of a counter variable <strong>in</strong> several different expressions,<br />

<strong>in</strong>clud<strong>in</strong>g one to <strong>in</strong>crement the counter's value. In fact, a common adage among<br />

computer architects is that "a program spends n<strong>in</strong>ety percent of its time <strong>in</strong> ten<br />

percent of its code."<br />

• Spatial locality: If a program accesses a certa<strong>in</strong> memory location, then it<br />

is likely to access other locations that are near this one. For example, a program<br />

us<strong>in</strong>g an array is likely to access the locations of this array <strong>in</strong> a sequential or nearsequential<br />

manner.<br />

Computer scientists <strong>and</strong> eng<strong>in</strong>eers have performed extensive software profil<strong>in</strong>g<br />

experiments to justify the claim that most software possesses both of these k<strong>in</strong>ds of<br />

locality-of-reference. For example, a for-loop used to scan through an array will<br />

exhibit both k<strong>in</strong>ds of locality.<br />

Temporal <strong>and</strong> spatial localities have, <strong>in</strong> turn, given rise to two fundamental design<br />

choices for two-level computer memory systems (which are present <strong>in</strong> the <strong>in</strong>terface<br />

between cache memory <strong>and</strong> <strong>in</strong>ternal memory, <strong>and</strong> also <strong>in</strong> the <strong>in</strong>terface between<br />

<strong>in</strong>ternal memory <strong>and</strong> external memory).<br />

The first design choice is called virtual memory. This concept consists of provid<strong>in</strong>g<br />

an address space as large as the capacity of the secondary-level memory, <strong>and</strong> of<br />

transferr<strong>in</strong>g data located <strong>in</strong> the secondary level, <strong>in</strong>to the primary level, when they<br />

896

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!