Home » Posts tagged "c"

Windows 上的 Chrome 改用 Clang 編譯

這應該是上個禮拜蠻熱鬧的一件事情 (i.e. 很多人看熱鬧 XD),就是 Google ChromeVisual C++ 改成用 Clang 編譯:「Clang is now used to build Chrome for Windows」。


We conducted extensive A/B testing of performance. Performance telemetry numbers are about the same for MSVC-built and clang-built Chrome – some metrics get better, some get worse, but all of them are within 5% of each other.

後面有猜測換過去的原因,可以看出因為 open source 加上 Google Chrome 團隊有強大的技術能力下,用 open source 軟體可以對 compiler 大量客製化各種功能,另外也是因為一個 compiler 就可以吃多個平台,可以省下一些跨平台的力氣 (像是相容性語法)。

而 Visual C++ 在商業支援與文件兩方面比較好的優勢,在這個情況下就顯得不是那麼重要了...

Twitter 放出來的 Vireo,一套 Open Source 授權的 Video Processing Library

Twitter 放出 Vireo,一套以 MIT License 釋出的 Video Processing Library:「Introducing Vireo: A Lightweight and Versatile Video Processing Library」。專案庫在 GitHubtwitter/vireo 可以取得。

C++ 寫的,另外也已經提供 Scala 的接口,這應該是讓 Twitter 的人可以方便使用:

Vireo is a lightweight and versatile video processing library that powers our video transcoding service, deep learning recognition systems and more. It is written in C++11 and built with functional programming principles. It also optionally comes with Scala wrappers that enable us to build scalable video processing applications within our backend services.

在 Tools 的部份也可以看到很多功能,像是:

thumbnails: extracts keyframes from the input video and saves them as JPG images

viddiff: checks if two video files are functionally identical or not (does not compare data that does not affect the playback behavior)

另外要注意的是,預設不會將 GPL 的套件納入編譯,需要指定 --enable-gpl 才會編進去:

The following libraries are disabled by default. To enable GPL licensed components, they have to be present in your system and --enable-gpl flag have to be explicitly passed to configure

看起來主要就是最常見的那包... (libavformat / libavcodec / libavutil / libswscale / libx264)

GitHub 上有大量重複的程式碼...

扣除掉 fork 的程式碼後,研究人員在 GitHub 上還是發現有大量重複的程式碼:「DéjàVu: a map of code duplicates on GitHub」。

This paper analyzes a corpus of 4.5 million non-fork projects hosted on GitHub representing over 482 million files written in Java, C++, Python, and JavaScript. We found that this corpus has a mere 85 million unique files.

Java/C++/Python/JavaScript 寫的 4.5M 個專案有 482M 個檔案,但只有 85M 個檔案是不一樣的 XD

想一想其實也是... 現在愈來愈多工具產生程式碼了 XD (i.e. Scaffold)

C++ 與組語的速度...

Hacker News Daily 上看到「Why is this C++ code faster than my hand-written assembly for testing the Collatz conjecture?」覺得很有趣...

作者寫了一段 assembly,但跑起來比用 C++ 同義的版本慢多了。目前最高分的答案給了很清楚的解釋...

    mov rbx, 2
    xor rdx, rdx
    div rbx

上面這段 code 是作者寫的組語版本,用到 div 指令,這是非常慢的指令:

On Intel Haswell, div r64 is 36 uops, with a latency of 32-96 cycles, and a throughput of one per 21-74 cycles.

相較於 C++ 的版本,用到的是 shr (logical shift right,以位元方式往右平移,最高位補零),速度快太多:

shr rax, 1 does the same unsigned division: It's 1 uop, with 1c latency, and can run 2 per clock cycle.

這是用到無號整數透過 shr 平移一格剛好是除以二的特性,因為速度的關係,這個用法到現在還是很常被拿來用,但對於平常沒在寫 assembly 的人就會有上面的誤解 XDDD

各家 glob 的效能...

在「Glob Matching Can Be Simple And Fast Too」這邊看到在分析 (a.*)nb 這樣的 pattern 的效能 (像是 a.*a.*a.*b 這樣的東西),第一波先測 shell,結果發現有趣的現象:

那個 csh 是怎麼了 XDDD

Looking at the source code, it doesn’t attempt to perform glob expansion itself. Instead it calls the C library implementation glob(3), which runs in linear time, at least on this Linux system. So maybe we should look at programming language implementations too.


各家實做方式不一樣 XD

然後文章裡有提到這個方式是之前蠻常見的 DoS 技巧,用很少的資源就可以吃光你的 CPU resource... 另外也提到了可能的解法,就是限制星號的數量:

At the very least, most FTP servers should probably reject glob patterns with more than, say, 3 stars.


