How can I guarantee that a type that doesn’t implement Sync can actually be safely shared between threads?

Another solution for this case is to move a mutable reference to the item into the thread, even though mutability isn’t required. Since there can be only one mutable reference, the compiler knows that it’s safe to be used in another thread. use crossbeam; // 0.7.3 use std::cell::RefCell; fn main() { let mut val = … Read more

When to use pointers in C#/.NET?

When is this needed? Under what circumstances does using pointers becomes inevitable? When the net cost of a managed, safe solution is unacceptable but the net cost of an unsafe solution is acceptable. You can determine the net cost or net benefit by subtracting the total benefits from the total costs. The benefits of an … Read more

What is the fastest way to convert a float[] to a byte[]?

There is a dirty fast (not unsafe code) way of doing this: [StructLayout(LayoutKind.Explicit)] struct BytetoDoubleConverter { [FieldOffset(0)] public Byte[] Bytes; [FieldOffset(0)] public Double[] Doubles; } //… static Double Sum(byte[] data) { BytetoDoubleConverter convert = new BytetoDoubleConverter { Bytes = data }; Double result = 0; for (int i = 0; i < convert.Doubles.Length / sizeof(Double); … Read more

True Unsafe Code Performance

Some Performance Measurements The performance benefits are not as great as you might think. I did some performance measurements of normal managed array access versus unsafe pointers in C#. Results from a build run outside of Visual Studio 2010, .NET 4, using an Any CPU | Release build on the following PC specification: x64-based PC, … Read more

Processing vec in parallel: how to do safely, or without using unstable features?

Today the rayon crate is the de facto standard for this sort of thing: use rayon::prelude::*; fn main() { let mut data = vec![1, 2, 3]; data.par_iter_mut() .enumerate() .for_each(|(i, x)| *x = 10 + i as u32); assert_eq!(vec![10, 11, 12], data); } Note that this is just one line different from the single-threaded version using … Read more