Skip to content

Instantly share code, notes, and snippets.

View mmomtchev's full-sized avatar

Momtchil Momtchev mmomtchev

View GitHub Profile
@mmomtchev
mmomtchev / gcc.md
Last active June 20, 2024 11:24
fibonacci-gcc-results with unwinding
test function size execution time
cc-O0-no_unwind 242 bytes 181.44
cc-O0-unwind_dontcare 274 bytes 216.39
cc-O0-unwind_noexcept 230 bytes 215.46
cc-O0-unwind_throwing 353 bytes 215.35
cc-O0-unwind_catching 385 bytes 215.07
cc-O2-no_unwind 108 bytes 25.74
cc-O2-unwind_dontcare 132 bytes 26.05
cc-O2-unwind_noexcept 108 bytes 25.78
@mmomtchev
mmomtchev / fibonacci-with-destruction.cc
Last active June 20, 2024 11:12
Gists for the C++ exceptions medium story
#include <stdexcept>
#include <stdio.h>
static int objects;
class IntWithDestructor {
int val;
public:
IntWithDestructor() : val{} { objects++; }
@mmomtchev
mmomtchev / gcc.md
Created June 19, 2024 15:57
fibonacci-gcc-results -O3
test function size (bytes) execution time (seconds)
test-cc-O3-no_unwind 679 bytes 21.30
test-cc-O3-unwind_catching 723 bytes 19.11
test-cc-O3-unwind_dontcare 679 bytes 21.28
test-cc-O3-unwind_noexcept 679 bytes 21.29
test-cc-O3-unwind_throwing 747 bytes 21.30
test-c-O3 679 bytes 21.27
@mmomtchev
mmomtchev / gcc.md
Created June 19, 2024 15:49
fibonacci-gcc-results -O2
test function size (bytes) execution time (seconds)
test-cc-O2-no_unwind 679 bytes 21.31
test-cc-O2-unwind_catching 172 bytes 29.78
test-cc-O2-unwind_dontcare 679 bytes 21.30
test-cc-O2-unwind_noexcept 679 bytes 21.36
test-cc-O2-unwind_throwing 159 bytes 23.05
test-c-O2 679 bytes 21.29
@mmomtchev
mmomtchev / gcc.md
Last active June 19, 2024 16:27
fibonacci-gcc-results -O0
test function size (bytes) execution time (seconds)
test-cc-O0-no_unwind 91 bytes 55.88
test-cc-O0-unwind_catching 211 bytes 68.59
test-cc-O0-unwind_dontcare 89 bytes 54.04
test-cc-O0-unwind_noexcept 89 bytes 55.92
test-cc-O0-unwind_throwing 176 bytes 62.99
test-c-O0 89 bytes 54.32
@mmomtchev
mmomtchev / fibonacci.cc
Last active June 19, 2024 15:37
Gists for the C++ exceptions medium story
#include <stdexcept>
#include <stdio.h>
static int result;
int fibonacci(int i) {
// Optimize the branch prediction
if (i <= 1) [[unlikely]] {
if (i < 0)
throw std::runtime_error{"Negative number"};
// Does std::logic_error copy the string
// or takes a reference?
//
// The C++ specifications are deliberately vague
//
// With MSVC, try compiling this with 1 and with 0
// You will be very surprised
//
#define _HAS_EXCEPTIONS 1
#include <stdexcept>
@mmomtchev
mmomtchev / transform.js
Created March 18, 2024 16:40
Crude transformer for bison grammars to use named references
/**
* Transform a single bison item to named references
*
* For example to transform only "c_decl"
*
* node transform.js Source/CParse/parser.y c_decl Source/CParse/parser.y
*/
const fs = require('fs');
@mmomtchev
mmomtchev / msvc-exception-lambda-bug.cc
Last active September 8, 2023 22:15
msvc-exception-lambda-bug
#include <Magick++.h>
#include <exception>
#include <functional>
#include <string>
typedef std::function<const char *()> MakeErr;
class LambdaException : public std::exception {
MakeErr get_err;
@mmomtchev
mmomtchev / std_exception.cc
Created August 16, 2023 15:00
Copying `std::exception` with an error message
#include <iostream>
int main() {
auto err = std::logic_error("some error");
std::cout << err.what() << std::endl;
// using the derived copy constructor
std::logic_error err2 = err;
std::cout << err2.what() << std::endl;