• Sharding: A General Method to Improve Lock Granularity in a Brute-force Way

    Example: Concurrent Hashmap

    Suppose we need a concurrent hashmap. I know there are lots of libraries, but let’s assume we need to write it by ourselves. For simplicity, we only support three kinds of operations: get, put and remove.

    Trivial Solution

    We can use std::unordered_map, but it is not thread-safe. So, the most straightforward solution would be to have a big lock to protect it. It’s simple, just like this:

    Read on →

  • Cython Tutorial

    I have always been loving Python. When people mention Python, they usually think of the following two advantages:

    1. Easy to write
    2. Easy to call C/C++ libraries

    In fact, the first bullet point comes with super slow execution speed. And the second bullet point requires C/C++ libraries to export APIs according to Python specification, which might not be available for most of the libraries.

    During the internship at Tianrang Inc., I had several experiences with Cython. I think although this tool is buggy sometimes and has terrible user experience sometimes, it can boost Python significantly and also help create wraps for C/C++ libraries. In this article, I’d introduce Cython to readers. Notice that, Cython is different from CPython. Cython helps us to conveniently:

    • Use Python syntax to mix code in C/C++ and Python while boosting Python performance
    • Call C/C++ libraries

    Read on →

  • Utilize Multicore Processors with Python

    I used not to care about parallelizing small programs because I thought parallelizing them would cost me more time than writing the programs themselves, and also because there are only four cores on my laptop. Unless the task were IO intensive, I would leave it running with a single thread. Everything has changed since I have access to a machine with 32 cores and 128GB memory. Looking at all those idle cores from htop, I felt the urge to utilize them. And I found it’s super easy to parallel a Python program.

    Read on →

  • Core Dump File inside Docker

    When a program crashes, you’ll see error messages like Segmentation Fault. You might also see (core dumped) as well. I used to feel annoyed when I saw those words because the program crashed. When I was an intern at Tianrang Networking Inc. this summer, I watched my colleague Wengong Jin work on C++ programs. Then realized that I should be glad to see (core dumped) because the core dump file keeps all runtime information at the moment the program crashes, including data in memory, threads, stack traces, registers, and so on. It should be helpful for locating the bug.

    Read on →

  • Is it safer to login with QR code?

    Yesterday I saw a post on Zhihu, the Chinese Quora, asking why some popular websites adopted QR code as the default login method and also the security concerns behind it. I didn’t see an answer that truly answers the question, so I decided to speak out my thoughts.

    If the following reasoning doesn’t interest you, you might also jump to the “Conclusion” section.

    Read on →