Identify and eliminate dead code (100+ lines) #2BRL-CAD
Status: ClosedTime to complete: 96 hrs Mentors: Popescu Andrei, Mihai Neacsu, SeanTags: C, programming, reduction, refactoring, dead code

BRL-CAD is huge. With any large body of code, one inevitably ends up with a mix of good and bad coding practices. On the whole, BRL-CAD is actually better than most but we are constantly working on improving the code. This includes eliminating DEAD code, i.e., code that is not called or used by anything.

References:

  • http://en.wikipedia.org/wiki/Dead_code
  • http://brlcad.org/wiki/Code_Cleanup
  • http://brlcad.org/wiki/Compiling
  • http://brlcad.org/wiki/SVN

This task involves reducing BRL-CAD's source code by 100 or more lines of code by eliminating DEAD code. You can use whatever method you like to identify dead code, but beware that there are more than 1 million lines of code in BRL-CAD, so you're not likely going to find this duplication just by browsing. You also cannot eliminate "unused" functions that are declared in our public API headers (i.e., our include/ headers).

We suggest using a dead code detection tool, but beware false-positives. You can eliminate a single chunk of dead code or (more likely) several smaller sections of dead code to get to 100+ lines.

One manual way to find dead code is to convert non-library functions (i.e., functions not in the src/lib* directories and not declared in an include/ header) to static functions. If declared static and not used anywhere, GCC will report it as an unused function during compilation (or throw an error if you're wrong).

Another way to find dead code is through scripting. You can write a script to extract a list of all function definitions, then search for any that have no uses (in any directory). A little more tricky to script it right, but you could easily find thousands of lines this way. We'll create a follow-on task for you to submit your script if you write one this way.

Download our latest Subversion trunk sources and make sure you can compile cleanly first. Then you can do whatever you need to find dead code. Make your edits, then make sure the code still compiles (run "make", "make test", "make regress" and "make benchmark" to test your changes)! This is critical, it must still compile exactly as before. Finally, create and submit a patch file of your changes (see the references, svn will create the patch file for you).

Feel free to join the brlcad-devel mailing list or IRC channel to discuss your changes beforehand.

Uploaded Work
File name/URLFile sizeDate submitted
_comb_12.2 KBDecember 17 2014 13:43 UTC
dead.patch14.4 KBDecember 29 2014 12:52 UTC
Comments
Adarshon December 3 2014 20:44 UTCTask Claimed

I would like to work on this task.

Deepak on December 3 2014 20:45 UTCTask Assigned

This task has been assigned to Adarsh. You have 100 hours to complete this task, good luck!

Melange on December 7 2014 00:45 UTCTask due soon

There are less than 24 hours left until the deadline, please submit your work soon.

Melange on December 8 2014 00:45 UTCTask Reopened

Melange has detected that the final deadline has passed and it has reopened the task.

Marc Tannouson December 16 2014 13:16 UTCTask Claimed

I would like to work on this task.

Popescu Andrei on December 16 2014 13:16 UTCTask Assigned

This task has been assigned to Marc Tannous. You have 96 hours to complete this task, good luck!

Marc Tannouson December 17 2014 13:46 UTCWhat I did

1) After running Simian, found duplicate code in g-step/comb.c g-ap214/comb.c


2) Apparently, they were both declaring the same function, and they both included a "Comb.h" used in their own directory.


3) I created a new file, Comb.h that was in both their parent directory ( they were both coming from /src/conv/step so I created it here ) with the entire contents of the old Comb.h + the function they were both using, and included it in the two files.


4) Eliminated the duplicated code, included the newly formed Comb.h, removed the old Comb.h that now served no purpose and created a patch.


Regards,


Marc

Marc Tannouson December 17 2014 13:46 UTCReady for review

The work on this task is ready to be reviewed.

Daniel_R on December 17 2014 14:32 UTCWrong topic?

This task is about DEAD but not DUPLICATED code, e.g. nowhere used functions.

Daniel_R on December 17 2014 14:32 UTCTask Needs More Work

One of the mentors has sent this task back for more work. Talk to the mentor(s) assigned to this task to satisfy the requirements needed to complete this task, submit your work again and mark the task as complete once you re-submit your work.

Marc Tannouson December 17 2014 14:35 UTC???

http://www.google-melange.com/gci/task/view/google/gci2014/5893656028381184

Same thing, Sean closed it. Why would Simian be mentioned if I am looking for dead code?

Marc Tannouson December 17 2014 14:36 UTCReady for review

The work on this task is ready to be reviewed.

Marc Tannouson December 17 2014 14:38 UTC

It was the exact same title, did not read the description, have no idea why they differ. What should I do?

Daniel_R on December 17 2014 14:48 UTCTask Reopened

This task has been Reopened.

shardulcon December 28 2014 07:53 UTCTask Claimed

I would like to work on this task.

Gauravjeet Singh on December 28 2014 08:50 UTCTask Assigned

This task has been assigned to shardulc. You have 96 hours to complete this task, good luck!

shardulcon December 29 2014 07:15 UTCDead File

What do I do if I find a file which defines only one function, which is not used anywhere? Do I remove the file and the corresponding entries from CMakeLists.txt and the declaring header file? (I can get a clean build this way.) But this seems a bit counter-intuitive because I could extend the process indefinitely, ending up with nothing at all.


Thanks for your attention!

Mihai Neacsu on December 29 2014 07:24 UTC

shardulc,


If you do come across that sort of file, then by all means, remove it from all your above mentioned points.  

shardulcon December 29 2014 12:52 UTCReady for review

The work on this task is ready to be reviewed.

shardulcon December 29 2014 12:58 UTCDead Code Script

I did this task by writing my own dead code detection script. The script is in Python, and uses a little regex to find function declarations and uses. It's probably buggy and gives out a lot of false positives, but it doesn't miss any dead functions (I hope). It could be faster if I used grep instead of Python's re module, which I am working on. But it works! Can I submit it?


The patch I've uploaded contains the changes I made after running my script on the src/conv directory to look for function definitions, and searching for calls to them everywhere in the src/ directory. It is more than 100 lines if deleted files are counted.


Thanks for your attention!

Sean on December 30 2014 06:14 UTCawesome

shardulc, that's awesome.  You absolutely can submit it.


I'll add another task to create a dead code detection script just for this.  The only thing you'll have to be careful of is that you don't eliminate any functions that are declared publicly (in the top-level include/ directory).  Even if we don't use that function, external applications might.


I presume you're going to go after all the other dead code elimination tasks too?  :)


It's still plenty of work to identify the code, edit the files, test the compilation, run our regression tests, etc.


 

Sean on December 30 2014 06:14 UTCTask Closed

Congratulations, this task has been completed successfully.

shardulcon December 30 2014 09:03 UTC

The other open dead code elimination tasks are all beginner tasks, so I can't do them anymore. Also, you don't have to worry about functions in the libraries or public headers because the script can be run on specific directories.

Sean on January 14 2015 19:45 UTCfollow-on

https://www.google-melange.com/gci/task/view/google/gci2014/6455158829481984

Sean on January 14 2015 19:47 UTCalso

Also, all the remaining tasks to eliminate dead code are no longer marked as beginner tasks.  Let me know if you need more! :)