Tor 在考慮使用 Rust 改寫

不過也不確定是不是愚人節消息就是了:「[tor-dev] Tor in a safer language: Network team update from Amsterdam」。

Tor 考慮使用 Rust 改寫,目前已經完成的部份,以及接下來的規劃:

What has already been done:
- Rust in Tor build
- Putting together environment setup instructions and a (very small) initial draft for coding standards
- Initial work to identify good candidates for migration (not tightly interdependent)

What we think are next steps:
- Define conventions for the API boundary between Rust and C
- Add a non-trivial Rust API and deploy with a flag to optionally use (to test support with a safe fallback)
- Learn from similar projects
- Add automated tooling for Rust, such as linting and testing

目前看到後續的討論只有「[tor-dev] Tor in a safer language: Network team update from Amsterdam」這篇,也許等全世界的 4/1 都過了之後再回來確認吧...

OpenBSD 將 ACME Client (Let's Encrypt Client) 納入系統

看到 OpenBSD 直接把 ACME 協定的 client 放進系統內,而 ACME 也就是 Let's Encrypt 所使用的協定:「Let's Encrypt client imported into -current」:

Module name:src
Changes by:florian@cvs.openbsd.org2016/08/31 16:01:42

Added files:
usr.sbin/acme-client: ChangeLog Makefile acctproc.c base64.c 
                      certproc.c chngproc.c dbg.c dnsproc.c 
                      extern.h fileproc.c http.c http.h jsmn.c 
                      jsmn.h json.c keyproc.c letskencrypt.1 
                      main.c netproc.c revokeproc.c rsa.c rsa.h 
                      sandbox-pledge.c util-pledge.c util.c 

Log message:
Import Kristaps' letskencrypt and call it acme-client in tree.
OK to get it in deraadt@ (and probably beck@)

At least deraadt@, beck@ and otto@ are fine with the name and the
disagreements stopped.

用的是 acme-client,先前叫做 letskencrypt,以 C 開發的 ACME client。

在 C 裡 Concurrency 的 Library

看到「libdill: Structured Concurrency for C」這個東西,在 C 裡實作了兩個不同種類的 concurrency,一個是 proc (process-based) 一個是 go (corouting-based)。

支援的 function 算是蠻清晰的,範例也很清楚:

#include <libdill.h>
#include <stdio.h>
#include <stdlib.h>

coroutine int worker(const char *text) {
    while(1) {
        printf("%s\n", text);
        msleep(now() + random() % 500);
    return 0;

int main() {
    msleep(now() + 5000);
    return 0;

也有 channel 的觀念可以用,之後需要寫玩具的話應該是個好東西...

因為一條 USB 線燒了三個設備...

Benson Leung 花了不少時間在測試 USB 線 (「Google 工程師在 Amazon 上留下對 USB-C 線的精彩評價...」與「Google 工程師在 Amazon 上對 Type C USB 線的評論」),而前陣子他測到一條超棒的 USB 線,把他三個測試設備都燒壞了:(出自這邊)

Surjtech's A-to-C cable seriously damaged a Pixel 2 laptop and two USB PD analyzers.

他在 Amazon 上留下的說明可以看到他發現廠商直接把 GND 與 Vbus 的線接錯了 (這個有厲害...):

I directly analyzed the Surjtech cable using a Type-C breakout board and a multimeter, and it appears that they completely miswired the cable. The GND pin on the Type-A plug is tied to the Vbus pins on the Type-C plug. The Vbus pin on the Type-A plug is tied to GND on the Type-C plug.


I've had the cable taken apart, and we've discovered some interesting details. I'll post a post with pictures soon.

1) Vbus and Gnd are switched. Red wire goes to G on the Type-C's PCB, Black wire goes to V.
2) 10 kΩ resistor instead of 56 kΩ resistor used.
3) resistor hooked up as a Pull-down instead of a pull-up
4) Wire is COMPLETELY missing SuperSpeed wires. It is NOT actually a USB 3.1 cable, even though it has a blue connector on the A side and SuperSpeed logos.

Bad. So bad.

照片可以在這邊看到。看起來 Benson Leung 得再去 Google 內要新的機器來測試了...

Android NDK 宣佈向 Clang 靠攏...

Hacker News Daily 上看到 Android NDK 宣佈向 Clang 靠過去的消息:「Changelog for NDK Build 2490520」。

Android NDK 做為效能的加速手段而使用到 C 或是 C++,所以會使用對應的 compiler suite:

The NDK is a toolset that allows you to implement parts of your app using native-code languages such as C and C++. Typically, good use cases for the NDK are CPU-intensive applications such as game engines, signal processing, and physics simulation.


PSA: Everyone should be switching to Clang.


GCC in the NDK is now deprecated.