TIL-4: Alignment
Table of Contents
Alignment and Cache⌗
Common cache line size is 64 bytes
To ensure data in different thread doesn’t interfere with other due to the requirement of cache consistency, we need to make sure they operate on data that is on different cache lines
C++ provides us with keyword
alignas
alignas
can be used to 1) the declaration or definition of a class; 2) the declaration of a non-bitfield class data member; 3) the declaration of a variable, except that it cannot be applied to the following: a function parameter; the exception parameter of a catch clause.- Example:
struct alignas(32) sse_t { ... };
oralignas(64) int x;
orint alignas(64) x;
C++ also provide
hardware_destructive_interference_size
andhardware_constructive_interference_size
in<new>
. According to cppreference, “these constants provide a portable way to access the L1 data cache line size.” 1hardware_destructive_interference_size
defines “minimum offset between two objects to avoid false sharing.”hardware_constructive_interference_size
defines “maximum size of contiguous memory to promote true sharing.”// occupies one cache line struct alignas(hardware_constructive_interference_size) OneCacheLiner { std::atomic_uint64_t x{}; std::atomic_uint64_t y{}; }; // occupies two cache lines struct TwoCacheLiner { alignas(hardware_destructive_interference_size) std::atomic_uint64_t x{}; alignas(hardware_destructive_interference_size) std::atomic_uint64_t y{}; } twoCacheLiner;
What exactly is alignment?
- “The number of bytes between successive addresses at which objects of this type can be allocated”
- “Each object type imposes its alignment requirement on every object of that type;”
- “The largest fundamental alignment of any type is implementation-defined and equal to the alignment of
std::max_align_t
” (which is the size oflong double
16 bytes)
alignof
- Like
sizeof
, but returned the alignment of the object
- Like
Trivia⌗
Shell command starts with spaces can hide this command from history
A simplified evaluation order of shell (This is derived from one of our recent projects: Develop a Shell in Java)
- Command substitution is performed (see command substitution);
- The command is split into arguments based on whitespaces
- Globbing is performed
- The application is called
Commonly used shortcuts for oh-my-zsh
git
plugin- Cheatsheet
ga = git add
,gaa = git add -all
gb = git branch
,gbd = git branch -d
,gbD = git branch -D
gcb = git checkout -b
,gco = git checkout
,gd = git diff
gf = git fetch
,gfo = git fetch origin
gl = git pull
gp = git push
,gpf = git push --force-with-lease
,gpf! = git push --force
gm = git merge
gr = git remote
,gra = git remote add
grb = git rebase
,grba = git rebase --abort
,grbc = git rebase --continue
,grbi = git rebase -i
git reset
different levels (soft
,mixed
,hard
) 2time
command 3- Real is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).
- User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.
- Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like ‘user’, this is only CPU time used by the process.
https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size “cppreference std::hardware_destructive_interference_size, std::hardware_constructive_interference_size” ↩︎
https://stackoverflow.com/a/3528483 “mkarasek’s answer: What’s the difference between git reset --mixed, --soft, and --hard?” ↩︎
http://zch051383471952.blogspot.com/2010/01/different-of-real-user-sys-time.html “DIFFERENT OF REAL USER SYS TIME” ↩︎