Ruby efficiency for large number of operations
-
add_face compare the added face to the existing geometry in the active context. So the more geometry in that context, the slower it gets.
-
@drewmorgan said:
Is it just that the model is growing, and when I do add_face it uses the existing faces in the model somehow during that operation? I'm guessing that's what you meant by geometry merging..
Yes. Entity.add_face (or line or anything) isn't just a simple method that blindly adds raw geometry, it tries to merge vertices and split edges etc - applying much of the SketchUp "stickyness" magic. It's good for tools where you add a face or edge at a time based on user input in a container with existing geometry, but for what you are going generating a large lump of geometry it quickly gets slow.
PolygonMesh does less of this - though it will merge vertices.
-
Ok, that makes sense.. Unforuntately, though, I am still seeing essentially the same behavior.. It does seem to be better - it doesn't hang until like 1200 now, but still slows down to a crawl and eventually a hang..
I tried making a group for each item I want to add (which would be ideal) and also moved the group and fill_from_mesh call out of the loop, which didn't seem to make a difference..
Just saw tt_su's post indicating that PolygonMesh still merges vertices.. That must be what's causing the problem, but is there any way around it? 20000 meshes doesn't seem like such a big number to me, so I'm surprised its so problematic.. Is this just something I can't do with SKP? I'm actually not trying to generate a "good SketchUp model", but rather just trying to get a visualization.. Maybe Rhino or something would be better at this type of thing?
I could do some conversion outside of SKP I suppose, but it doesn't seem like a format conversion issue, it seems like a number of points issue, so I don't think that would provide any benefit.. Hmmm..
-
You mentioned you have the console open. At least in older versions, the console is slow. Sometimes I get the idea it rewrites the whole content with every line added. In SU14 it is supposed to be slightly better, and there is a secret clear command, but still it is best when you output only relevant data to the console and only during development (later only errors labeled with Our plugin's name).
-
Well, reducing the console output helped.. I printed out only every 500th item, and over about 30 or 45 minutes it made it to 5500 (of 20000), which is the farthest I've seen it get yet.. But it still ended up not responding and having to be killed.. Maybe it just simply too many points for it to handle? I have to believe SKP supports large facet-count models reasonably well.. Does SKP performance degrade that significantly when you want to add a face to a large facet/point count model, or is it something specific with the Ruby API? As I mentioned before, I'm talking a total of ~20,000 rectangular facets, so I wouldn't normally even consider that a high facet count or high point count..
I might play with it a little more, but I'll probably resort to Rhino.. If I make progress on the SKP front, I will update the thread, and if there's any other suggestions or thoughts you all have, I'd still be happy to hear them, because the SketchUp solution would be far preferred.. Thanks!
-
Can we see some sample code?
As Andreas mentioned, Ruby Console output significantly slow down code execution. Having the outliner open might also affect performance.
I'd suggest you test performance by not outputting to the console.
The absolutely fastest was I have found is:
Have a rough estimate of how many points and polygons you will generate and use this when you initialize the new PolygonMesh. Internally this reserves space for all the data so we avoid too many memory allocations.
Then generate all the unique points to be used, add them to the mesh and take the index returned by PolygonMesh.add_point and store it in a Hash for quick retrieval later on. Then add your polygons using the indices you built in the previous step.You should be able to generate 20.000 triangles in less than a second or two.
-
You should post your data file, so that we can try.
Personally, I think you can probably generate the 20,000 faces in less than a minute in SU14Fredo
-
@fredo6 said:
Personally, I think you can probably generate the 20,000 faces in less than a minute in SU14
For sure! But forget minutes - it should be around a second.
-
I made up a small test program that uses the same general flow as my real case, and it produces the same result, so I can post that.. I also generated a simple test file.. This is not the actual file I'm trying to use, but just a test case generated with a script, so its obviously very regular and not very interesting..
Given what you guys are saying, I'm more confident its something wrong with my code, so looking forward to your responses.. Thanks a lot!
The Ruby script from my plugins dir
-
Thomthom was right, time is in seconds and the longest is to read the file and generate the Polygon Mesh (4 sec) whereas the face generation takes 0.5 seconds.
This is the output with your 20000 quad faces
[highlight=#ffff00:2915pieu]Reading File and creating Polygon Mesh....
Nfaces = 20000 - Creating Mesh time = 4.291246
Generation of Faces [20000] - Time = 0.517029[/highlight:2915pieu]Here is the script for that. Note that it inserts a menu item in the Plugins menu
drewmorgan - test_skp_efficiency.rbFredo
-
It would probably be possible to squeeze even more performance out of it, but it'd get increasingly more challenging.
If you where able to pre-process all the 3d points first and index them it would save more time. As PolygonMesh is doing a linear search for each point you add.
However, as the data is structured in your sample file this would probably be too slow to do in Ruby. But if you wrote a small Ruby C Extension to read the file and compute the unique set of points needed in C before converting it to Ruby it should take a couple of seconds of the time to create the PolygonMesh. -
I thought PolygonMesh would do that, based on C code. So doing it in Ruby would not help for performance (which is already quite good).
Fredo
-
Yes, PolygonMesh will merge the points using C, but it's still doing it very inefficiently. It does a linear search each time I received a Point3d object. So when you grow to a large set of points this slows down noticeably.
Which is why you will get best performance out of it if you use add_point first before using add_polygon (with indices). -
@tt_su said:
Which is why you will get best performance out of it if you use add_point first before using add_polygon (with indices).
Do you mean that add_polygon will accept indexes instead of points? This is not obvious from the doc!
If so, then yes, we can probably improve performance.Thanks for the tip
Fredo
-
@fredo6 said:
Do you mean that add_polygon will accept indexes instead of points? This is not obvious from the doc!
Correct. I just checked the docs again - I was working from memory - and you're right. It's not obvious, it's only mentioned in add_point.
Using indices you can also control the soft/smooth or hidden properties of the edges bounding the polygons you add. See Entities.fill_from_mesh for more info on how that is controlled.
-
Sorry I've not gotten back to you lately - I got pulled off this for a while..
The program you provided is definitely a lot faster and seems to work well, except for in the 20000 face test case, it causes SkecthUp to crash.. It seems to be during the fill_from_mesh call, because it prints out a message to console right before that happens.. But then I get a window that pops up saying "SketchUp Application has stopped working", and it crashes.. It seems to work on some test files I have with a lower number of faces, so maybe its just too many faces for a single polymesh? Does that seem reasonable, or do you think the crash is due to something else?
A couple of observations / things I've learned:
-
If I make each face be its own group (i.e. make a polymesh INSIDE the loop and fill_from_mesh inside the loop as well) it goes slooooooow. Ideally for my plugin, each one of these would be their own group (of course, what I originally was trying to do is a bit more complicated so each one would be more than a single plate).. I wonder if there's a way to get each one to be a separate group but still be reasonably fast? Based on our previous discussion, I'm guessing once you "fill_from_mesh", it then checks the points that are read in later will all the previous ones, whereas if you just put them all in the polymesh it doesn't do those checks?
-
If there's more than one implementation of the ruby function in the same directory, SKP will not provide an error message, but will pick one and use that.. This made me think I was losing my mind as I was modifying stuff left and right and not seeing any updates. Turns out when I created the file to post here, I just wrote a subset of my main plugins file to the same dir. All my mods were to my main file, but apparently, SKP was using the (unmodified) implementation in the subset file.. Argh. That took me far longer to figure out than it should have.
-
-
@drewmorgan said:
The program you provided is definitely a lot faster and seems to work well, except for in the 20000 face test case, it causes SkecthUp to crash.. It seems to be during the fill_from_mesh call, because it prints out a message to console right before that happens.. But then I get a window that pops up saying "SketchUp Application has stopped working", and it crashes.. It seems to work on some test files I have with a lower number of faces, so maybe its just too many faces for a single polymesh? Does that seem reasonable, or do you think the crash is due to something else?
20000 faces shouldn't crash SU, no.
Did you submit the BugSplat? If you did, did you enter some details that I can use to look it up? -
@drewmorgan said:
- If I make each face be its own group (i.e. make a polymesh INSIDE the loop and fill_from_mesh inside the loop as well) it goes slooooooow. Ideally for my plugin, each one of these would be their own group (of course, what I originally was trying to do is a bit more complicated so each one would be more than a single plate).. I wonder if there's a way to get each one to be a separate group but still be reasonably fast?
Got a sample snippet for this? When optimizing for speed it's hard to talk generically. Having a common set of sample code to refer to will help.
@drewmorgan said:
Based on our previous discussion, I'm guessing once you "fill_from_mesh", it then checks the points that are read in later will all the previous ones, whereas if you just put them all in the polymesh it doesn't do those checks?
I'm sorry, but I don't quite follow what you mean here.
fill_from_mesh require that you add the PolygonMesh to an empty Entities collection. It then simply builds the PolygonMesh without doing any more magic.@drewmorgan said:
- If there's more than one implementation of the ruby function in the same directory, SKP will not provide an error message, but will pick one and use that.. This made me think I was losing my mind as I was modifying stuff left and right and not seeing any updates. Turns out when I created the file to post here, I just wrote a subset of my main plugins file to the same dir. All my mods were to my main file, but apparently, SKP was using the (unmodified) implementation in the subset file.. Argh. That took me far longer to figure out than it should have.
That's Ruby for you, it let you override anything. The last one to load will be the one running.
-
@tt_su said:
@drewmorgan said:
The program you provided is definitely a lot faster and seems to work well, except for in the 20000 face test case, it causes SkecthUp to crash.. It seems to be during the fill_from_mesh call, because it prints out a message to console right before that happens.. But then I get a window that pops up saying "SketchUp Application has stopped working", and it crashes.. It seems to work on some test files I have with a lower number of faces, so maybe its just too many faces for a single polymesh? Does that seem reasonable, or do you think the crash is due to something else?
20000 faces shouldn't crash SU, no.
Did you submit the BugSplat? If you did, did you enter some details that I can use to look it up?When I get the crash, I don't get an option to send in the BugSplat report. Do I have to activate that somewhere? I poked around in the preferences, and maybe missed it? I'm only given the option to "Close the program" or "Close the program and look for a solution".. Down in the "details" section, it mentions BugSplat.dll but there's no option to submit it.. Sorry to be dense!
-
@tt_su said:
@drewmorgan said:
- If I make each face be its own group (i.e. make a polymesh INSIDE the loop and fill_from_mesh inside the loop as well) it goes slooooooow. Ideally for my plugin, each one of these would be their own group (of course, what I originally was trying to do is a bit more complicated so each one would be more than a single plate).. I wonder if there's a way to get each one to be a separate group but still be reasonably fast?
Got a sample snippet for this? When optimizing for speed it's hard to talk generically. Having a common set of sample code to refer to will help.
@drewmorgan said:
Based on our previous discussion, I'm guessing once you "fill_from_mesh", it then checks the points that are read in later will all the previous ones, whereas if you just put them all in the polymesh it doesn't do those checks?
I'm sorry, but I don't quite follow what you mean here.
fill_from_mesh require that you add the PolygonMesh to an empty Entities collection. It then simply builds the PolygonMesh without doing any more magic.Sorry - not very clear.. What I meant was, in fredo6's posted script, he creates one big PolygonMesh outside the loop over faces, and calls "add_polygon" for each face inside the loop.. Then, after the loop, he creates a new group and calls fill_from_mesh to include all the faces all at once.. That seems to be working fairly quickly. However, if I move the creation of the PolygonMesh inside the loop, add a single face to it, create a new group, and call fill_from_mesh all inside the loop, it crawls and eventually hangs like it was originally..
So, this seems quick:
model = Sketchup.active_model numItemsLine = testFile.gets numItems = numItemsLine.split(" ")[2].to_i polyMesh = Geom;;PolygonMesh.new(4 * numItems, numItems) for i in 0..numItems - 1 ##... polyMesh.add_polygon [face0Vert0, face0Vert1, face0Vert2, face0Vert3] end newGroup = model.active_entities.add_group newGroup.entities.fill_from_mesh polyMesh, true
but this is slow:
model = Sketchup.active_model numItemsLine = testFile.gets numItems = numItemsLine.split(" ")[2].to_i for i in 0..numItems - 1 ##... polyMesh = Geom;;PolygonMesh.new(4, 1) polyMesh.add_polygon [face0Vert0, face0Vert1, face0Vert2, face0Vert3] newGroup = model.active_entities.add_group newGroup.name = "itemNum" + i.to_s newGroup.entities.fill_from_mesh polyMesh, true end
Advertisement