Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  Print  
  A poor man's struct (err, MappedObject)  (Read 7556 times)
0 Members and 1 Guest are viewing this topic.
Offline HamsterofDeath

Jr. Member
**

Posts: 75


Java games rock!


« Reply #30 on: 2006-03-22 08:05:47 »

that backed-by-float[]-floatbuffer is STILL faster than using Unsafe.

i removed the double increment. saved about 2ms (of 29). i'm at 27 now. and i'm just reading the same element 10 million times now, so there shouldn't be cache flushing, right?.
the 10mb size shouldn't matter at all. every test iterates over every element.

on my machine, your direct float buffer is slower (a few ms) than the backed one...
Online Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5866
Medals: 255


Hand over your head.


« Reply #31 on: 2006-03-22 09:03:46 »

Let's not turn this thread into a benchmark frenzy, please.

The generated machine-code depends greatly on the JIT and can't really be predicted.

Sometimes FloatBuffers perform like float-arrays, sometimes they perform 10 times worse... let me show you what I mean:

Testing all 4 types of access:
1  
2  
3  
4  
float[]:               183ms
unsafe:                127ms
floatbuffer (direct):  1131ms
floatbuffer (float[]): 1035ms



Testing 3 types of access (all minus floatbuffer (float[]) )
1  
2  
3  
4  
float[]:               194ms
unsafe:                128ms
floatbuffer (direct):  147ms <--------- 7.7x faster
floatbuffer (float[]): 0ms



So code in other methods doing other things, affects performance in appearantly unrelated code!



When invoking this anywhere:
FloatBuffer.allocate(elements);
the code using DirectFloatBuffer drops performance by factor 7-8 Shocked

Probably because the JIT notices FloatBuffer now has more than 1 active subclass, so it can't optimize the general FloatBuffer class as it would do when only having DirectFloatBuffers around.


1  
2  
3  
4  
5  
6  
7  
benchmark();
printResults(); // floatbuffer (direct):  115ms

FloatBuffer.allocate(elements);

benchmark();
printResults(); // floatbuffer (direct):  971ms



* Riven learned something new today

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Spasi

JGO Ninja
***

Posts: 589
Medals: 26


Molon Lave


« Reply #32 on: 2006-03-22 12:10:52 »

Probably because the JIT notices FloatBuffer now has more than 1 active subclass, so it can't optimize the general FloatBuffer class as it would do when only having DirectFloatBuffers around.

This situation is supposed to be better in Mustang with bimorphic call inlining.
Games published by our own members! Go get 'em!
Online Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5866
Medals: 255


Hand over your head.


« Reply #33 on: 2006-03-22 12:19:30 »

Life is better after 6  Grin

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline tusaki

Full Member
**

Posts: 112
Medals: 1


In a mad world only the mad are sane.


« Reply #34 on: 2006-03-22 13:06:11 »

Here are my results for both benchmarks. (including suggested improvements)

Benchy
1  
2  
3  
4  
5  
6  
7  
8  
9  
before FloatBuffer.allocate()
   float[]:               211ms
   unsafe:                218ms
   floatbuffer (direct):  304ms

after FloatBuffer.allocate()
   float[]:               249ms
   unsafe:                219ms
   floatbuffer (direct):  1150ms


UnsafeDemo:
1  
2  
3  
4  
5  
6  
7  
8  
Array time: 30.98215 ms
x array= 1.24908006E9
unsafe time 52.641886 ms
x unsafe= 1249216512
Floatbuffer time: 31.53641 ms
x array= 1.24949581E9
Directbytebuffer2float time: 101.041054 ms
x array= 1.14874189E9


Offline HamsterofDeath

Jr. Member
**

Posts: 75


Java games rock!


« Reply #35 on: 2006-03-22 15:23:08 »

the reason for unsafedemo being slower is that i use a "naked" unsafe instance. benchy extracts it from a bytebuffer.
if i use a bytebuffer's Unsafe, unsafe.get/put & arrays are almost equally fast. Unsafe is a bit faster, but you could have trouble avoiding the "base + (index<<bytes_of_type)"-calculation when accessing the buffer. if you can avoid it, Unsafe is the way to go Smiley
Online Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5866
Medals: 255


Hand over your head.


« Reply #36 on: 2006-03-22 16:22:11 »

My implementation avoids it... Wink

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline chaosdeathfish

Jr. Member
**

Posts: 56



« Reply #37 on: 2006-03-28 17:06:47 »

Does the JVM do bounds-check removal on Buffer types? I know i does on arrays..
Online Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5866
Medals: 255


Hand over your head.


« Reply #38 on: 2006-03-28 17:11:26 »

1  
2  
3  
4  
float[]:               194ms
unsafe:                128ms
floatbuffer (direct):  147ms <--------- 7.7x faster
floatbuffer (float[]): 0ms

As it is faster than float[], I'd be very surprised if it did not remove bound-checks.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline princec
« League of Dukes »

JGO Kernel
*****

Posts: 8073
Medals: 91


Eh? Who? What? ... Me?


« Reply #39 on: 2006-03-29 04:52:37 »

Only the server VM does bounds check hoisting, AFAIK.

Cas Smiley

Games published by our own members! Go get 'em!
Online Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5866
Medals: 255


Hand over your head.


« Reply #40 on: 2006-03-29 05:04:39 »

Indeed. I run all benchmarks against the server-vm for erm.. performance-reasons Smiley

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline HamsterofDeath

Jr. Member
**

Posts: 75


Java games rock!


« Reply #41 on: 2006-03-29 05:36:00 »

Does the JVM do bounds-check removal on Buffer types? I know i does on arrays..

i'd say: no. a bytebuffer's get and put methods are always a bit slower than direct array accesses. (at least the last 10 times i looked at the results)
if i simply call a bytebuffer's private put/get-methods (jdk hack) which don't do bounds checks manually, i'm almost at array speed (50% slower). if i inline the calls and access the unsafe object directly, using (base+(offset<<2)) as the address to get an int, i'm really almost at array speed (up to 10% difference, sometimes faster, sometimes slower, which means it's pretty equal). using any precalucated value as an address speeds things up, and i'm at 200% array speed.
i assume this means:
a) java uses the same calculation to access random values in arrays
b) java doesn't do array bounds checks if the vm can be sure that no evil index will be used.
Pages: 1 [2]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.167 seconds with 20 queries.