Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  JEP for making Unsafe a public API  (Read 2583 times)
0 Members and 1 Guest are viewing this topic.
Offline Spasi
« Reply #30 - Posted 2014-05-09 15:03:58 »

So has anyone looked at how hotspot expands the various get/put methods after compiling?

They compile down to trival memory loads/stores. At least in the x86 disassemblies I've inspected. However...

The real problem with Unsafe access comes from hotspot NEVER reordering accesses in the JITed code. This has a measurable effect on performance and I first encountered it when doing performance testing with Riven's original mapped objects library. I'm not sure what the technical problem is, but it looks like all native calls (intrinsified or not) act as implicit code barriers and GC safepoints. Unsafe access disassembly looks exactly like the source Java code (same access order that is) and I don't know if this is a bug or a correctness/security issue. Definitely though, hotspot does not apply the same optimizations to Unsafe as it does to fields (of course I'm talking about plain access, not volatile/ordered) and any computation going in or out of off-heap memory will suffer a performance penalty.

I've written a JMH benchmark that showcases the problem. The baseline is Matrix4fJava.mul4f, which performs standard 4x4 matrix multiplication, where the target may be one of the operands. The matrix is represented as a POJO with float fields. It is compared to doing the exact same calculation with a float array, a FloatBuffer and finally, Unsafe. There are also optimized versions that have been manually tuned to minimize stack usage, close to what hotspot does automatically for field access. Results on Java 8 GA x64 (edit: added float vs int results and made more clear what the baseline is):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
// Floating point
Benchmark                       Mode   Samples         Mean   Mean error    Units
UnsafeBench.field               avgt        10       24.972        0.229    ns/op

UnsafeBench.array               avgt        10       30.871        1.331    ns/op
UnsafeBench.arrayOptimized      avgt        10       26.681        0.044    ns/op
UnsafeBench.buffer              avgt        10       31.548        0.493    ns/op
UnsafeBench.bufferOptimized     avgt        10       28.809        0.071    ns/op
UnsafeBench.unsafe              avgt        10       31.541        0.160    ns/op
UnsafeBench.unsafeOptimized     avgt        10       24.517        0.056    ns/op

// Integer
Benchmark                       Mode   Samples         Mean   Mean error    Units
UnsafeBench.field               avgt        10       29.582        0.153    ns/op

UnsafeBench.array               avgt        10       37.068        0.303    ns/op
UnsafeBench.arrayOptimized      avgt        10       31.804        0.125    ns/op
UnsafeBench.buffer              avgt        10       44.780        0.134    ns/op
UnsafeBench.bufferOptimized     avgt        10       39.070        0.175    ns/op
UnsafeBench.unsafe              avgt        10       37.281        0.343    ns/op
UnsafeBench.unsafeOptimized     avgt        10       29.032        0.194    ns/op

This is the reason I'm not particularly enthusiastic about this JEP. My only hope is seeing John Rose being involved with both value types and Project Panama. It's the best opportunity to highlight what a pain IPC and interacting with native APIs is, and somehow making value types/arrays possible to back with off-heap memory.
Offline Roquen
« Reply #31 - Posted 2014-05-09 20:26:38 »

Interesting.  I was expecting to hear that get/set were identical to field accesses.  It certainly seems like a 'defect' if they are acting like barriers.  If you haven't then now might be a good time to ask the question on the compiler mailing list since issues like the improved memory model, public Unsafe and structure like functionality are on people's minds.  I'll look at the source soon.
Offline Roquen
« Reply #32 - Posted 2014-05-12 12:53:33 »

Spasi's benchmark indicates this is very unlike to be interesting but I tossed this together anyway if anyone is motivated to play around:

https://github.com/roquendm/JGO-Grabbag/blob/master/src/roquen/vm/FatOaf.java

Makes on offheap object with overlayed array headers for (byte,int,float). 
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #33 - Posted 2014-06-10 08:09:23 »

In a moment of work-avoidance I took a quick peek at android's Unsafe.  It looks like one could use a FatOaf backing to port LibStruct.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #34 - Posted 2014-06-10 14:54:17 »

AFAIK Android's Unsafe only supports bulk operations, making it useless for LibStruct persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Roquen
« Reply #35 - Posted 2014-06-10 15:03:52 »

I was looking here:
https://android.googlesource.com/platform/libcore/+/master/libdvm/src/main/java/sun/misc/Unsafe.java
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #36 - Posted 2014-06-10 16:34:19 »

Ah right...
1  
2  
public native int getInt(Object obj, long offset);
public native void putInt(Object obj, long offset, int newValue);


I, however, need:
1  
2  
public native int getInt(long address);
public native void putInt(long address, int newValue);


Besides, if they can't get FloatBuffer performance right after half a decade, what are the odds Unsafe calls will be in intrinsified.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Roquen
« Reply #37 - Posted 2014-06-10 16:41:59 »

On intrinsics: zero percent.  That's why I was thinking that FatOaf thing...then they are needed.  Maybe I should attempt to motive myself to see if it actually works first before opening my mouth any more.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #38 - Posted 2014-06-10 17:22:34 »

My Google Fu failed on 'FatOaf' Emo

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online BurntPizza
« Reply #39 - Posted 2014-06-10 17:24:35 »

I assume he's talking about this: https://github.com/roquendm/JGO-Grabbag/blob/master/src/roquen/vm/FatOaf.java
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #40 - Posted 2014-06-10 18:03:44 »

Yeap.  So if it works, unsafe is only needed to verify the object headers and to set up the (per thread) memory chunks.
Offline noctarius

Senior Newbie





« Reply #41 - Posted 2014-09-03 09:40:58 »

Interesting to see this picked up here Smiley

PS: Sorry for resurrecting, haven't seen it earlier.
Pages: 1 [2]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!