Skip to content

Instantly share code, notes, and snippets.

John Calsbeek Nexuapex

Block or report user

Report or block Nexuapex

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View absl_flat_hash_map_latency.txt
// Abseil absl::flat_hash_map
// Analysis of the critical path for perfectly predicted load hit.
// https://godbolt.org/z/P_ksaa
//
// x86_64
// Instruction latencies have been entirely ignored.
# rdi = desired key
mov rax, qword ptr [rip + kHashSeed] # [a00] rax = per-process seed (with entropy)
add rax, rdi # [a01] rax = hash so far
@Nexuapex
Nexuapex / bit_cast.h
Created Apr 1, 2018
Minimal-dependencies implementation of bit_cast.
View bit_cast.h
#include <string.h>
#if defined(__has_attribute)
#if __has_attribute(always_inline)
#define ALWAYS_INLINE __attribute__((always_inline))
#endif
#endif
#if defined(_MSC_VER)
// To reduce debug build overhead:
@Nexuapex
Nexuapex / resource_table.cc
Created Apr 26, 2016
Sketch of a few ways to store 8 or 16 elements indexed by a packed byte array.
View resource_table.cc
#include <xmmintrin.h>
#include <stdint.h>
struct ResourceTable8
{
uint64_t type_bits_;
uintptr_t elements_[8];
inline uint64_t mask_for_type(uint8_t type)
{
@Nexuapex
Nexuapex / env.cmd
Created Mar 6, 2015
A Python script that injects environment variables into the calling cmd.exe shell.
View env.cmd
@echo off
rem = """
rem This is a hybrid batch and Python script. The batch (right here) invokes
rem the Python script and treats its standard output as a list of environment
rem variables to set in the current environment.
rem The -x argument causes the Python interpreter to ignore the first line.
rem The -S argument disables the implicit 'site' package import (speed go fast).
FOR /F "tokens=1,2 delims==" %%v IN ('python -x -S "%~f0" %*') DO SET %%v=%%w
GOTO :eof
"""
@Nexuapex
Nexuapex / workerpool.py
Created Jan 8, 2015
Simple Python MPMC queue and thread pool.
View workerpool.py
from __future__ import absolute_import
import sys
import threading
import collections
class WorkerPool(object):
"""A thread pool that processes items from an unbounded work queue, for
simple multi-producer, multi-consumer processes.
@Nexuapex
Nexuapex / Microsoft C++.sublime-build
Created Nov 17, 2014
A simple Sublime Text 3 build system for Visual Studio 2013's C/C++ compiler.
View Microsoft C++.sublime-build
{
"shell_cmd": "\"%VS120COMNTOOLS%\\..\\..\\VC\\vcvarsall.bat\" amd64 && cl /nologo \"$file\"",
"file_regex": "^(.*?)\\(([0-9+])(?:,([0-9+]))?\\)\\s+:\\s+(.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++"
}
View simultaneous_initiation.py
import socket
create_socket = lambda: socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
target_port = 49400 # Any port in the ephemeral port range that no process on your machine is using.
port = 0
# Create and bind sockets, working our way through the ephemeral port space, until we get close to our target.
while not (target_port - 5 < port < target_port):
sock = create_socket()
sock.bind(('127.0.0.1', 0))
@Nexuapex
Nexuapex / plaintive_function.cc
Last active Aug 29, 2015
An attempt to reinvent `std::function' that ended up being about things I don't like about C++.
View plaintive_function.cc
// Because it was possible to implement std::move and std::forward<T> as library
// functions, so that's what happened. Now this header is everywhere. This is
// 2,800 lines of kitchen sink for me.
#include <utility>
// C++ has half-decent pattern matching, but only on types. If only some of this
// energy could be thrown at language constructs like `switch'.
//
// Oh, right, and this relies on partial specialization, which only works on
// classes. So you get these pointless classes that contain a single typedef.
@Nexuapex
Nexuapex / inlined.h
Last active Aug 29, 2015
I would get behind this if I could just static_assert(size <= Size) in the placement operator new. Close but no cigar.
View inlined.h
#ifndef INLINED_OBJECT_H
#define INLINED_OBJECT_H
#include <string.h>
template <typename T, size_t Size>
class inlined
{
public:
inlined();
@Nexuapex
Nexuapex / msvcrt_heap_exc.cc
Last active Dec 24, 2015
There's this handy flag on Win32 heap objects called HEAP_GENERATE_EXCEPTIONS, which causes the heap manager to raise a structured exception instead of returning null on a failed allocation. But the C runtime creates its heap behind the scenes and doesn't allow you access to it, and you can't add this flag after creation anyway. Unless you are t…
View msvcrt_heap_exc.cc
#include <malloc.h>
#include <windows.h>
static HANDLE FindCrtHeap()
{
// Pick a block that we know is in the CRT heap.
//
_HEAPINFO crtEntry = {};
if (_HEAPOK != _heapwalk(&crtEntry))
You can’t perform that action at this time.