create account

Code Optimization: Memory by krishtopa

View this thread on: hive.blogpeakd.comecency.com
· @krishtopa · (edited)
$5.18
Code Optimization: Memory
![](https://i.imgur.com/XWQGbNm.jpg)

*Most programmers think that computing system is a processor which executes instructions and a memory that stores instructions and data for the processor. In this simple model, the memory is considered as a linear system of bytes and a processor can refer to any area in memory in constant time. Although this model is effective in most situations, it does not reflect how modern systems actually work.*

*In fact, the memory system forms a hierarchy that consists of storage devices that have different capacity, cost and access time. Small fast cache, located close to the processor, is the buffer zone that stores a small piece of data located in a relatively slow memory. RAM serves as a buffer for slow local drives. Local disks are used to buffer data from remote machines connected through a network.*

*In this article, we will learn how the storage devices are organized into a hierarchy. We will especially focus on the cache memory, which serves as a buffer zone between the processor and RAM.*

-------

# Hierarchy of memory

Modern storage systems form a hierarchy from fast small types of memory to large slow types. A specific level of the hierarchy is a cache for data located on a lower level. This means that it has got a copy of data from a lower level. When the processor wants to get some data, it looks for it on the first - the fastest high levels. And goes down to the lower, if it can’t find.

CPU registers are located on the top of the hierarchy. Access to them takes 0 cycles, but there are few of them. Next, there are few kilobytes of the cache memory of the first level, access to which will take approximately 4 cycles. Then there are a couple of hundred kilobytes of a slower cache of the second level. Then there are few megabytes of cache on the third level. It is much slower but still faster than RAM. Next, there is relatively slow RAM.

![](https://i.imgur.com/DlJrHHV.png)

The RAM can be considered as a cache to the local disk. They are large, slow, and cheap. The computer downloads the files from the disk into RAM when it starts working with them. The gap in access time between RAM and disk is enormous. The drive is slower that RAM in thousands of times and is slower than the cache of the first level in million times. It is better to address several thousand times to the RAM than once to the disk. Such data structures as B-trees are based on this rule.

The local disk itself may be considered as a cache for data that are placed on remote servers. When you visit a website, your browser stores images from a Web page on a disk, so that when you visit them again you didn’t need to download them.

Fast memory is very expensive, and slow is very cheap. It is a great idea of architects of systems to combine the large size of the slow and small size of cheap memory. Thus, the system can run at a fast speed and be not expensive.

# Locality

Locality - the basic concept of this article. As a rule, the programs with good locality are faster than programs with poor locality.

When the processor reads data from the memory, it copies it to its own cache, removing the other data from the cache. The program works better with fewer variables and often refers to them.

![](https://i.imgur.com/MKnA0hY.jpg)

Moving of data between levels of hierarchy is carried out with certain size blocks. For example, Core i7 Haswell processor moves data between caches with the block of 64 bytes.

Therefore, if you read some bytes from memory, and then read the byte next to it, it will certainly be in the cache. This is a benefit of spatial locality. We need to work with data that is located in memory near each other.

-------

*The memory system is organized as a hierarchy of storage devices with small and fast devices at the top of the hierarchy and large and slow devices below. Programs with a good locality work with the data from processor caches. Programs with poor locality work with data from relatively slow RAM.*

***In particular, I can recommend the following techniques:***

- *Concentrate your attention on the inner loops. There are a lot of computations and memory accesses.*
- *Try to maximize the spatial locality reading from the memory sequentially in the order in which they are located.*
- *Try to maximize the temporal locality using data objects as often as possible after they have been read from memory.*

References: [1](http://www.viva64.com/en/t/0084/), [2](http://www.tutorialspoint.com/compiler_design/compiler_design_code_optimization.htm)

*[Follow me](https://steemit.com/@krishtopa), to learn more about **popular science**, **math**, and **technologies***

**With Love,
Kate**

Image credit: [1](http://www.austbssc.xyz/memory-hierarchy-by-gurpreet-singh/), [2](http://henanhaochi.com/diagram/diagram-of-memory-hierarchy), [3](http://www.rightrelevance.com/search/articles?query=web%20development)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 273 others
properties (23)
authorkrishtopa
permlinkcode-optimization-memory
categoryprogramming
json_metadata{"tags":["programming","technology","education","it","internet"],"image":["https://i.imgur.com/XWQGbNm.jpg","https://i.imgur.com/DlJrHHV.png","https://i.imgur.com/MKnA0hY.jpg"],"links":["http://www.viva64.com/en/t/0084/","http://www.tutorialspoint.com/compiler_design/compiler_design_code_optimization.htm","https://steemit.com/@krishtopa","http://www.austbssc.xyz/memory-hierarchy-by-gurpreet-singh/","http://henanhaochi.com/diagram/diagram-of-memory-hierarchy","http://www.rightrelevance.com/search/articles?query=web%20development"],"app":"steemit/0.1","format":"markdown"}
created2016-11-28 15:41:24
last_update2016-11-28 18:44:30
depth0
children9
last_payout2016-12-29 18:07:27
cashout_time1969-12-31 23:59:59
total_payout_value4.713 HBD
curator_payout_value0.467 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,918
author_reputation48,350,480,258,009
root_title"Code Optimization: Memory"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,864,987
net_rshares25,992,248,263,961
author_curate_reward""
vote details (337)
@allfabeta · (edited)
почему вы решили уйти из Стима?
Why did you decide to leave Steemit platform?
properties (22)
authorallfabeta
permlinkre-krishtopa-code-optimization-memory-20170810t195552945z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-08-10 19:55:54
last_update2017-08-10 19:56:39
depth1
children3
last_payout2017-08-17 19:55:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length77
author_reputation36,177,226,486,370
root_title"Code Optimization: Memory"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id11,430,792
net_rshares0
@krishtopa ·
$0.02
Я не уходила) Но совсем не было времени. Извиняюсь за поздний ответ. Unfortunately, it was hard year, I was trying to do too many things at the same time.
👍  
properties (23)
authorkrishtopa
permlinkre-allfabeta-re-krishtopa-code-optimization-memory-20171209t175210203z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-12-09 17:52:12
last_update2017-12-09 17:52:12
depth2
children2
last_payout2017-12-16 17:52:12
cashout_time1969-12-31 23:59:59
total_payout_value0.018 HBD
curator_payout_value0.005 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length154
author_reputation48,350,480,258,009
root_title"Code Optimization: Memory"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id22,910,786
net_rshares6,540,505,476
author_curate_reward""
vote details (1)
@allfabeta ·
$0.54
Надеюсь опыт пошел на пользу и вы нашли что-то свое ?
👍  ,
properties (23)
authorallfabeta
permlinkre-krishtopa-re-allfabeta-re-krishtopa-code-optimization-memory-20171211t031134521z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-12-11 03:11:33
last_update2017-12-11 03:11:33
depth3
children1
last_payout2017-12-18 03:11:33
cashout_time1969-12-31 23:59:59
total_payout_value0.404 HBD
curator_payout_value0.131 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length53
author_reputation36,177,226,486,370
root_title"Code Optimization: Memory"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id23,064,171
net_rshares124,742,291,881
author_curate_reward""
vote details (2)
@ashwim ·
thanks for sharing this...
properties (22)
authorashwim
permlinkre-krishtopa-code-optimization-memory-20161128t171809677z
categoryprogramming
json_metadata{"tags":["programming"]}
created2016-11-28 17:18:09
last_update2016-11-28 17:18:09
depth1
children0
last_payout2016-12-29 18:07:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length26
author_reputation889,653,568,721
root_title"Code Optimization: Memory"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,865,584
net_rshares0
@linkback-bot-v0 ·
This post has been linked to from another place on Steem.


  - [Advanced Steem Metrics Report for 28th November 2016](https://steemit.com/steemit/@ontofractal/advanced-steem-metrics-report-for-28th-november-2016) by @ontofractal




Learn more about and upvote to support [**linkback bot v0.5**](https://steemit.com/steemit/@ontofractal/steem-linkback-bot-v0-5-the-reddit-awareness-release). Flag this comment if you don't want the bot to continue posting linkbacks for your posts.

Built by @ontofractal
properties (22)
authorlinkback-bot-v0
permlinkre-krishtopa-code-optimization-memory-linkbacks
categoryprogramming
json_metadata{}
created2016-11-29 19:36:42
last_update2016-11-29 19:36:42
depth1
children0
last_payout2016-12-29 18:07:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length506
author_reputation1,915,954,976,722
root_title"Code Optimization: Memory"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,874,828
net_rshares0
@readallaboutit ·
Thank you for upvoting my photo. If you like that one you will love my other ones. https://steemit.com/@readallaboutit
properties (22)
authorreadallaboutit
permlinkre-krishtopa-code-optimization-memory-20170613t133912609z
categoryprogramming
json_metadata{"tags":["programming"],"links":["https://steemit.com/@readallaboutit"],"app":"steemit/0.1"}
created2017-06-13 13:39:06
last_update2017-06-13 13:39:06
depth1
children0
last_payout2017-06-20 13:39:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length118
author_reputation7,213,483,635,136
root_title"Code Optimization: Memory"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,735,085
net_rshares0
@screenname ·
Re: Code Optimization: Memory
<p>This post has been ranked within the top 80 most undervalued posts in the second half of Nov 28. We estimate that this post is undervalued by $10.94 as compared to a scenario in which every voter had an equal say.</p> 
<p>See the full rankings and details in <a href="https://steemit.com/curation/@screenname/the-daily-tribune-most-undervalued-posts-of-nov-28---part-ii">The Daily Tribune: Nov 28 - Part II</a>. You can also read about some of our methodology, data analysis and technical details in <a href="https://steemit.com/curation/@screenname/introducing-the-daily-tribune-most-undervalued-posts-of-nov-04---part-i">our initial post</a>.</p>
<p>If you are the author and would prefer not to receive these comments, simply reply "Stop" to this comment.</p>
👍  , , , , ,
properties (23)
authorscreenname
permlinkre-code-optimization-memory-20161129t012338
categoryprogramming
json_metadata"{"replyto": "@krishtopa/code-optimization-memory"}"
created2016-11-29 01:23:39
last_update2016-11-29 01:23:39
depth1
children0
last_payout2016-12-29 18:07:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length766
author_reputation46,276,338,038,330
root_title"Code Optimization: Memory"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id1,868,927
net_rshares32,230,301,559
author_curate_reward""
vote details (6)
@steemitboard ·
Congratulations @krishtopa! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png)](http://steemitboard.com/@krishtopa) Award for the number of upvotes

Click on any badge to view your own Board of Honnor on SteemitBoard.
For more information about SteemitBoard, click [here](https://steemit.com/@steemitboard)

If you no longer want to receive notifications, reply to this comment with the word `STOP`

By upvoting this notification, you can help all Steemit users. Learn how [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-krishtopa-20170610t092516000z
categoryprogramming
json_metadata{"image":["https://steemitboard.com/img/notifications.png"]}
created2017-06-10 07:25:24
last_update2017-06-10 07:25:24
depth1
children0
last_payout2017-06-17 07:25:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length687
author_reputation38,975,615,169,260
root_title"Code Optimization: Memory"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id4,517,840
net_rshares0