Skip to content

Instantly share code, notes, and snippets.

View d0k's full-sized avatar

Benjamin Kramer d0k

View GitHub Profile
commit 99ea7d9b3d64c51694b8cc35c6bd25b4945ab0c2
Author: Benjamin Kramer <benny.kra@gmail.com>
Date: Wed Apr 25 12:39:50 2012 +0200
X86: Turn cmovs into branches when profitable.
This came up when a change in block placement formed a cmov and slowed down a
hot loop by 50%:
ucomisd (%rdi), %xmm0
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef __SSE2__
#include <emmintrin.h>
#endif
static const char *SkipToNewline(const char *CurPtr, const char *BufferEnd) {
unsigned char C = *CurPtr;
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *boyer_moore_horspool(const char *h, size_t hlen,
const char *n, size_t nlen) {
const unsigned char *haystack = (const unsigned char *)h;
const unsigned char *needle = (const unsigned char *)n;
@d0k
d0k / allocs.d
Created July 25, 2010 13:14
set of dtrace scripts to analyze memory allocations
#pragma D option quiet
pid$target::malloc:entry {
@size[probefunc] = sum(arg0);
@totalsize = sum(arg0);
@calls[probefunc] = count();
@totalcalls = count();
}
pid$target::calloc:entry {
@d0k
d0k / ptee.c
Created June 24, 2010 14:32
tee stdin into a process and stdout
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#ifndef BUFSIZ
#define BUFSIZ 1024
#endif
int main(int argc, char *argv[]) {
if (argc < 2)
@d0k
d0k / dupeln.pl
Created June 24, 2010 14:31
Turn duplicate files into hardlinks
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use Digest::MD5;
my %files;
sub wanted {
@d0k
d0k / sortuniq.cpp
Created June 24, 2010 14:27
sort | uniq -c | sort -n
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
typedef std::map<const std::string, unsigned> map_t;
typedef std::pair<unsigned, const std::string*> pair_t;
typedef std::vector<pair_t> array_t;
@d0k
d0k / clean.c
Created November 26, 2009 14:31
find . -name '*...*' -delete
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
DIR *dir;
unsigned long count = 0, unlinked = 0;
struct dirent *ent;
int ret;
@d0k
d0k / copytime.pl
Created November 26, 2009 14:14
apply file times from one file to another
#!/usr/bin/perl
use warnings;
use strict;
use File::stat;
use File::Basename;
if (@ARGV == 0) {
print {*STDERR} "Usage: $0 infile outfile\n";
print {*STDERR} "Usage: $0 indir outdir\n";
@d0k
d0k / historyclean.m
Created April 29, 2009 13:47
Safari history cleaner written in Objective C 2.0
/* Safari History.plist cleaner
* compile with:
* gcc historyclean.m -o historyclean
*/
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr,