Re: resource.c - use of recursion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Dec 26, 2001 at 04:04:45PM +0530, Mohit Kalra wrote:
> I was going through the resource.c in 2.4 and saw the function called
> do_resource_list
> 
> 
> static char * do_resource_list(struct resource *entry, const char *fmt, int
> offset, char *buf, char *end)
> {
> /* code that I am deleting to make this short*/
> 
> 
>                 if (entry->child)
>                         buf = do_resource_list(entry->child, fmt, offset-2,
> buf, end);
>                 entry = entry->sibling;
> 
> ..
> ..
> }
> 
> 
> We are usually advised not to use recursion in the kernel code .... and i
> guess the reasons are
> 1. The kernel stack is limited to the 2 four KB pages (8KB) for x86
> 2.  This is slow coz it requires pushing and popping of stuff from the
> stack.

Depends on the architecture. On ARM the first four function arguments
will go into registers and only the fifth will be pushed on the stack.
Besides, the stack will go into the D-cache, so it's not *that* slow.

> -1  But ofcourse the readability is much better above.
> 
> The above can be rewritten in non recursive way as way.  Now my problem is
> not why the above is not non-recursive but i want to ask that upto what
> limits is it ok to use recursion.   Was some calculation done on the max
> possible limit of the global resource tree.
> The use of recursion may be pretty desirable for itz simplicity and easy of
> use.

The maximum level of recursion I get on my laptop is only one, so it's
not that deep:

  fcf0-fcff : Intel Corp. 82440MX EIDE Controller
     fcf0-fcf7 : ide0

This leaves plenty of room on the stack.

> Also wouldn't it be better that in order to match the readability as above,
> a kernel programmer should implement the non - recursive code and put in a
> comment as shown below

The question is if it is worth doing it non-recursively. The code in
question it not in a hot path, so it doesn't need heavy optimisation.
In that case readability is preferred over speed. The recursive code
might even be smaller.


Erik

-- 
J.A.K. (Erik) Mouw, Information and Communication Theory Group, Faculty
of Information Technology and Systems, Delft University of Technology,
PO BOX 5031, 2600 GA Delft, The Netherlands  Phone: +31-15-2783635
Fax: +31-15-2781843  Email: J.A.K.Mouw@its.tudelft.nl
WWW: http://www-ict.its.tudelft.nl/~erik/
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
IRC Channel:   irc.openprojects.net / #kernelnewbies
Web Page:      http://www.kernelnewbies.org/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux