Skip to content

Instantly share code, notes, and snippets.

@oxalica
oxalica / my_benchmark.rs
Created October 4, 2019 09:48
Benchmarks of fs::metadata, stat64, statx, and statx converted to stat64
// statx-sys = "0.3.0"
// libc = "0.2.62"
// criterion = "0.3.0"
//
// Bench with file `a` but no file `b` in working dir.
#![feature(const_cstr_unchecked)]
use libc;
use statx_sys;
use std::{ffi::CStr, fs, mem};
use criterion::*;
time: 0.979; rss: 176MB parse_crate
time: 0.000; rss: 176MB attributes_injection
time: 0.000; rss: 176MB incr_comp_prepare_session_directory
time: 0.000; rss: 176MB incr_comp_garbage_collect_session_directories
time: 0.000; rss: 176MB recursion_limit
time: 0.000; rss: 176MB plugin_loading
time: 0.000; rss: 176MB plugin_registration
time: 0.028; rss: 178MB pre_AST_expansion_lint_checks
time: 0.000; rss: 178MB crate_injection
time: 0.991; rss: 373MB expand_crate
@oxalica
oxalica / json.hs
Created June 18, 2020 18:28
A toy parser for json
-- https://www.json.org/json-en.html
{-# LANGUAGE OverloadedStrings #-}
import Data.Map.Strict (Map, fromList)
import qualified Data.Text as T
import Data.Char (isDigit, chr, isHexDigit)
import Control.Monad (ap)
import Control.Applicative (Alternative(..), optional)
newtype Parser a = Parser { runParser :: T.Text -> Either String (T.Text, a) }
@oxalica
oxalica / Main.idr
Created July 7, 2020 08:14
Typed printf with runtime format string
module Main
import Printf
%default total
||| Define the "like" relationship on two `Format`.
|||
||| One format is "like" the other one iff the former with all non-format-specifier removed is
||| the same as the later.
@oxalica
oxalica / brainfuck_quine_gen.rs
Last active October 28, 2020 12:44
Brainfuck quine generator
use brainfuck::{program::Program, tape}; // brainfuck = "0.2.1"
fn gen_printer(s: &str) -> String {
assert!(!s.is_empty());
let s = s.as_bytes();
let mut buf = "+".repeat(s[0] as usize) + ".";
for (&prev, &cur) in s.iter().zip(&s[1..]) {
if prev < cur {
buf += &"+".repeat((cur - prev) as usize);
} else if cur < prev {
@oxalica
oxalica / flake.nix
Created January 16, 2021 14:49
Rust wasm example development environment
{
inputs = {
nixpkgs.url = github:nixos/nixpkgs/nixos-unstable;
flake-utils.url = github:numtide/flake-utils;
rust-overlay.url = github:oxalica/rust-overlay;
};
outputs = { nixpkgs, flake-utils, rust-overlay, ... }:
flake-utils.lib.eachSystem ["x86_64-linux"] (system: let
pkgs = import nixpkgs {
@oxalica
oxalica / bench-R5-2400G.txt
Last active January 23, 2021 16:33
Vec::retain: swap+truncate vs. drop+move
retain_std u64 almost_kept
time: [123.44 us 127.97 us 134.19 us]
Found 14 outliers among 100 measurements (14.00%)
2 (2.00%) low mild
5 (5.00%) high mild
7 (7.00%) high severe
retain_early_drop u64 almost_kept
time: [98.345 us 100.49 us 103.22 us]
Found 15 outliers among 100 measurements (15.00%)
@oxalica
oxalica / tuple_getter.rs
Last active March 25, 2021 19:48
Extract value from nested tuple by given type
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=440c65898633c8dba89e0a036f049e95
use std::ops::Deref;
use std::marker::PhantomData;
#[repr(transparent)]
struct Getter<A, T>(T, PhantomData<A>);
impl<A, T, U> Deref for Getter<A, (T, U)> {
type Target = Getter<A, U>;
fn deref(&self) -> &Self::Target {
@oxalica
oxalica / Main.hs
Created October 16, 2021 09:57
Script serializer
module Main where
import qualified Data.Map as M
import Text.Printf (printf)
import Control.Monad (ap)
data Script a = Script { genScript :: Int -> (Int, [Operation], a) }
instance Functor Script where
fmap f (Script gen) = Script $ fmap f . gen
@oxalica
oxalica / keybase.md
Created September 8, 2022 20:46
keybase.md

Keybase proof

I hereby claim:

  • I am oxalica on github.
  • I am oxalica (https://keybase.io/oxalica) on keybase.
  • I have a public key whose fingerprint is F90F FD6D 585C 2BA1 F13D E8A9 7571 654C F88E 31C2

To claim this, I am signing this object: