Skip to content

Instantly share code, notes, and snippets.

View benwills's full-sized avatar

Ben Wills benwills

View GitHub Profile
@benwills
benwills / Enum_Bitfield.php
Last active December 5, 2021 14:17
Using Enums for Bitfield Values in PHP8.1 (and convert int values to string arrays or original enum value arrays)
<?php
enum Status : int
{
case Undefined = 0;
case Opt1 = 1 << 0;
case Opt2 = 1 << 62;
case Opt3 = 1 << 63;
static public function MatchArr(int $x) : false|array
@benwills
benwills / epoll_rcvlowat_test_client.c
Last active March 15, 2020 14:45
epoll client, testing with sockopt SO_RCVLOWAT
/*
A few years ago, I tried to create an HTTP client using epoll that could manage many connections. Performance was incredibly poor due to what I determined to be too many syscalls from epoll returning on every packet received. I then moved to FreeBSD and kqueue() and got the performance I was looking for.
I mentioned this on a Hackernews thread announcing a new epoll feature: https://news.ycombinator.com/item?id=17851855#17856351
@caf on HN (@keaston on GitHub) responded saying this was possible using SO_RCVLOWAT. I recall testing this a few years ago, but still could not get it working. Given that it was pretty early in my C days, it's possible I could have been doing a number of things wrong.
@keaston wrote a quick server script to demonstrate this ability. Curious if this also applied to client sockets, I dug up and modified some old code to test it.
@benwills
benwills / zlib.inflate.multi-stream.c
Last active August 30, 2017 06:22
zlib inflate, where the file/buffer contains multiple streams.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <zlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>
@benwills
benwills / goto_lut_vs_switch.c
Last active October 19, 2015 07:39
Performance benchmark of a computed goto (lookup table) vs a switch statement in C.
#include <stdio.h>
#include <inttypes.h>
#include <sys/time.h>
#include <time.h>
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
char buf[BUFSIZ];
FILE *fp;
if ((fp= popen("/bin/php -f /webserver/pages/test.php", "r")) != NULL)
{
/* Read from the PHP command output */
while (fgets(buf, BUFSIZ, fp) != NULL)
{
/* Process the output from PHP as desired */
...
@benwills
benwills / latency.txt
Last active August 29, 2015 14:15 — forked from jboner/latency.txt
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms
Read 4K randomly from SSD* 150,000 ns 0.15 ms
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
unsigned
input = 0b0111u,
n_bits = 4u,
*bits = (unsigned*)malloc(sizeof(unsigned) * n_bits),
bit = 0;
// core_id = 0, 1, ... n-1, where n is the system's number of cores
int stick_this_thread_to_core(int core_id) {
int num_cores = sysconf(_SC_NPROCESSORS_ONLN);
if (core_id < 0 || core_id >= num_cores)
return EINVAL;
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
@benwills
benwills / create_cnv_ascii.c
Created December 30, 2014 12:18
Create a Lookup Table for ascii Conversions
#include <stdio.h>
#include <string.h>
//
// Create entries for a boolean lookup table to be used for character conversions.
//
// Example output from list[], below:
// 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
// 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
// 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , '-', '.', 0 ,