Ethereal-dev: Re: [Ethereal-dev] Programming style/performance question

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Guy Harris <gharris@xxxxxxxxx>
Date: Wed, 28 Aug 2002 11:43:24 -0700
On Wed, Aug 28, 2002 at 12:40:40PM +0200, Yaniv Kaul wrote:
> Which is better?

"It depends". :-)

> 1. proto_tree_add_item(ntree, hf_ip_addr, tvb, offset, SIZE_IP_ADDR, 0);

(Actually, I generally prefer using "TRUE" or "FALSE" as the last
argument, to make it a bit more obvious what you're telling
"proto_tree_add_item()".)

I consider that one preferable if

	1) the IP address being added to the tree comes straight from
	   the packet, rather than being computed

and

	2) you haven't fetched that IP address into a variable already

as

	1) you don't have to write code to fetch it from the packet -
	   "proto_tree_add_item()" does it for you

and

	2) that means that you don't have to put "tvb", "offset", and
	   SIZE_IP_ADDR into two calls (and make sure they're the same
	   in both calls).

You also don't have to make sure the "XXX" in "proto_tree_add_XXX"
matches the type of the header field.

> 2. proto_tree_add_ipv4(ntree, hf_ip_addr, tvb, offset, SIZE_IP_ADDR, ip);

If "ip" is computed rather than directly fetched, that's the only call
that works.

If you've already fetched it for other reasons, that call is more
efficient, but it might not be sufficiently more efficient to override
some of the other advantages.

So I tend to prefer 1), in general.