Skip to content

Instantly share code, notes, and snippets.

View cfsamson's full-sized avatar

Carl Fredrik Samson cfsamson

View GitHub Profile
@cfsamson
cfsamson / version1.cr
Last active September 15, 2018 14:22
crystal_gmp
# Fastest implementation in Crystal, but needs the following lines added to lib_gmp.cr
# and recompile the Crystal:
#
# fun submul_ui = __gmpz_submul_ui(rop : MPZ*, op1 : MPZ*, op2 : ULong)
# fun addmul_ui = __gmpz_addmul_ui(rop : MPZ*, op1 : MPZ*, op2 : ULong)
#
# ...or you could rebind to GMPlib and implement them in your own `lib` and use that
# in the #addmul and #submul methods instead if you don't have Crystal source code available.
# **SEE VERSION 5 for an implementation of this without changing the stdlib and confirm the perfomance**
@cfsamson
cfsamson / uow.rs
Created February 14, 2019 23:10
A small code example on an idea of how to implement the Unit Of Work pattern in Rust
fn main() {
let mut session = UoW::new();
let entry = session.load::<SomeEntity>();
println!("{:?}", entry);
entry.value().msg = "Changed".to_string();
println!("{:?}", entry);
let other_entry = session.load::<SomeOtherEntity>();
println!("{:?}", other_entry);
@cfsamson
cfsamson / pathracer_line_by_line.rs
Last active March 5, 2019 21:21
Line by line port of the C# code
use lazy_static::lazy_static;
use rand::random;
use std::io::Write;
use std::ops::{Add, Mul, Not, Rem};
#[derive(Debug, Clone, Copy)]
struct Vec3 {
x: f32,
y: f32,
z: f32,
@cfsamson
cfsamson / pathracer_letter_iteration.rs
Created March 4, 2019 14:38
Adapt the code to allow iteration of letters instead of indexing
use lazy_static::lazy_static;
use rand::random;
use std::io::Write;
use std::ops::{Add, Mul, Not, Rem};
#[derive(Debug, Clone, Copy)]
struct Vec3 {
x: f32,
y: f32,
z: f32,
}
@cfsamson
cfsamson / optimizing_part1.rs
Created March 4, 2019 18:05
optimizing part 1 code changes
// -------- We change the code from this: --------
lazy_static! {
static ref LETTERS: Vec<i32> = {
let x: String = [
"5O5_", "5W9W", "5_9_", // P (without curve)
"AOEO", "COC_", "A_E_", // I
"IOQ_", "I_QO", // X
"UOY_", "Y_]O", "WW[W", // A
"aOa_", "aWeW", "a_e_", "cWiO" // R (without curve)
].concat();
@cfsamson
cfsamson / optimizing_part2.rs
Created March 4, 2019 18:15
Optimizing the code part 2
// -------- Our main function will change from this --------
fn main() {
// {...}
for y in (0..h as u64).rev() {
for x in (0..w as u64).rev() {
let mut color = Vec3::from(0.0);
for _ in (0..samples_count).rev() {
color = color
+ trace(
position,
@cfsamson
cfsamson / optimizing_with_rayon.rs
Last active March 4, 2019 18:38
Optimizing the code part 3
// -------- We change the relevant code in main() from this: --------
fn main() {
// {...}
let bytes: Vec<u8> = pixels.iter().flat_map(|(y, x)| {
let mut color = Vec3::from(0.0);
for _ in (0..samples_count).rev() {
color = color + trace(position, !(goal + left * (*x - w / 2.0 + random_val()).into() + up * (*y - h / 2.0 + random_val()).into()));
}
color = color * (1.0 / samples_count as f32).into() + (14.0 / 241.0).into();
@cfsamson
cfsamson / raytracer_memincrease_fix.rs
Created March 7, 2019 19:48
optimizing_fixing_mem_increase
// For the sake of brevity I won't repeat the code we replace here instead point out
// that this replaces lines 307-328 in our original code example and of course the
// lass change we made when introducint Rayon the first time
fn main() {
// {...}
// first we create a range iterator ower
let bytes: Vec<u8> = (0..h as u32)
// turn it in to a parrallell iterator
.into_par_iter()
@cfsamson
cfsamson / raytracer_memincrease_fix.rs
Last active March 7, 2019 22:51
optimizing_fixing_mem_increase
// For the sake of brevity I won't repeat the code we replace here instead point out
// that this replaces lines 307-328 in our original code
fn main() {
// {...}
// first we create a range iterator over y-coordinates
let bytes: Vec<u8> = (0..h as u32)
// turn it in to a parallel iterator
.into_par_iter()
@cfsamson
cfsamson / raytracer_memincrease_fix_2.rs
Last active March 9, 2019 17:52
raytracer_memincrease_fix_2
// For the sake of brevity I won't repeat the code we replace here instead point out
// that this replaces lines 307-328 in our original code
const BYTES_PER_PIXEL: usize = 3;
fn main() {
// {...}
// first allocate a zeroed Vec<u8> to hold our results.
// btw: this initialization method is the fastest way to ask
// for zeroed memory: see https://github.com/rust-lang/rust/issues/54628
let mut bytes = vec![0u8; h as usize * w as usize * BYTES_PER_PIXEL];