Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  A poor man's struct (err, MappedObject)  (Read 8794 times)
0 Members and 1 Guest are viewing this topic.
Offline HamsterofDeath

Junior Member




Java games rock!


« Reply #30 - Posted 2006-03-22 14: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...
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #31 - Posted 2006-03-22 15: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Spasi
« Reply #32 - Posted 2006-03-22 18: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! Check 'em out!
Try the Free Demo of Droid Assault
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #33 - Posted 2006-03-22 18: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline tusaki

Junior Member




In a mad world only the mad are sane.


« Reply #34 - Posted 2006-03-22 19: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

Junior Member




Java games rock!


« Reply #35 - Posted 2006-03-22 21: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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #36 - Posted 2006-03-22 22:22:11 »

My implementation avoids it... Wink

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline chaosdeathfish

Junior Member


Projects: 1



« Reply #37 - Posted 2006-03-29 00:06:47 »

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

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #38 - Posted 2006-03-29 00: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline princec
« League of Dukes »

JGO Kernel


Medals: 196
Projects: 3


Eh? Who? What? ... Me?


« Reply #39 - Posted 2006-03-29 11:52:37 »

Only the server VM does bounds check hoisting, AFAIK.

Cas Smiley

Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #40 - Posted 2006-03-29 12: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline HamsterofDeath

Junior Member




Java games rock!


« Reply #41 - Posted 2006-03-29 12: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (81 views)
2013-05-17 21:29:12

alaslipknot (91 views)
2013-05-16 21:24:48

gouessej (122 views)
2013-05-16 00:53:38

gouessej (114 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.094 seconds with 21 queries.