0%

我對Rust也是初學者的狀態,以下是從A 30 minute introduction to Rust看來的,既然Rust被定位成System programming language,難免要拿來跟C++比一比。看看Ruat友什麼特別之處。

The power of ownership

在C/C++,很簡單可以寫出這樣的程式碼。

1
2
3
4
5
6
7
8
9
10
int* dangling(void)
{
int i = 1234;
return &i;
}
int add_one(void)
{
int* num = dangling();
return *num + 1;
}
Read more »

由於現在CPU對於Memory Byte Algined的處理較好,所以很多資料結構的擺法都會偏向於4 Byte Alginment的方式進行。例如

1
2
3
4
struct string_t {
int length;
char val[1];
} ;

string_t的大小為8,而非眾人想像中的5,如果要強制讓這個Structure大小為5的話,可以採用以下這種方式。

1
2
3
4
5
6
7
8
9
10
11
12
// GCC Style
struct string_t {
int length;
char val[1];
} __attribute__ ((packed));
// Visual C++ Style
#pragma pack(push,1)
struct string_t {
int length;
char val[1];
} ;
#pragma pack(pop)

如果不用這兩種方式的話,如何使用投機的方式來減少分配

1
2
3
4
5
6
7
8
#include <stddef.h>
struct string_t *allocstr(int len, char *str)
{
struct string_t *string = malloc(offsetof(struct string_t, str) + len + 1);
string->length = len;
memcpy(string->str, str, len);
return string;
}

其實上面那個char val[1]也是不需要的,在支援zero array之後,用這個方式更簡單

1
2
3
4
struct string_t {
int length;
char val[0];
} ;

這篇是Concurrency in C++11的部份筆記,先寫有關C++11標準的部份。
future, promise被加入C++11,大概等同於C#的Task或是Java8的Future

最簡單的情況

以讀寫一個檔案作為範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vector<char> readFile(const string& inPath)
{
ifstream file(inPath, ios::binary | ios::ate);
size_t length = (size_t)file.tellg();
vector<char> buffer(length);
file.seekg(0, std::ios::beg);
file.read(&buffer[0], length);
return buffer;
}
size_t writeFile(const vector<char>& buffer, const string& outPath)
{
ofstream file(outPath, ios::binary);
file.write(&buffer[0], buffer.size());
return (size_t)file.tellp();
}

如果要拷貝一個檔案,可以這樣寫。

1
2
3
4
size_t sync_copyFile(const string& inFile, const string& outFile)
{
return writeFile(readFile(inFile), outFile);
}

這樣寫得問題在於,讀寫都是同步動作。如果檔案一大,就什麼事都不用做了。如何使用Multithread來感善動作。

Read more »

Partion Application

顧名思義,提供函數一部分的參數。變成一個新的函數。
最簡單的例子就像這樣

1
2
3
4
5
6
7
8
int sub(int a, int b)
{
return a - b;
}
int sub2(int v)
{
return sub(v, 2);
}

當然上面這種寫法復用度並不高,於是在C++11把bind跟function納入標準配備之後,可以寫成這樣。

1
std::function<int(int)> sub2 = std::bind(sub, std::placeholders::_1, 2);

有時間再來討論bind跟function。
不過就可讀性來說,Pyhton版的強多了

1
2
3
4
5
import functools
def sub(a, b):
return a - b

sub_two = functools.partial(sub, b = 2)

Currying

Currying是為了解決不一樣的問題
假設一個函數有多個參數,把他轉化成多個只有一個參數的函數組合。
用C++11的Lambda Expression來做示範。

1
2
3
4
5
6
7
std::function<int(int)> add2(int a)
{
return std::function<int(int)>([=](int b) -> int{
return a + b;
});
}
cout << add2(3)(5) << endl;

基本上PCurrying可以實現一部分Partion Application的應用。不過以上麵的sub2為例,Currying在這邊就不是用。
更清楚的描述可以看What is the difference between currying and partial application

不知道哪時候會用到,先做個筆記。需要的話再跟巨觀建字去搜尋。

測試工具

不定期更新

C Language

C++ Language

未分類

Stackoverflow看到這串討論很有意思。
以下的Code,雖然時間複雜度都一樣O(n),不過排序過後的速度遠遠超過沒排序,將sort註解掉之後可以看出很明顯的差異。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <algorithm>
#include <ctime>
#include <iostream>

int main()
{
// Generate data
const unsigned arraySize = 32768;
int data[arraySize];

for (unsigned c = 0; c < arraySize; ++c)
data[c] = std::rand() % 256;

// !!! With this, the next loop runs faster
//std::sort(data, data + arraySize);

// Test
clock_t start = clock();
long long sum = 0;

for (unsigned i = 0; i < 100000; ++i)
{
// Primary loop
for (unsigned c = 0; c < arraySize; ++c)
{
if (data[c] >= 128)
sum += data[c];
}
}

double elapsedTime = static_cast<double>(clock() - start) / CLOCKS_PER_SEC;

std::cout << elapsedTime << std::endl;
std::cout << "sum = " << sum << std::endl;
}
Read more »

寫起來,免得忘記。

在程式Crash前直接呼叫gdb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void dump(int signo)
{
char buf[1024];
char cmd[1024];
FILE *fh;

snprintf(buf, sizeof(buf), "/proc/%d/cmdline", getpid());
if (!(fh = fopen(buf, "r")))
exit(0);
if (!fgets(buf, sizeof(buf), fh))
exit(0);
fclose(fh);
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0';
snprintf(cmd, sizeof(cmd), "gdb %s %d", buf, getpid());
system(cmd);

exit(0);
}
signal(SIGSEGV, &dump);
Read more »

對最近的心得做個總結。
這篇Boost application performance using asynchronous I/O描述了幾種常用的IO Model。
首先要先解釋兩個很像,卻又部太相同的名詞

  • Synchronous Application發起I/O Operation,並且等待其完成 (如 read / write)
  • Non-Synchronous Application僅發起I/O Program Request,由Kernel通知Application完成
  • Blocking Application因為等待某事件,而不能繼續往下執行
  • Non-Blocking Application不受事件影響

Synchronous blocking I/O

最常見的I/O Model,就是在Event未結束前不會將控制權交還給Application。

1
2
int len = recv(s, .....);
// Do other things

在這種Model之下,為了要解決Blocking的問題,就得使用Multi-Porcess或是Multi-Thread的方式來做。

Read more »