3

I have found several off heap in memory data storage, such as Chronicle-Map, mapdb, etc... They all have usage of locks (stamped read write lock, or reentrant read write lock).

Is there any data structure that is off heap and lock free on Java? Or is there any off heap data structure that is read-lock-free?

  • What do you mean by an "off-heap" data structure? I thought all objects were stored on the heap. – NomadMaker Nov 09 '20 at 07:24
  • @NomadMaker https://stackoverflow.com/q/6091615/12323248 – akuzminykh Nov 09 '20 at 07:27
  • 2
    there is no such thing as lock-free data structure. They use locks anyway, in the form of compare-and-swap machine instruction. So if you are looking for fast data structures, just measure the speed of this and that structures. I doubt you can beat the speed of Chronicle data structures. – Alexei Kaigorodov Nov 09 '20 at 10:25
  • 3
    @AlexeiKaigorodov I think "lock-free data structure" is a pretty established term in [non-blocking synchronization](https://en.wikipedia.org/wiki/Non-blocking_algorithm). And yes, CAS is mostly (always?) used, which is a machine instruction, not a lock. What do you mean by CAS being a lock? – akuzminykh Nov 10 '20 at 08:20
  • @akuzminykh I mean that when contention, CAS wood try and try in a loop, effectively blocking the algorithm, until contention is dismissed. – Alexei Kaigorodov Nov 10 '20 at 10:04

2 Answers2

1

Agrona's ring buffer is fully lock free off-heap data structure. This queue could be used as inter-process-communication library (and actually is used inside aeron) and doesn't leverage any locks/system calls for message passing. Besides that, I'm pretty sure Agrona has more off-heap data structures, so you might want to check it out.

Andrey Cheboksarov
  • 649
  • 1
  • 5
  • 9
  • I don't think the OP is interested in ring buffers, and I don't think Agrona supplies off-heap lists, sets, maps. – spongebob Dec 10 '20 at 19:03
0

Lock-free is not necessarily faster than lock-based.

In modern JVMs, built-in locks are extremely fast, and beat old lock-free algorithms by a high magnitude.

This looks like an XY problem. What are you trying to achieve?

spongebob
  • 8,370
  • 15
  • 50
  • 83