Skip to content

Instantly share code, notes, and snippets.

@arrieta
arrieta / async_server.py
Created March 3, 2018 14:18
Asynchronous Server Basic Example
"""
The server code is based entirely on D. Beazley's talk "Topics of Interest (Python Asyncio)"
given in 2015 at the Python Brazil conference.
http://pyvideo.org/python-brasil-2015/keynote-david-beazley-topics-of-interest-python-asyncio.html
The server can handle 60,000 requests in 2.020 seconds (29,696 requests/second) in a ~2015 macOS.
"""
@arrieta
arrieta / mt-log.cpp
Created March 4, 2018 00:17
An approach to multithreaded logging to an existing buffer
#include <algorithm>
#include <atomic>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
#include <iterator>
#include <chrono>
@arrieta
arrieta / SecureMemoryRegion.cpp
Created October 15, 2017 22:24
Provide a memory region that never swaps to disk.
// -*- coding:utf-8; mode:c++; mode:auto-fill; fill-column:80; -*-
/// @file SecureMemoryRegion.cpp
/// @brief Provide region of memory that never swaps to disk.
/// @author J. Arrieta <Juan.Arrieta@nablazerolabs.com>
/// @date October 15, 2017
/// @copyright (c) 2017 Nabla Zero Labs
///
/// $ clang++ -o SecureMemoryRegion SecureMemoryRegion.cpp -std=c++1z \
/// -Wall -Wextra -Ofast -march=native
@arrieta
arrieta / awareness.cpp
Created September 19, 2018 14:57
C++ Translation of Go Code
// I am interested in practical comparisons between C++ and Go. To this end, I
// find "good" Go code (by some definition of "good") and translate it into C++.
//
// It is mostly unimportant what the code does (in fact, it does not do anything
// at all). What matters is to attempt to capture the same API and semantics.
//
// This file implements a class called "Awareness" found in the hashicorp repo:
//
// https://github.com/hashicorp/memberlist/blob/master/awareness.go
//
@arrieta
arrieta / check-rsa-signature.go
Created September 22, 2018 02:07
Check RSA Signature (golang)
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"fmt"
@arrieta
arrieta / egg_drop.py
Created November 29, 2017 19:08
General Egg Drop Problem (Dynamic Programming)
# egg_drop.py
#
# Solution to the general egg drop problem via:
#
# 1. A recursive-memoized algorithm
# 2. A bottom-up table construction
#
# (C) 2017 J. Arrieta, Nabla Zero Labs
# MIT License.
@arrieta
arrieta / non-owning-ts-cache.cpp
Created October 6, 2018 22:12
C++ Non-Owning Thread-Safe Object Cache
#include <chrono>
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
// In practice these would be actual types instead of aliases.
using Id = int;
@arrieta
arrieta / string_view_and_arguments.cpp
Created October 23, 2018 16:24
Experiments with std::string_view, argument dispatching, and stuff like that.
/// Experiments with string view, dispatching, and stuff like that.
/// J. Arrieta
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <locale>
#include <string>
// POSIX only
#include <sys/stat.h>
@arrieta
arrieta / pds.cpp
Created October 27, 2018 15:56
PDS OBJ Parser Excercise
#include <cctype>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <stdexcept>
#include <string>
inline bool semantic_compare(std::size_t n, const char *value,
const std::string &s) {
@arrieta
arrieta / pool_of_T.cpp
Created November 3, 2018 18:21
Aligned Object Pool
#include <cassert>
#include <iostream>
#include <memory>
#include <type_traits>
#include <vector>
template <typename T>
class Pool {
public:
using Block = typename std::aligned_storage<sizeof(T), alignof(T)>::type;