Skip to content

Instantly share code, notes, and snippets.

View htfy96's full-sized avatar
🐢
WFH

Zheng Luo htfy96

🐢
WFH
View GitHub Profile
@htfy96
htfy96 / static_inline_example.md
Last active April 11, 2024 14:13
static inline vs inline vs static in C++

In this article we compared different behavior of static, inline and static inline free functions in compiled binary. All the following test was done under g++ 7.1.1 on Linux amd64, ELF64.

Test sources

header.hpp

#pragma once

inline int only_inline() { return 42; }
static int only_static() { return 42; }
@htfy96
htfy96 / cpp-build-profile-2024.md
Last active April 2, 2024 16:48
C++ Build profiles for 2024 projects

C++ Build profiles for 2024 projects

Standard development profile

This profile achieves 50% - 80% release profile performance, while also provides a reasonable amount of safety checks and debugging support. This should also be the profile for your CI build.

Compilation flags

-Og -Wall -Wextra -D_FORTIFY_SOURCE=2 -fstack-protector-strong -g -D_GLIBCXX_ASSERTIONS
@htfy96
htfy96 / bookmarklet.md
Last active March 16, 2024 07:19
Simple bookmarklet to send current webpage to dyweb/weekly

Add the following target to your bookmark bar.

Remember to replace %24YOUR_TOKEN_HERE with your Github personal access token. The token could be acquired at Settings -> Developer Settings -> Personal Access Token and check public_repo permission.

javascript:(function()%7Bconst%20apiRequest%20%3D%20(src%2C%20param)%20%3D%3E%20fetch('https%3A%2F%2Fapi.github.com%2F'%20%2B%20src%2C%20Object.assign(%7Bheaders%3A%20%7B%20Accept%3A%20'application%2Fvnd.github.v3%2Bjson'%2C%20'Content-Type'%3A%20'application%2Fjson'%2C%20Authorization%3A%20'token%20%24YOUR_TOKEN_HERE'%20%7D%7D%2C%20param))%3BapiRequest('repos%2Fdyweb%2Fweekly%2Fissues').then(v%20%3D%3E%20v.json()).then(d%20%3D%3E%20d.filter(item%20%3D%3E%20item.labels.some(lab%20%3D%3E%20lab.name%20%3D%3D%3D%20'working'))%5B0%5D.number).then(issuenum%20%3D%3E%20%7Bconst%20url%20%3D%20'repos%2Fdyweb%2Fweekly%2Fissues%2F'%20%2B%20issuenum%20%2B%20'%2Fcomments'%3Bconst%20description%20%3D%20window.prompt('Please%20input%20your%20description%20of%20this%2
@htfy96
htfy96 / cdq.cpp
Last active March 3, 2024 20:03
Solution to CF Educational Round 56 Problem E, with CDQ divide-and-conquer (CDQ分治)
#include <bits/stdc++.h>
/*
https://codeforces.com/contest/1093/problem/E
Convert all modification and query into single-point events (t, x, y). Initially sort by t,
for each interval, recursively handle independent two parts. Then calc the interference
between part one and two: only mod in part 1 can affect result in part 2. Therefore, we can
sort part1 and 2 respectively by x, then process all mod event in part 1 and all query events in
part 2 in a sort-merge-like way. The Y-dimension is maintained with a fenwick tree.
In the end all elements will be sorted by x.
@htfy96
htfy96 / README.md
Last active July 25, 2022 17:43
Restrict C++ unsafe behaviors in unsafe{} block using clang-query and simple commands

C++ has a lot of dark corners. Unfortunately, sometimes we need to allow inexperienced developers to write some C++ code to meet the deadline. The intersection of the two cases often makes things worse: programmers used to delegate memory management to garbage collection tend to throw off new everywhere in the source, and those stuck with compile error will use every evil hack to get around with it. Code review is a feasible way to ensure code quality in this case, but a better choice is to restrict them into a relatively safe subset of the language.

In this article, I will show how to use clang-query and a simple script to restrict some unsafe behaviors in unsafe block/namespace using simple commands:

#include "common.hpp"

struct X {
    int f: 2; // error: use of bit field without enclosing Unsafe
};
@htfy96
htfy96 / feeds.opml
Created June 27, 2018 05:16
My feedly feeds collection
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
<title>Vic subscriptions in feedly Cloud</title>
</head>
<body>
<outline text="news" title="news">
<outline type="rss" text="路透: 时事要闻" title="路透: 时事要闻" xmlUrl="http://cn.reuters.com/rssFeed/CNTopGenNews/" htmlUrl="http://cn.reuters.com"/>
<outline type="rss" text="Washington Post: Breaking News, World, US, DC News &amp; Analysis" title="Washington Post: Breaking News, World, US, DC News &amp; Analysis" xmlUrl="http://www.washingtonpost.com/rss/homepage" htmlUrl="http://www.washingtonpost.com/pb/homepage/"/>
@htfy96
htfy96 / osuperfast.md
Last active January 4, 2021 09:41
-Osuperfast: gcc optimization flags used in our project
CXXFLAGS="-ffast-math -fdevirtualize-speculatively  -fassociative-math -freciprocal-math -fno-signed-zeros -fno-trapping-math -fipa-pta -mrecip=all -fgcse-sm -fgcse-las -fgcse-after-reload -march=native  -ftree-vectorize -floop-interchange -floop-block -ftree-coalesce-vars -floop-nest-optimize -fgraphite-identity -ftree-loop-ivcanon -fivopts" LDFLAGS="-ffast-math -fdevirtualize-speculatively"

Benchmark

Basic O3 + -march=native

rendering time = 91ms

Basic O3 + -march=native + -ffast-math + -mrecip=all

rendering time = 92ms

set guiheadroom=-20
set shell=/bin/bash
if &term =~ '^\(xterm\|screen\)$' && $COLORTERM == 'gnome-terminal'
set t_Co=256
endif
filetype on
filetype plugin on
set autoindent
set nolazyredraw
set number
@htfy96
htfy96 / how-to-delete-easy.md
Last active June 21, 2020 23:26
How to delete a bunch of files on server without interrupting other workload in 2020

What doesn't work

ionice

Two reasons:

  • Common answer: Many modern OS doesn't use BFQ scheduler, and deadline-related schedulers ignore ionice: check this with cat /sys/block/sda/queue/scheduler
  • In kernel, the IO on writeback pages isn't accounted to the actual process until cgroup v2 OR blkio scheduler. So, if you deleted some files which updates the dentry page cache of parent directory, the writeback page cache won't be accounted to the user and thus stay unlimited

What does work

tl;dr:

@htfy96
htfy96 / install_dep_and_build
Created December 5, 2019 18:32
Install conan packages and build with cmake_multi generator
#!/bin/bash
# Usage:
# Assuming you are in ROOT/build
# and ROOT contains conanfile.txt and CMakeLists.txt
# ./install_dep_and_build [Debug|Release|RelWithDebInfo]
# Prerequisite:
# Make sure you have
# [generators]