Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6255643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:24:13+00:00 2026-05-24T14:24:13+00:00

I have always wondered how i can control what is cached into the memory.

  • 0

I have always wondered how i can control what is cached into the memory.

I always thought it was not possible to do with c++ atleast.

Until one day when a person told me not to include lua scripts in c++ application because it “…is notorious for completely ruining your cache…”.

That got me thinking , is there anyway in c++ or any other compiled language to control what your program caches in memory.Because if lua can modify my cache performance, then why can’t I.

If so,

i. Is it Architecture dependent or OS dependent ?

ii. Can you access what is in the cache or what is cached?.

Just to be Clear i am talking about CPU cache.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:24:15+00:00Added an answer on May 24, 2026 at 2:24 pm

    The CPU will cache all the data it needs and because its size is limited when it has to load something new it will drop anything that was the least recently used.

    Basically you don’t have direct control over it, but indirectly you have some:

    What you have to know is that CPUs use cache lines. Each cache line is a small block of memory.

    So if the CPU needs some data it will fetch the whole block. So, if you have some data that is very frequently used and would normally be scattered in the memory you can put it for example inside a struct so the the effective usage of the CPU cache will better (you cache less things that aren’t really needed). Note: 99.99% of the time you don’t need these kind of optimizations.

    A more useful example is walking through a 2d array that doesn’t fit into cache. If you walk it linearly you will load each cache line once, process it and some point later the CPU will drop it. If you use the indexes the wrong way each cache line will be loaded multiple times and because main memory access is slow, your code will be a lot slower. CPU can also do better prefetching if you walk linearly (direction doesn’t matter).

    Cache peformance can also be ruined by calling some external library which needs a lot of data and/or code so you main program+data will be dropped from the caches and when the call finishes the CPU has to load it again.

    If you do heavy optimizations and want to know how you utilize the L1/L2/.. cache you can do simulations. Valgrind has an excellent module called Cachegrind which does exactly that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have always wondered how delegates can be useful and why shall we use
I have always wondered how OSs such as Windows or Mac OS X can
Ok, i have always wondered if these 2 actions are possible: To manipulate cookies.
I have always wondered why can't we use hyphens in between function names and
I have always wondered but can't find a definitive answer. When should I use
I have always wondered about this. So many application setups have a zip file
I have always wondered how programming languages are different, if they are indeed different,
I have always wondered what's the best way to achieve this task. In most
I've always wondered, lets say you have two asp.net websites running in the same
I have always thought that in order to connect to SQL server using windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.