logo logo

 Back to main page

The NWNX Community Forum

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
nwn-lib erf bottleneck solved (mostly Attn: elven)

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Linux development
View previous topic :: View next topic  
Author Message
Fireboar



Joined: 17 Feb 2008
Posts: 323

PostPosted: Tue Mar 27, 2012 2:06    Post subject: nwn-lib erf bottleneck solved (mostly Attn: elven) Reply with quote

I noticed recently that building a large-ish (over 6k files) module (or hak, or erf) using nwn-lib's Erf class takes a long time (over a minute) - slightly longer than the toolset itself. This didn't seem right, so I did some investigating to find the bottleneck. After my investigation I propose the following patch.

Code:
diff -u a/erf.rb b/erf.rb
--- a/erf.rb   2012-03-27 01:02:35.000000000 +0100
+++ b/erf.rb   2012-03-27 01:03:01.000000000 +0100
@@ -133,12 +133,12 @@
           [c.resref, @content.index(c), c.res_type, 0].pack("a#{fnlen} V v v")
         }.join("")
 
-        pre_offset = 160 + locstr.size + keylist.size + 8 * @content.size
+        offset = 160 + locstr.size + keylist.size + 8 * @content.size
 
         reslist = @content.map {|c|
-          offset = pre_offset +
-            @content[0, @content.index(c)].inject(0) {|sum,x| sum + x.size }
-          [offset, c.size].pack("V V")
+          r = [offset, c.size].pack("V V")
+          offset += c.size
+          r
         }.join("")
 
         offset_to_locstr = 160


The bottleneck is in calculating the offset - the original code sums the size each time and generates sub-arrays. My alteration is simpler: it simply keeps a running offset total, relying on the fact that arrays always enumerate in order. In my tests, this reduced the time taken from over a minute to under 5 seconds.
Back to top
View user's profile Send private message
elven



Joined: 28 Jul 2006
Posts: 259
Location: Germany

PostPosted: Tue May 01, 2012 23:47    Post subject: Reply with quote

Hi,

thanks for reporting this. :) I was away for a while, and saw this thread just now (the forum didn't notify me either). Feel free to open issues on github next time, too - that's the official bugtracker for this project.

Anyways:

I'm not really sure why I did it that way when I wrote that code. I've committed your changes in master. They'll end up in the new release that I'll get out one of these days - including native ruby1.9 support.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Linux development All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group