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

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

What is an “internal address” in Java?

This is clearly implementation-specific. Below I include the Object.hashCode() implementation used in OpenJDK 7. The function supports six different calculation methods, only two of which take any notice of the object’s address (the “address” being the C++ oop cast to intptr_t). One of the two methods uses the address as-is, whereas the other does some … Read more

Why does sun.misc.Unsafe exist, and how can it be used in the real world?

examples VM “intrinsification.” ie CAS (Compare-And-Swap) used in Lock-Free Hash Tables eg:sun.misc.Unsafe.compareAndSwapInt it can make real JNI calls into native code that contains special instructions for CAS read more about CAS here http://en.wikipedia.org/wiki/Compare-and-swap The sun.misc.Unsafe functionality of the host VM can be used to allocate uninitialized objects and then interpret the constructor invocation as any … Read more

Is there a way to get a reference address? [duplicate]

You can get the object index with Unsafe. Depending on how the JVM is using the memory (32-bit addresses, 32-bit index, 32-bit index with offset, 64-bit address) can affect how useful the object index is. Here is a program which assumes you have 32-bit index in a 64-bit JVM. import sun.misc.Unsafe; import java.lang.reflect.Field; import java.util.Arrays; … Read more

How do I get an owned value out of a `Box`?

Dereference the value: fn unbox<T>(value: Box<T>) -> T { *value } There’s a nightly associated function into_inner you can use as well: #![feature(box_into_inner)] fn unbox<T>(value: Box<T>) -> T { Box::into_inner(value) } Way back in pre-1.0 Rust, heap-allocated values were very special types, and they used the sigil ~ (as in ~T). Along the road to … Read more

How can I use pointers in Java?

All objects in Java are references and you can use them like pointers. abstract class Animal {… } class Lion extends Animal {… } class Tiger extends Animal { public Tiger() {…} public void growl(){…} } Tiger first = null; Tiger second = new Tiger(); Tiger third; Dereferencing a null: first.growl(); // ERROR, first is … Read more

tech