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 / 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 / IteratedRun.sol
Last active June 17, 2018 04:42
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=true&gist=
pragma solidity ^0.4.0;
contract IteratedRun {
struct City {
bytes32 name;
bool visited;
}
address owner;
City[] cities;
@htfy96
htfy96 / strong-datas.md
Last active June 2, 2018 14:45
839-strong-datas

2000 strings, all 'aaaaaaaaa'

["aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaa
@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 / bad_rendering_results.md
Last active December 27, 2017 09:19
A collection of my bad rendering results

living_room_1000x1000_0

living_room_1000x1000_0

living_room_1000x1000_0

living_room_1000x1000_0

cs

@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

@htfy96
htfy96 / gdb_cquery.log
Created November 19, 2017 06:40
GDB log for cquery
(gdb) info threads
Id Target Id Frame
* 1 Thread 0x7f7c5e0c3740 (LWP 11427) "querydb" 0x00007f7c5a696756 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
2 Thread 0x7f7c5936a700 (LWP 11428) "stdin" 0x00007f7c598caa98 in read () from /usr/lib/libc.so.6
3 Thread 0x7f7c58b69700 (LWP 11429) "stdout" 0x00007f7c5a696756 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
4 Thread 0x7f7c48368700 (LWP 11430) "completequery" 0x00007f7c5a69143d in pthread_join () from /usr/lib/libpthread.so.0
5 Thread 0x7f7c53fff700 (LWP 11431) "completeparse" 0x00007f7c5a69143d in pthread_join () from /usr/lib/libpthread.so.0
6 Thread 0x7f7c537fe700 (LWP 11432) "indexer0" 0x00007f7c5a696756 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
7 Thread 0x7f7c52ffd700 (LWP 11433) "indexer1" 0x00007f7c5a696756 in pthread_cond_timedwait@@GLIBC_2.3.2 () from /usr/lib/libpthread.so.0
8 Thread 0x7f7c527fc700 (LWP 11434) "indexer2
@htfy96
htfy96 / _GLIBCXX_CONCEPT_CHECKS.md
Created November 4, 2017 06:23
_GLIBCXX_CONCEPT_CHECKS

Introduction

_GLIBCXX_CONCEPT_CHECKS, according to online doc of gcc, is a macro that performs compile-time checking on certain template instantiations to detect violations of the requirements of the standard. This macro has no effect for freestanding implementations.

Example

#include <set>

struct A
{
@htfy96
htfy96 / gnome-shell.css
Created October 8, 2017 12:07
gnome-shell adapta theme css
/* This file is part of adapta-gtk-theme Copyright (C) 2016-2017 Tista <tista.gma500@gmail.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. */
* { transition-property: border-color, border-image, background-color, background-image, box-shadow; transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); transition-duration: 0.15s; }
*:focus { transition-duration: 0s; }
stage { font-size: 10pt; font-family: Roboto, Cantarell, Sans-Serif; font-weight: 400; color: #263238; }
.button { border-color: transparent; color: rgba(38, 50, 56, 0.87); background-color: transparent; box-shadow: 0 0 transparent; text-shadow: none; icon-shadow: none; padding: 7px 16px 8px; border-radius: 2px; border-width: 0; font-weight: 500; }
.button:hover { border-color: transparent; color: #263238; background-color: rgba(38, 50, 56, 0.12); box
@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; }