-
Stack optimization for small sized objects in modern C++
I came across a popular technique for providing a handle for storing small objects in the handle itself and larger ones on the heap. Using modern C++, this can be implemented quite nicely at compile time. Here is a simple example:
// max bytes to store on the stack constexpr int on_stack_max = 20; template<typename T> struct Scoped { // store a T in Scoped // ... T obj; }; template<typename T> struct OnHeap { // store a T on the free store // ... T* objp; }; template<typename T> using Handle = typename std::conditional<(sizeof(T) <= on_stack_max), Scoped<T>, // first alternative OnHeap<T> // second alternative >::type; void f() { Handle<double> v1; // the double goes on the stack Handle<std::array<double, 200>> v2; // the array goes on the free store }Let’s break this down
constexpr int on_stack_max = 20;: This line defines a constant expression for the maximum number of bytes that can be stored on the stack.template<typename T> struct Scoped { T obj; };: This is a template struct that can store an object of any type T on the stack.template<typename T> struct OnHeap { T* objp; };: This is a template struct that can store a pointer to an object of any type T on the heap.template<typename T> using Handle = typename std::conditional<(sizeof(T) <= on_stack_max), Scoped<T>, OnHeap<T>>::type;: This line defines a template alias Handle that usesstd::conditionalto decide whether to useScoped<T>orOn_heap<T>. If the size ofTis less than or equal toon_stack_max, it usesScoped<T>. Otherwise, it usesOn_heap<T>.void f() { Handle<double> v1; Handle<std::array<double, 200>> v2; }: This function demonstrates how to use the Handle template.v1is a Handle that stores a double on the stack, because the size of a double is less thanon_stack_max.v2is a Handle that stores anstd::array<double, 200>on the heap, because the size ofstd::array<double, 200>is greater thanon_stack_max.
Of course, this assumes that
Tcan be copied and moved around, and that it has a finite size. IfTis not copyable or movable, you will need to adjust the implementation accordingly.This shows how powerful modern C++ can be in terms of compile-time programming. It allows you to make decisions at compile time based on the properties of types, which can lead to more efficient and flexible code.
Read more -
Dive into Python asyncio - part 2
In the second part of this series on deep diving into
asyncioandasync/awaitin Python, we will be looking at the following topics:- task, task groups, task cancellation
- async queues
- async locks and semaphores
- async context managers
- async error handling
-
Dive into Python asyncio - part 1
For as long as I have worked in Python land, I never had to touch the async part of the language. I know that
asynciolibrary has gotten a lot of love in the past few years. Recently I’ve came across an opportunity to do a lot of IO and non-cpu bound work in Python. I decided to take a deep dive into theasynciolibrary and see what it has to offer.In part 1 of this series (I originally just wanted to write one post and realized the scope is way too big), we’ll cover:
- How async code interfaces with synchronous code in Python
- How to convert synchronous code to asynchronous code, including how to prevent blocking of the event loop via custom
ThreadPoolExecutor - How to use
asyncioto run multiple tasks concurrently
Basic example, async hello world
import asyncio async def hello_world(): asyncio.sleep(1) print("Hello world") asyncio.run(hello_world()) >>> Hello worldRunning two async functions in parallel
import asyncio async def foo(): while True: asyncio.sleep(1) print("foo") async def bar(): while True: asyncio.sleep(1) print("bar") asyncio.run(asyncio.gather(foo(), bar()))What if I have existing synchronous methods?
We can wrap a synchronous function in an async function, an example implementation would be a decorator (i love decorators, btw):
def async_wrap( loop: Optional[asyncio.BaseEventLoop] = None, executor: Optional[Executor] = None ) -> Callable: def _async_wrap(func: Callable) -> Callable: @wraps(func) async def run(*args, loop=loop, executor=executor, **kwargs): if loop is None: loop = asyncio.get_event_loop() pfunc = partial(func, *args, **kwargs) return await loop.run_in_executor(executor, pfunc) return run return _async_wrapThe above decorator is a higher order decorator (it takes arguments and then generates another decorator), example usage is the following:
Read moreimport asyncio import time @async_wrap() def foo(): while True: time.sleep(1) print("foo from sync") async def bar(): while True: asyncio.sleep(1) print("bar from async") asyncio.run(asyncio.gather(foo(), bar())) -
What is copiable?
What is copiable anyway?
Python is garbage collected and has a reference counting system. This means that when you create an object, it is stored in memory and a reference to it is stored in a variable. When you assign a variable to another variable, the reference count for the object is incremented. When you delete a variable, the reference count is decremented. When the reference count reaches zero, the object is deleted from memory.
This is a very simple explanation of how Python works. There are many more details that I will not go into here. The point is that when you assign a variable to another variable, you are not creating a copy of the object. You are creating a new reference to the same object. This is important to understand because it can lead to some unexpected behavior.
Questions I had:
- What happens when you assign a variable to another variable?
- What happens when you return a complex object (i.e. a class) as part of a tuple from a function?
- What happens when you spin up a subprocess, call a method you defined in one class, and give it an object as an argument?
-
Dependency injection in Python
Since Python type hints are introduced, they have made complex Python code-bases much more readable and easier to maintain - especially combined with newer static analysis tools such as
mypyorpylint. However, even with these tools, Python is still a dynamic language. When using a dynamic language on a larger application (>5k LOC), the ability to do whatever we wanted any where and any time is more of a curse than a blessing.In this post, I wanted to discuss several options of implementing loosely coupled code in large Python codebases that I have played around with and the final solution of dependency injection based pattern I ended up deciding on.
Read more -
Javascript oddities
A collection of weird things in Javascript:
1.
varscoping rulesfor (var i = 0; i < 3; ++i) { const log = () => { console.log(`a ${i}`); } setTimeout(log, 100); } for (let i = 0; i < 3; ++i) { const log = () => { console.log(`b ${i}`); } setTimeout(log, 100); }The output here is:
"a 3" "a 3" "a 3" "b 0" "b 1" "b 2"Why does
varcause it to print 3?2.
constin Javascript does not mean the same as C/C++. Example:const value = 3; value = 4; // error, cannot override a constant value += 3; // error const obj = {a : 3}; obj.a += 3; //allowed obj.a = 5; //allowedTurns out
constin Javascript is more of a “const” reference likeconst &in C++. It does not mean the value itself is constant - just the reference to the array cannot be changed.3. Converting time formats can be tricky
Suppose you have a time in
yyyy-mm-DDformat and you want it inmm/DD/yyyyformat.new Date('2016-06-05'). toLocaleString('en-us', {year: 'numeric', month: '2-digit', day: '2-digit'}) // Output: >>> '06/04/2016'Wait, what happened?, I asked for 2016-06-05 in
mm/dd/YYYYbut it gave me06/04/2016instead! This because all dates by default assumes it’s GMT time, when you convert it to a local timezone, you might get a different date.The
momentlibrary fortunately makes this a lot easier.var date = new Date('10/01/2021'); var formattedDate = moment(date).format('YYYY-MM-DD');If we don’t want some extra dependency, it’s probably easier to just not convert the date into a Javascript
Dateobj and directly do string operations on it to get it to the format you want. Example:Read morefunction reformatDateString(dateString) { //reformat date string to from YYYY-MM-DD to MM/DD/YYYY if (dateString && dateString.indexOf('-') > -1) { const dateParts = dateString.split('-'); return `${dateParts[1]}/${dateParts[2]}/${dateParts[0]}`; } return dateString; } -
Rust like enums in C++
While browsing the excellent modern C++ reactive console UI library FTXUI, I noticed this piece of code in how the events are typed/handled.
// event.hpp struct Event { // --- Constructor section --------------------------------------------------- static Event Character(char); static Event CursorReporting(std::string, int x, int y); // Other constructor methods // --- Arrow --- static const Event ArrowLeft; static const Event ArrowRight; static const Event ArrowUp; static const Event ArrowDown; // .... Other definitions etc....My immediate reaction to this is that this feels weird - partially because I am not used to the author’s C++ style, in addition, the combination of static member variables sharing the parent-type, and static methods for constructors are really confusing to read.
Let’s dive into it to see how things work.
Read more -
Python testing ecosystem
-
Common, stupid, but non-obvious C++ mistakes I made
Internet consensus tends to label C++ as a hard language; I like to think Cpp is a “deep” language. There are always rooms for improvement - doesn’t matter how long you have been coding in C++. The expressiveness and the depth of the language is double-edged. It is what makes C++ great, but also makes it daunting for new users. These are the mistakes I’ve made in my daily usage of C++. I hope they can be useful for other people to avoid them in the future.
1. Capture by reference on transient objects
Callbacks (lambda functions, function pointers, functors, or
Read morestd::bindon static functions) are a common paradigm when you work with message queues, thread pools, or event based systems. Lambda and closures give you a lot of power - but too much power could often cause problems, consider the following code: -
Rich D3 interactivity in Jekyll posts
This is me trying to reproduce the results in this Stack Overflow post and Dan Cole’s blog.
Things to keep in mind:
- Include morley.csv (Search local JavaScript for
/morley.csv) - Include D3.js
- Include box.js
- Include the local CSS and JavaScript
Check out the code for this post on GitHub.
Read more - Include morley.csv (Search local JavaScript for
-
Maximize the luck surface area
Hacker News recently had a post about “maximizing luck surface area”. Jason Roberts made a point that seemingly random success that people experience that most people attributes to luck, is really a game of probability.
While most people follow the axiom of doing “good work” by spending years going through school, honing their craft, and then practicing those crafts and skills to perfection at work (cough, programming languages, frameworks, design patterns, algorithms, etc.) Many people neglects the other important pre-requisites to success - letting others know about your work and building up the reputation for network effects.
Read more
-
Using an Apple Silicon Macbook for C++/Python development
The newest and latest (late-2020) Apple Macbook Air and Macbook Pro 13” with Apple Silicon has been out on the market for a while now. Recently, I had the priviledge of getting a Macbook Air with M1 chip as as a dev machine to test various things (c++, python, jupyter notebook) that I use.
From the Youtube reviews (MKBHD) and various benchmarking websites (Toms’ Hardware, Daring Fireball), I already know that the performance of the M1 chips is really impressive at the given power consumption; and that people are getting ridiculous battery lifes on their M1 Macs. However, as someone that intends to use the Mac as a development daily driver, my main concern is how my workflow and toolchain will work with the new Apple Silicon and ARM64 instruction set. After a few weeks of tinkering and exploration, I think it is safe to say that Apple has done an incredible job to ensure smooth transition to the new hardware, and that AMD/Intel/Microsoft or any other hardware manufacturer should be shitting their pants right now for the on-slaught that’s about to come.
Read more -
Passing by const reference
While working in any large C++ project, we often deal with having to write small utility functions that takes in some temporary and then perform some operation on it to return a transformed variable. For example
std::string append_path(const std::string& basepath, const std::string& child_path) { return basepath + "/" + child_path; } // Invoking this function: const std::string basepath = "/var/tmp"; const std::string text_file = "some_file.txt"; const std::string new_path = append_path(basepath, text_file); // this works const std::string other_new_path = append_path(basepath, "some_other_file.txt"); // this also worksDoes this seems strange to you? We are requiring the variable to be passed in by reference. The
"some_other_file.txt"argument in the 2nd invocation of the argument is a “r-value”, and we are able to refer to the content of this r-value string as a reference to some variable.It turned out, as Herb Sutter explained it here:
The C++ language says that a local const reference prolongs the lifetime of temporary values until the end of the containing scope, but saving you the cost of a copy-construction (i.e. if you were to use an local variable instead).
So, effectively, the r-value life-time is extended to the function scope when it is being invoked as an argument to a function immediately after its definition.
Now I can rest easy, knowing that the behavior of my program won’t depend on how aggressive the compilers optimize or reuse the memory of variables that are no longer considered “needed” by scoping rules.
Read more -
Abstraction All the Way Down
Programming or any problem solving skill is really all about distilling complex problems into simpler abstractions. The art of creating elegant abstractions is a skill that developers must acquire on their journey to master software craftsmanship. Terse abstractions that fully describes a problem helps reduce cognitive burden, allowing the developer to focus on what really matters in the code.
Read more -
Embedding icon into GUI executables for MacOS / Windows via cmake
CMake while being arcane and quirky in its own way, is currently the standard cross-platform compilation platform for C++/C build systems. I think the power of CMake comes from all the community contributions and the knowledge base built up over the years.
I recently worked on a project to migrate it from an archaic Perl build system into the land of CMake. Following modern CMake practices really made dependency management a breeze (i.e. target based compilation flag specification, target based linking, transient header propagation via INTERFACE/PUBLIC/PRIVATE, generator expressions to optionally specify different compiler warning / optimization levels)
One of the things that took a little while for me to figure out is how to embed a GUI based application an application icon that is in a cross-platform friendly method. I googled around and really didn’t come across a simple enough solution, so I decided to roll my own.
Read more