Skip to content

Instantly share code, notes, and snippets.

View folkertdev's full-sized avatar

Folkert de Vries folkertdev

View GitHub Profile
@folkertdev
folkertdev / bounds and overflow checks.md
Created August 27, 2024 12:05
effect of bounds and overflow checks in zlib-rs

Some further zlib-rs benchmarks using commit c9b7299fa81ca2a8448cbe3e96a42275c9b41b24

Bounds checks

Rust will panic when you try to index out of bounds

This check has a cost: your CPU must perform this check. Typically this involves two instructions: a compare "is the index in bounds" and a jump "go to the panic code". Hence, we intuitively expect programs that perform bounds checking to be slightly slower. In theory, C has an edge here because it does not, by default, check that array access is in bounds.

@folkertdev
folkertdev / blogpost-compress.rs
Created August 8, 2024 10:04
benchmark for measuring zlib-rs versus zlib-ng compression performance
use std::ffi::{c_int, c_uint};
// we use the libz_sys but configure zlib-ng in zlib compat mode
use libz_sys as libz_ng_sys;
use zlib_rs::{DeflateFlush, ReturnCode};
fn main() {
let mut it = std::env::args();
Finished `dev` profile [unoptimized] target(s) in 0.05s
WARNING: The `change-id` is missing in the `config.toml`. This means that you will not be able to track the major changes made to the bootstrap configurations.
NOTE: to silence this warning, add `change-id = 127866` at the top of `config.toml`
Building stage0 library artifacts (x86_64-unknown-linux-gnu)
Finished `release` profile [optimized + debuginfo] target(s) in 0.11s
Building compiler artifacts (stage0 -> stage1, x86_64-unknown-linux-gnu)
Finished `release` profile [optimized + debuginfo] target(s) in 0.19s
Creating a sysroot for stage1 compiler (use `rustup toolchain link 'name' build/host/stage1`)
Building stage0 tool lld-wrapper (x86_64-unknown-linux-gnu)
Finished `release` profile [optimized + debuginfo] target(s) in 0.10s
WARNING: The `change-id` is missing in the `config.toml`. This means that you will not be able to track the major changes made to the bootstrap configurations.
NOTE: to silence this warning, add `change-id = 127866` at the top of `config.toml`
Building stage0 library artifacts (x86_64-unknown-linux-gnu)
Building compiler artifacts (stage0 -> stage1, x86_64-unknown-linux-gnu)
Creating a sysroot for stage1 compiler (use `rustup toolchain link 'name' build/host/stage1`)
Building stage0 tool lld-wrapper (x86_64-unknown-linux-gnu)
Building stage1 library artifacts (x86_64-unknown-linux-gnu)
Building stage0 tool compiletest (x86_64-unknown-linux-gnu)
Testing stage1 compiletest suite=ui mode=ui (x86_64-unknown-linux-gnu)
@folkertdev
folkertdev / stable-order.rs
Created August 2, 2024 08:36
two files where one has inconsistent ordering of the generated error messages
//@ needs-asm-support
//@ ignore-nvptx64
//@ ignore-spirv
#![feature(asm_const)]
use std::arch::{asm, global_asm};
// Const operands must be integers and must be constants.
@folkertdev
folkertdev / comparison.svg
Created June 26, 2024 08:50
implicit coordinates are relative zero
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@folkertdev
folkertdev / example.py
Created June 18, 2024 09:37
tsp async pyo3 experiment
from dataclasses import dataclass
import tsp_python
from tsp_python import AsyncStore, OwnedVid, ReceivedTspMessageVariant, FlatReceivedTspMessage
class ReceivedTspMessage:
@staticmethod
def from_flat(msg: FlatReceivedTspMessage):
match msg.variant:
case ReceivedTspMessageVariant.GenericMessage:
@folkertdev
folkertdev / main.rs
Created June 14, 2024 14:48
PEXT experiment for lzma/xz variable width integer decoding
pub fn decode2(buf: &[u8], size_max: usize, num: &mut u64) -> usize {
let number = unsafe { core::ptr::read_unaligned(buf.as_ptr().cast()) };
let bits = unsafe { core::arch::x86_64::_pext_u64(number, 0x7F7F_7F7F_7F7F_7F7Fu64) };
let bytes_used = (number & !0x7F7F_7F7F_7F7F_7F7Fu64).count_ones();
*num = bits | (buf[8] as u64) << 56;
(size_max > 0) as usize + bytes_used as usize
}
@folkertdev
folkertdev / README.md
Created March 9, 2024 17:06
Creating a zip file with a unix extra field

run and validate with

rm -f extrafield.zip && python add_unix_extra_field.py && zipinfo -v extrafield.zip

this should show

Archive:  extrafield.zip
@folkertdev
folkertdev / crc32.c
Created February 19, 2024 14:43
a vectorized crc32 implementation (from the zlib-ng repository)
// compile with
//
// $ clang -shared -o libcrc32.so -Wall -Werror -fpic -march=native -O3 crc32.c
//
// This command only works on x86_64 systems that support the pclmulqdq instruction.
#include <immintrin.h>
#include <wmmintrin.h>
#include <smmintrin.h> // _mm_extract_epi32
#include <inttypes.h>
#include <stdio.h>