Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  DirectByteBuffers slow???  (Read 2296 times)
0 Members and 1 Guest are viewing this topic.
Offline rreyelts

Junior Member




There is nothing Nu under the sun


« Posted 2004-05-18 18:54:16 »

Hi all,

So here's the situation.

I have a network of objects stored as a few arrays in a C++ JNI library. I expose that network data to my Java application through DirectByteBuffers. I wrap the ByteBuffers up in a class with a flyweight pattern, like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public class Network {
  private ByteBuffer nodes;
  public class Node {
    private int index;
    private int offset = NODE_SIZE + index;
    public int index( int index ) {
      this.index = index;
    }
    public float x() {
      return nodes.getFloat( offset + X_OFFSET );
    }
    public float y() {
      return nodes.getFloat( offset + Y_OFFSET );
    }
  }
}


Life was good. Or so I thought.

The problem I'm seeing, is that the access to the buffers seems very slow. I'm accessing, on average, about 50K nodes, and my times can run up into the seconds. Anybody seen this before?

I'm running with JDK 1.5 Beta 1, with -server and more than enough memory allocated. I give the server plenty of time to warm up before taking measurements.

God bless,
-Toby Reyelts

About me: http://jroller.com/page/rreyelts
Jace - Easier JNI: http://jace.reyelts.com/jace
Retroweaver - Compile on JDK1.5, and deploy on 1.4: http://retroweaver.sf.net.
Offline princec

JGO Kernel


Medals: 364
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2004-05-19 07:36:35 »

And now you know why we need Structs in the language. They are entirely essential to high-performance programming Wink (But you knew that anyway, hehe!)

Actually the problem is well known: calling getFloat()/putFloat() on a ByteBuffer is bastardly slow - but if you wrap your ByteBuffer in several sliced up FloatBuffers etc. then you get full speed optimized access. It's so ugly and unnecessary it makes me cry. Shoot the Sun engineers for me. Still no comment about the RFE from Sun, no-one's emailed me about it, not a squeak.

Cas Smiley

Offline Mark Thornton

Senior Member





« Reply #2 - Posted 2004-05-19 08:48:56 »

Also watch out for your byte ordering. ByteBuffer.duplicate and ByteBuffer.slice do not preserve the byte ordering.

getFloat on a ByteBuffer is probably difficult to accelerate as it must check the byte ordering each time. The wrapped FloatBuffer on the other hand has the byte ordering fixed at the time the buffer is created. In addition some architectures don't permit native reading of floats (or other objects larger than one byte) at arbitrary offsets but require alignment. Again a suitable FloatBuffer implementation can be created to reflect the alignment, but this may not be possible when reading floats direct from a ByteBuffer.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mark Thornton

Senior Member





« Reply #3 - Posted 2004-05-19 09:02:35 »

Incidentally Cas, have you considered the implications of alignment issues for accelerating your Struct proposal? While this may not matter much on Intel architecture, you can expect Sun to have a rather different view of the problem.
Offline princec

JGO Kernel


Medals: 364
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #4 - Posted 2004-05-19 10:06:55 »

When all the planets and stars are in place and the tripes are heavy etc. the JVM understands when it can safely use a struct in a buffer without worrying about alignments. Failing that it can fallback to spoofing the whole thing by autogenerating a wrapper class.

Cas Smiley

Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #5 - Posted 2004-05-19 11:51:55 »

Quote
Actually the problem is well known: calling getFloat()/putFloat() on a ByteBuffer is bastardly slow - but if you wrap your ByteBuffer in several sliced up FloatBuffers etc. then you get full speed optimized access.


Is there a bug number for this performance issue?

Offline Herkules

Senior Member




Friendly fire isn't friendly!


« Reply #6 - Posted 2004-05-19 13:05:38 »

does not sound like a bug?

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline princec

JGO Kernel


Medals: 364
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2004-05-19 14:39:17 »

It's not a bug, it's just the way the world works, and you can't expect the VM to optimize it away because it's far too generic.

Structs! Structs! Structs! Structs! Structs!

Cas Smiley

Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #8 - Posted 2004-05-19 17:08:37 »

I don't follow.  Why would you expect getFloat() to be so much slower on a ByteBuffer than a FloatBuffer when ultimately the exact same work must be done?  Assuming of course that the floats are aligned in memory the same in both cases..

Offline abies

Senior Member





« Reply #9 - Posted 2004-05-19 17:47:43 »

Not exactly the same work. Given FloatBuffer will be always aligned or always unaligned. In case of aligned buffer, native fetch can be directly generated by Hotspot, thus requiring no code in the middle (if bounds checks are already done). With ByteBuffer, every getFloat call needs to check if base address plus given offset gives aligned or unaligned result. This extra branch is quite a big cost compared to simple memory fetch.

In worst case, all paths would require the same work. Fortunately, if Hotspot detects that given FloatBuffer is aligned and in native order it can optimize access code in dramatic way.

Artur Biesiadowski
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

JGO Kernel


Medals: 364
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #10 - Posted 2004-05-19 19:01:01 »

Which is why Structs would r0xor so much. Help the VM optimise something that it can't figure out on its own. Save the world! Get the girl!*

Cas Smiley

* and be memory efficient with it!

Offline shawnkendall

Senior Member





« Reply #11 - Posted 2004-05-19 19:24:52 »

Structs are a year+ and counting and no response from JDK reps...

Shawn Kendall
Cosmic Interactive, LLC
http://www.facebook.com/BermudaDash
Offline princec

JGO Kernel


Medals: 364
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #12 - Posted 2004-05-19 20:58:34 »

Mmm. Despite my relatively frequent direct and indirect communications with GTG members, too.

Cas Smiley

Offline JasonB

Junior Member





« Reply #13 - Posted 2004-05-19 23:16:29 »

Quote
Mmm. Despite my relatively frequent direct and indirect communications with GTG members, too.

Ahem.

What a shame Java's not open source so it could be a community effort to add structs.   Grin

Sorry.  Couldn't resist.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (13 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (31 views)
2014-08-16 06:45:27

Tekkerue (30 views)
2014-08-16 06:22:17

Tekkerue (19 views)
2014-08-16 06:20:21

Tekkerue (29 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (42 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
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!