Skip to content

Instantly share code, notes, and snippets.

@haberman
haberman / main.dart
Created March 14, 2023 23:10
enchanted-aurora-9547
T create<T>(T Function(int) creator, int a) {
return creator(a);
}
class Bar {
int member;
Bar(this.member);
void doPrint() { print(member); }
}
@haberman
haberman / main.dart
Created February 27, 2023 17:20
mellow-jungle-4935
class Getter<T> {}
extension on Getter<int> {
int get() => 42;
}
extension on Getter<String> {
String get() => "the answer to life, the universe, and everything";
}
@haberman
haberman / main.dart
Created February 18, 2023 22:23
mellow-arc-8120
class Getter<T> {}
T get<T>(Getter<T> getter) {
if (T == int) {
return 42 as T;
} else if (T == String) {
return "the answer to life, the universe, and everything" as T;
}
throw "Unexpected type: ${T.toString()}";
autocmd FileType bzl AutoFormatBuffer buildifier
syntax on
let mapleader = ","
set wildmode=list:longest
set list
set listchars=tab:>-,trail:-,extends:>,precedes:<,nbsp:+
set sts=2
set sw=2
set bs=2
0.7% 0.7% 8645 google::protobuf::TextFormat::Parser::ParserImpl::ConsumeField(google::protobuf::Message*)
0.6% 1.2% 7131 google::protobuf::io::Printer::Print(char const*, char const*, std::string const&, char const*, std::string const&, char const*, std::string const&, char const*, std::string const&, char const*, std::string const&, char const*, std::string const&, char const*, std::string const&, char const*, std::string const&)
0.5% 1.8% 6346 google::protobuf::DescriptorBuilder::BuildMessage(google::protobuf::DescriptorProto const&, google::protobuf::Descriptor const*, google::protobuf::Descriptor*)
0.5% 2.2% 6248 google::protobuf::io::Printer::Print(char const*, char const*, std::string const&, char const*, std::string const&, char const*, std::string const&, char const*, std::string const&, char const*, std::string const&, char const*, std::string const&, char const*, std::string const&)
0.5% 2.7% 5974 google::protobuf::util::converter::ProtoWriter::RenderPrimitiveField
@haberman
haberman / upbwrapper.rs
Last active January 3, 2016 17:29
Prototype for a scheme for upb Rust wrappers for upb.
// Prototype for a scheme for upb Rust wrappers for upb.
//
// Output (when linked with fakeupb.c):
// C: new()
// C: number() = 1
// Number: 1
// C: setnumber(5)
// C: number() = 5
// Number: 5
// C: freeze() = 1
// Fake upb: a fake implementation for a small subset of the
// actual upb interface, for testing with Rust.
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
uint32_t refcount;
// On my system:
// virtual call time: 4.340
// non-virtual call time: 3.970
// virtual call overhead 9.32%
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#define NOINLINE __attribute__ ((noinline))
// Output on my system:
// virtual call time: 4.340
// non-virtual call time: 3.970
// virtual call overhead 9.32%
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#define NOINLINE __attribute__ ((noinline))
@haberman
haberman / jit1.c
Created December 12, 2012 03:33
Code for a trivial (but real) JIT. Part of an upcoming article / blog post.
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) {
// Machine code for:
// mov eax, 0
// ret
unsigned char code[] = {0xb8, 0x00, 0x00, 0x00, 0x00, 0xc3};