Asynchronous processing with in-memory databases or how to handle one million transactions per second on a single CPU core

Denis Anikin
Nov 16, 2016 · 10 min read
std::unordered_map
1.cpp
#include <map>
#include <unordered_map>
#include <iostream>
const int SIZE = 1000000;int main()
{
std::unordered_map<int,int> m;
m.reserve(1000000);
long long c = 0;
for (int i = 0; i < SIZE;++i)
c += m[i*i] += i;
std::cout << c << std::endl;
}
g++ -std=c++11 -O3 1.cpp -o 1MacBook-Air-anikin:Downloads anikin$ time ./1
real 0.465s
user 0.422s
sys 0.032s
MacBook-Air-anikin:Downloads anikin$ g++ -std=c++11 1.cpp -o 1MacBook-Air-anikin:Downloads anikin$ time ./1
real 0.883s
user 0.835s
sys 0.033s
MacBook-Air-anikin:Downloads anikin$ uname -aDarwin MacBook-Air-anikin.local 13.4.0 Darwin Kernel Version 13.4.0: Mon Jan 11 18:17:34 PST 2016; root:xnu-2422.115.15~1/RELEASE_X86_64 x86_64
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd = open(“/dev/zero”, O_RDONLY);

for (int i = 0; i < 1000000; ++i)
{
unsigned char c;
if (read(fd, &c, 1) == -1)
{
fprintf(stderr, “error on read\n”);
break;
}
}
close(fd);
return 0;
}
MacBook-Air-anikin:Downloads anikin$ time ./2real 0.639s
user 0.099s
sys 0.495s
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd = open(“/dev/zero”, O_RDONLY);
for (int i = 0; i < 1000; ++i)
{
unsigned char c[1000];
if (read(fd, &c, 1000) == -1)
{
fprintf(stderr, “error on read\n”);
break;
}
}
close(fd);
return 0;
}
MacBook-Air-anikin:Downloads anikin$ time ./2real 0.007s
user 0.001s
sys 0.002s

Denis Anikin

Written by

VP Tech at Citymobil (taxi aggregator)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade