Geometry generation takes very long

Hello again. I wrote a mesh generator in python and to me it seems that it runs a lot slower than it should. Geom with 3084 vertices takes about 0.45 seconds to generate. As I was browsing forum I noticed that other people are getting a lot better results. Why could this take so long for me?

First, Python is not terribly fast for processing many vertices, though that does seem particularly slow.

Are you running Windows, or something else? Have you tried it with the latest buildbot release?

David

At the moment I am trying to rewrite generator so it would add all vertex at once via GeomVertexDataHandle.setData(). I will post results.

Btw, my OS is Windows 7 and I tried latest buildbot release.

How data, which will be supplied to GeomVertexArrayDataHandle.setData(string data) should be formatted??? I can’t find information on this anywhere, just that it has to be a string. At first I thought it should be bytes, but there can be negative vertex values too so it’s doesn’t fit.

It’s the raw buffer data. The type of the data depends on the format of the vertex data, e.g. a 4-component float32 column will use four 4-byte floating points.

You probably don’t want to mess with that - GeomVertexWriter takes care of it.

If you know how many vertices you will be adding ahead of time, you can save some time by calling vdata.setNumRows(expectedVertices).

David

I tried this. It decreased generation time by 0.04~ seconds And finally I managed to rewrite generator and performance is still absolutely the same. No speed improvement whatsoever.

Try using the Python profiler to narrow down where your time is being spent.

David

In geom.addPrimitive(). Also few people on the chat tested my code and they got results: 0.75 and 0.90~. It appears it’s really that slow. However I noticed that it gets exponentially slower the more vertexes are already added in the Geom. So it’s actually 22 times faster to make one small mesh size of 1/4 big mesh, which I need.

I assume you mean geomPrimitive.addVertex()? Right, there is a minor flaw in the 1.7.x branch and earlier that may force it to reallocate its buffer at each increment, especially on Windows, which accounts for the exponential slowdown you are seeing.

This is what I recently fixed on the trunk, so this method should be a lot faster in the buildbot release. If nothing else, on the buildbot release there should be a new method, geomPrimitive.reserveNumVertices(), which you can call at the beginning to pre-allocate all of the space for the number of times you will subsequently call addVertex().

Unless you really do mean geom.addPrimitive(), which is strange because usually you wouldn’t call that method more than once when constructing geometry. Are you constructing a different GeomTriangles object for each triangle?

David

Yes I am. But I’ve also written a version of generator where I don’t do that and it’s still as slow or even slower.

Here are these two versions. Maybe you’ll notice something out of order. Second one isn’t done yet. I was too lazy to figure out how exactly to store vertices so resulting mesh is all messed up.

pastebin.com/QgMPQX4B

pastebin.com/2W1bJxxQ

EDIT: As I just noticed slow performance of second version is caused by python itself.

And there’s cython version, which compiles fine, but crashes when calling self.vertexWriter.add_data3f()

pastebin.com/Sm8aSNR9

Your first implementation is the right idea, but you should create just one GeomTriangles object, and keep appending to it. Otherwise you will suffer not only at generation time, but also at render time–each GeomTriangles object must be sent individually to the graphics card in a separate call, so you really want to have as few of these objects as you can.

Your second implementation goes too far the other way–it attempts to do absolutely everything using Python calls, avoiding as many of the Panda calls as possible. This will generally tend to be very slow. Panda exists to do the heavy lifting that Python wasn’t built for. :wink:

I don’t know anything about Cython or why it would be crashing. I suspect perhaps the reference counts are not being held of the dynamic objects you are creating.

David

Thank you very much! :smiley: I did what you said and now generation is about 15~ times faster and fps is 7 times bigger.

Though I didn’t notice any performance difference between yesterdays buildbot release and default 1.7.1.

Maybe it’s more noticeable with bigger meshes.