c++ - How does std::alignas optimize the performance of a program? -


in 32-bit machine, 1 memory read cycle gets 4 bytes of data.
reading below buffer, should take 32 read-cycle read buffer of 128 bytes mentioned below.

char buffer[128]; 

now, suppose if have aligned buffer mentioned below please let me know how make faster read?

alignas(128) char buffer[128]; 

i assuming memory read cycle remain 4 bytes only.

the size of registers used memory access 1 part of story, other part size of cache-line.

if cache-line 64 bytes , char[128] naturally aligned, cpu needs manipulate 3 different cache-lines. alignas(64) or alignas(128), 2 cache-lines need touched.

if working memory mapped file, or under swapping conditions, next level of alignment kicks in: size of memory page. call 4096 or 8192 byte alignments.

however, doubt alignas() has significant positive effect if specified alignment larger natural alignment compiler uses anyway: increases memory consumption, may enough trigger more cache-lines/memory pages being touched in first place. it's small misalignments need avoided because may trigger huge slowdowns on cpus, or might downright illegal/impossible on others.

thus, truth in measurement: if need speedup can get, try it, measure runtime difference, , see whether works out.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -