A thought on value-typing existing Collections

Timo Kinnunen timo.kinnunen at gmail.com
Fri Dec 4 13:18:45 UTC 2015


I recently needed a primitive specialized HashMap<long, long> and looked into the implementations while deciding on one, eventually going with the https://github.com/OpenHFT/Koloboke library. The generated class hierarchy it uses is obnoxious, but the gist of the implementation can be explained with this pseudo-code:

	public void put(long key, long value) {
		long free = getOldFree();
		if(key == free) {
			free = chooseNewFree();
		int index = generateHashCode(key);
		putAtFirstFree(index, free, key, value);

This code will be very easy to convert to use value types. This is because it allows all longs as valid for both keys and values while at the same time it doesn’t use a fixed value as the marker in its implementation.  

In other words, because it permits every value as a valid input, including the values it’s using for internal bookkeeping!

Translated into the references and objects world, the easiest Collections to convert will be those that are internally strongly-typed and then -- a bit paradoxically -- allow null as a valid value as this prevents the use of null as an internal marker. Closely followed by those that use object identities in comparisons. Which also happen to be just the kinds of Collections Java programmers have been told for years to avoid. Oops?

Some food for thought for the weekend.

Have a nice day, 

Sent from Mail for Windows 10

More information about the valhalla-dev mailing list