Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Just go with the flow control, roll with the crunches, and, when you get a prompt, type like hell.


devel / comp.lang.c / Re: Function Pointers...

SubjectAuthor
* Function Pointers...Mike Sanders
+* Re: Function Pointers...Scott Lurndal
|`* Re: Function Pointers...Mike Sanders
| `* Re: Function Pointers...Scott Lurndal
|  +- Re: Function Pointers...Chris M. Thomasson
|  `- Re: Function Pointers...Mike Sanders
+* Re: Function Pointers...bart
|`* Re: Function Pointers...Mike Sanders
| `- Re: Function Pointers...Mike Sanders
+* Re: Function Pointers...Malcolm McLean
|`- Re: Function Pointers...Mike Sanders
+* Re: Function Pointers...Lawrence D'Oliveiro
|`* Re: Function Pointers...Mike Sanders
| `- Re: Function Pointers...Lawrence D'Oliveiro
+* Re: Function Pointers...Tim Rentsch
|`* Re: Function Pointers...Mike Sanders
| `* Re: Function Pointers...Malcolm McLean
|  `- Re: Function Pointers...bart
`* Re: Function Pointers...David Brown
 `- Re: Function Pointers...bart

1
Function Pointers...

<uqr7ne$jkp9$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33387&group=comp.lang.c#33387

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: porkc...@invalid.foo (Mike Sanders)
Newsgroups: comp.lang.c
Subject: Function Pointers...
Date: Sat, 17 Feb 2024 21:16:30 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 28
Sender: Mike Sanders <busybox@sdf.org>
Message-ID: <uqr7ne$jkp9$1@dont-email.me>
Injection-Date: Sat, 17 Feb 2024 21:16:30 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="78e07104eb5e89122762fe3a1aa0a15f";
logging-data="643881"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+/52zLSAXYZEwvpw+6tmxo"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
Cancel-Lock: sha1:bvEX/ozxPb3ufJNFwTENz5WP26w=
 by: Mike Sanders - Sat, 17 Feb 2024 21:16 UTC

For a good while I've been wanting to use function pointers
& I finally found an opportunity to do so...

Currently I have:

switch(ops.export) {
case 1: outputTXT(p, nodes, c); break;
case 2: outputCSV(p, nodes, c); break;
case 3: outputSQL(p, nodes, c); break;
case 4: outputHTM(p, nodes, c); break;
}

But instead I could use:

void (*output[])(FILE*, BLOCK[], int) =
{ outputTXT, outputCSV, outputSQL, outputHTM };

// do stuff

output[ops.export -1](p, nodes, c);

Now here's where I'm unsure: Perhaps the overhead of an indirect call
is miniscule, even negligible. But there *IS* additional overhead when
using function pointers correct?

--
:wq
Mike Sanders

Re: Function Pointers...

<Kt9AN.355935$7sbb.84907@fx16.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33388&group=comp.lang.c#33388

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Function Pointers...
Newsgroups: comp.lang.c
References: <uqr7ne$jkp9$1@dont-email.me>
Lines: 39
Message-ID: <Kt9AN.355935$7sbb.84907@fx16.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sat, 17 Feb 2024 21:23:54 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sat, 17 Feb 2024 21:23:54 GMT
X-Received-Bytes: 2007
 by: Scott Lurndal - Sat, 17 Feb 2024 21:23 UTC

porkchop@invalid.foo (Mike Sanders) writes:
>For a good while I've been wanting to use function pointers
>& I finally found an opportunity to do so...
>
>Currently I have:
>
>switch(ops.export) {
> case 1: outputTXT(p, nodes, c); break;
> case 2: outputCSV(p, nodes, c); break;
> case 3: outputSQL(p, nodes, c); break;
> case 4: outputHTM(p, nodes, c); break;
>}
>
>But instead I could use:
>
>void (*output[])(FILE*, BLOCK[], int) =
> { outputTXT, outputCSV, outputSQL, outputHTM };
>
>// do stuff
>
>output[ops.export -1](p, nodes, c);
>
>Now here's where I'm unsure: Perhaps the overhead of an indirect call
>is miniscule, even negligible. But there *IS* additional overhead when
>using function pointers correct?

Not generally. In both cases, the function call machine instruction
needs the address of function. Loading a register with
the function pointer (if it is not already available,
for example as an offset from already loaded base register)
may or may not add to the instruction count; depending
on what relative branch size(s) the function call instruction
supports even a direct call may require the compiler to
load a function pointer into a register anyway.

In your example, it depends on how the compiler implements
the switch statement - it could indeed just index into
a four element array containing the function pointers
when compiled with the appropriate optimization flags.

Re: Function Pointers...

<uqr8kf$jnoa$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33389&group=comp.lang.c#33389

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sat, 17 Feb 2024 21:32:00 +0000
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <uqr8kf$jnoa$1@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 17 Feb 2024 21:32:00 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="7009945b173c7ecad42f653af61e973c";
logging-data="646922"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gqDInZzQ9dYaOL1VbDKz0sS3teod7F+8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:izd2Z63ecvxF+KoXUa7SZxJUD+0=
In-Reply-To: <uqr7ne$jkp9$1@dont-email.me>
Content-Language: en-GB
 by: bart - Sat, 17 Feb 2024 21:32 UTC

On 17/02/2024 21:16, Mike Sanders wrote:
> For a good while I've been wanting to use function pointers
> & I finally found an opportunity to do so...
>
> Currently I have:
>
> switch(ops.export) {
> case 1: outputTXT(p, nodes, c); break;
> case 2: outputCSV(p, nodes, c); break;
> case 3: outputSQL(p, nodes, c); break;
> case 4: outputHTM(p, nodes, c); break;
> }
>
> But instead I could use:
>
> void (*output[])(FILE*, BLOCK[], int) =
> { outputTXT, outputCSV, outputSQL, outputHTM };
>
> // do stuff
>
> output[ops.export -1](p, nodes, c);
>
> Now here's where I'm unsure: Perhaps the overhead of an indirect call
> is miniscule, even negligible. But there *IS* additional overhead when
> using function pointers correct?
>

With a switch, sometimes the function call can be inlined. That can
speed up something like bytecode dispatching in an interpreter where you
might be doing 100s of millions of calls per second.

But judging from the names of your functions, each one looks to be
writing stuff out to a file. Then the function call overhead will be
insignificant.

Note that switch will also check that the ops.export index is in the
correct range; if less than 1 or greater than 4, nothing is called, but
the function table version will go wrong.

BTW you seem to have mixed up 1-based and 0-based here: 1 means TXT in
the switch version, but CSV in the function pointer version since arrays
are 0-based.

Re: Function Pointers...

<uqr9k5$k1e9$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33390&group=comp.lang.c#33390

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: porkc...@invalid.foo (Mike Sanders)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sat, 17 Feb 2024 21:48:54 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 26
Sender: Mike Sanders <busybox@sdf.org>
Message-ID: <uqr9k5$k1e9$1@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me> <Kt9AN.355935$7sbb.84907@fx16.iad>
Injection-Date: Sat, 17 Feb 2024 21:48:54 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="78e07104eb5e89122762fe3a1aa0a15f";
logging-data="656841"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19/MybmZXQeZ3h9c7sT8CzJ"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
Cancel-Lock: sha1:pESQPd6Lo4Sz8e9TG4WVzQEubuI=
 by: Mike Sanders - Sat, 17 Feb 2024 21:48 UTC

Scott Lurndal <scott@slp53.sl.home> wrote:

> Not generally. In both cases, the function call machine instruction
> needs the address of function. Loading a register with
> the function pointer (if it is not already available,
> for example as an offset from already loaded base register)
> may or may not add to the instruction count; depending
> on what relative branch size(s) the function call instruction
> supports even a direct call may require the compiler to
> load a function pointer into a register anyway.
>
> In your example, it depends on how the compiler implements
> the switch statement - it could indeed just index into
> a four element array containing the function pointers
> when compiled with the appropriate optimization flags.

Thanks Scott.

I keep reading otherwise (comfused about it all really):

<https://www.google.com/search?q=do+function+pointers+incur+overhead>

--
:wq
Mike Sanders

Re: Function Pointers...

<uqra17$k1e9$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33391&group=comp.lang.c#33391

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: porkc...@invalid.foo (Mike Sanders)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sat, 17 Feb 2024 21:55:51 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 27
Sender: Mike Sanders <busybox@sdf.org>
Message-ID: <uqra17$k1e9$2@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me> <uqr8kf$jnoa$1@dont-email.me>
Injection-Date: Sat, 17 Feb 2024 21:55:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="78e07104eb5e89122762fe3a1aa0a15f";
logging-data="656841"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+J8lY4JWWyp2Lhr/h9H8QE"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
Cancel-Lock: sha1:hynRjKU80LdaWd0MRJ75UtWIlVA=
 by: Mike Sanders - Sat, 17 Feb 2024 21:55 UTC

bart <bc@freeuk.com> wrote:

> But judging from the names of your functions, each one looks to be
> writing stuff out to a file. Then the function call overhead will be
> insignificant.

Hi bart.

So Scott seems to imply as well, I'll likly use the function pointer.
> Note that switch will also check that the ops.export index is in the
> correct range; if less than 1 or greater than 4, nothing is called, but
> the function table version will go wrong.
>
> BTW you seem to have mixed up 1-based and 0-based here: 1 means TXT in
> the switch version, but CSV in the function pointer version since arrays
> are 0-based.

ops.export is always 1 to 4, so:

output[ops.export -1](p, nodes, c);

keeps it within bounds of zero indexing (neverteless, good catch).

--
:wq
Mike Sanders

Re: Function Pointers...

<uqra82$k82b$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33392&group=comp.lang.c#33392

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm....@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sat, 17 Feb 2024 21:59:29 +0000
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <uqra82$k82b$1@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 17 Feb 2024 21:59:30 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bf9d8b7cebe715685ce3bde74d2a9173";
logging-data="663627"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+dLLjs2HmJbWGiU+MuQCvS+U38LU7uJlg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:x/CtKrtzwGWAXG1DExRzMKvhvhw=
Content-Language: en-GB
In-Reply-To: <uqr7ne$jkp9$1@dont-email.me>
 by: Malcolm McLean - Sat, 17 Feb 2024 21:59 UTC

On 17/02/2024 21:16, Mike Sanders wrote:
> For a good while I've been wanting to use function pointers
> & I finally found an opportunity to do so...
>
> Currently I have:
>
> switch(ops.export) {
> case 1: outputTXT(p, nodes, c); break;
> case 2: outputCSV(p, nodes, c); break;
> case 3: outputSQL(p, nodes, c); break;
> case 4: outputHTM(p, nodes, c); break;
> }
>
> But instead I could use:
>
> void (*output[])(FILE*, BLOCK[], int) =
> { outputTXT, outputCSV, outputSQL, outputHTM };
>
> // do stuff
>
> output[ops.export -1](p, nodes, c);
>
> Now here's where I'm unsure: Perhaps the overhead of an indirect call
> is miniscule, even negligible. But there *IS* additional overhead when
> using function pointers correct?
>

Compilers will often inline a direct call and perform aggressive
holistic optimsation of the resulting subroutine whilst they won't do so
with an indirect call, even if the value is passed as a constant. But of
course it depends on the compiler. Whether a direct call is in itself
faster than an indirect call or not depends on the architecture. Many
architectures don't really support direct calls and they are faked up by
loading a constant into a register, and so there is no difference in
execution speed. But sometimes the indirect call is a separate
instruction and so there can be a minuscule overhead.

--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: Function Pointers...

<uqra8c$k3pf$4@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33393&group=comp.lang.c#33393

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.samoylyk.net!nyheter.lysator.liu.se!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo...@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sat, 17 Feb 2024 21:59:40 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 8
Message-ID: <uqra8c$k3pf$4@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 17 Feb 2024 21:59:40 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6ecbb5fd13673e938e0c0f21615ca6a8";
logging-data="659247"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18quKim7tlodoga9bDKgSSl"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:hHxGba4OF91YAtEutTuOqikBTUU=
 by: Lawrence D'Oliv - Sat, 17 Feb 2024 21:59 UTC

On Sat, 17 Feb 2024 21:16:30 -0000 (UTC), Mike Sanders wrote:

> Now here's where I'm unsure: Perhaps the overhead of an indirect call is
> miniscule, even negligible. But there *IS* additional overhead when
> using function pointers correct?

“Premature optimization is the root of all evil.”
-- variously attributed to Tony Hoare or Donald Knuth

Re: Function Pointers...

<uqranm$kau6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33394&group=comp.lang.c#33394

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: porkc...@invalid.foo (Mike Sanders)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sat, 17 Feb 2024 22:07:50 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 16
Sender: Mike Sanders <busybox@sdf.org>
Message-ID: <uqranm$kau6$1@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me> <uqr8kf$jnoa$1@dont-email.me> <uqra17$k1e9$2@dont-email.me>
Injection-Date: Sat, 17 Feb 2024 22:07:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="78e07104eb5e89122762fe3a1aa0a15f";
logging-data="666566"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18HfpXIx5tDJH+QgjgDmKDf"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
Cancel-Lock: sha1:utDpCEka3O/imTPVo7wRB10W+h4=
 by: Mike Sanders - Sat, 17 Feb 2024 22:07 UTC

Mike Sanders <porkchop@invalid.foo> wrote:

> ops.export is always 1 to 4, so:
>
> output[ops.export -1](p, nodes, c);
>
> keeps it within bounds of zero indexing (neverteless, good catch).

void (*output[])(FILE*, BLOCK[], int) =
{ outputTXT, outputCSV, outputSQL, outputHTM };
// 0 1 2 3

--
:wq
Mike Sanders

Re: Function Pointers...

<aoaAN.339049$Wp_8.263627@fx17.iad>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33395&group=comp.lang.c#33395

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx17.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Function Pointers...
Newsgroups: comp.lang.c
References: <uqr7ne$jkp9$1@dont-email.me> <Kt9AN.355935$7sbb.84907@fx16.iad> <uqr9k5$k1e9$1@dont-email.me>
Lines: 30
Message-ID: <aoaAN.339049$Wp_8.263627@fx17.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sat, 17 Feb 2024 22:26:14 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sat, 17 Feb 2024 22:26:14 GMT
X-Received-Bytes: 2060
 by: Scott Lurndal - Sat, 17 Feb 2024 22:26 UTC

porkchop@invalid.foo (Mike Sanders) writes:
>Scott Lurndal <scott@slp53.sl.home> wrote:
>
>> Not generally. In both cases, the function call machine instruction
>> needs the address of function. Loading a register with
>> the function pointer (if it is not already available,
>> for example as an offset from already loaded base register)
>> may or may not add to the instruction count; depending
>> on what relative branch size(s) the function call instruction
>> supports even a direct call may require the compiler to
>> load a function pointer into a register anyway.
>>
>> In your example, it depends on how the compiler implements
>> the switch statement - it could indeed just index into
>> a four element array containing the function pointers
>> when compiled with the appropriate optimization flags.
>
>Thanks Scott.
>
>I keep reading otherwise (comfused about it all really):

The only place where function pointers (which generalize
to functions in this context) have a performance impact
is when using indirection into a shared object via a procedure
linkage table. That overhead is independent upon weather
you use a function or a function pointer.

You example would only be affected by that performance
impact if the function being called were in a different
shared object or dynamically linked library.

Re: Function Pointers...

<uqrc3h$kdp3$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33396&group=comp.lang.c#33396

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.samoylyk.net!nyheter.lysator.liu.se!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sat, 17 Feb 2024 14:31:12 -0800
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <uqrc3h$kdp3$2@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me> <Kt9AN.355935$7sbb.84907@fx16.iad>
<uqr9k5$k1e9$1@dont-email.me> <aoaAN.339049$Wp_8.263627@fx17.iad>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 17 Feb 2024 22:31:13 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e1558c86d6e9f7b89dd43e324f783dc6";
logging-data="669475"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/HhuQQ39taM9NQ+Df4h7GTxb/N9N8UNQ4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:KIC7e8T/gAXd348JriJmPtk7TCY=
In-Reply-To: <aoaAN.339049$Wp_8.263627@fx17.iad>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 17 Feb 2024 22:31 UTC

On 2/17/2024 2:26 PM, Scott Lurndal wrote:
> porkchop@invalid.foo (Mike Sanders) writes:
>> Scott Lurndal <scott@slp53.sl.home> wrote:
>>
>>> Not generally. In both cases, the function call machine instruction
>>> needs the address of function. Loading a register with
>>> the function pointer (if it is not already available,
>>> for example as an offset from already loaded base register)
>>> may or may not add to the instruction count; depending
>>> on what relative branch size(s) the function call instruction
>>> supports even a direct call may require the compiler to
>>> load a function pointer into a register anyway.
>>>
>>> In your example, it depends on how the compiler implements
>>> the switch statement - it could indeed just index into
>>> a four element array containing the function pointers
>>> when compiled with the appropriate optimization flags.
>>
>> Thanks Scott.
>>
>> I keep reading otherwise (comfused about it all really):
>
> The only place where function pointers (which generalize
> to functions in this context) have a performance impact
> is when using indirection into a shared object via a procedure
> linkage table. That overhead is independent upon weather
> you use a function or a function pointer.
>
> You example would only be affected by that performance
> impact if the function being called were in a different
> shared object or dynamically linked library.

Basically, something like this?

https://groups.google.com/g/comp.lang.c/c/kpc3prZ5Sk4/m/EKpSC-YYBAAJ

https://pastebin.com/raw/f52a443b1
(raw text, no ads)

Re: Function Pointers...

<uqrftv$l94a$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33398&group=comp.lang.c#33398

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: porkc...@invalid.foo (Mike Sanders)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sat, 17 Feb 2024 23:36:31 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 20
Sender: Mike Sanders <busybox@sdf.org>
Message-ID: <uqrftv$l94a$1@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me> <uqra82$k82b$1@dont-email.me>
Injection-Date: Sat, 17 Feb 2024 23:36:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a85606bf83a3d1f0ac62c79ba9d06c51";
logging-data="697482"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18f/lOHSBO0I1p6Rw3FuaQS"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
Cancel-Lock: sha1:a4kAWPtqAuOcehdsDZLbPp2zNuY=
 by: Mike Sanders - Sat, 17 Feb 2024 23:36 UTC

Malcolm McLean <malcolm.arthur.mclean@gmail.com> wrote:

> Compilers will often inline a direct call and perform aggressive
> holistic optimsation of the resulting subroutine whilst they won't do so
> with an indirect call, even if the value is passed as a constant. But of
> course it depends on the compiler. Whether a direct call is in itself
> faster than an indirect call or not depends on the architecture. Many
> architectures don't really support direct calls and they are faked up by
> loading a constant into a register, and so there is no difference in
> execution speed. But sometimes the indirect call is a separate
> instruction and so there can be a minuscule overhead.

Thanks Malcom.

Just plain ol' gcc or clang here, & I see no difference in speed.

--
:wq
Mike Sanders

Re: Function Pointers...

<uqrg1c$l94a$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33399&group=comp.lang.c#33399

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.samoylyk.net!newsfeed.xs3.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: porkc...@invalid.foo (Mike Sanders)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sat, 17 Feb 2024 23:38:20 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 12
Sender: Mike Sanders <busybox@sdf.org>
Message-ID: <uqrg1c$l94a$2@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me> <uqra8c$k3pf$4@dont-email.me>
Injection-Date: Sat, 17 Feb 2024 23:38:20 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a85606bf83a3d1f0ac62c79ba9d06c51";
logging-data="697482"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18uGVAtbfAqHcX0ysbITxuR"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
Cancel-Lock: sha1:0IL1E5I2hQsw8BDBBU+WhJGeA34=
 by: Mike Sanders - Sat, 17 Feb 2024 23:38 UTC

Lawrence D'Oliveiro <ldo@nz.invalid> wrote:

> ???Premature optimization is the root of all evil.???
> -- variously attributed to Tony Hoare or Donald Knuth

Aye, that's what I want to avoid, cute/crafty code might
cause issues...

--
:wq
Mike Sanders

Re: Function Pointers...

<uqrg86$l94a$3@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33400&group=comp.lang.c#33400

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: porkc...@invalid.foo (Mike Sanders)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sat, 17 Feb 2024 23:41:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 21
Sender: Mike Sanders <busybox@sdf.org>
Message-ID: <uqrg86$l94a$3@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me> <Kt9AN.355935$7sbb.84907@fx16.iad> <uqr9k5$k1e9$1@dont-email.me> <aoaAN.339049$Wp_8.263627@fx17.iad>
Injection-Date: Sat, 17 Feb 2024 23:41:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a85606bf83a3d1f0ac62c79ba9d06c51";
logging-data="697482"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gpSfnSPcnrHhUx5J2KP1d"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
Cancel-Lock: sha1:yklCROnLsBM8kf51DTAlThRbxk8=
 by: Mike Sanders - Sat, 17 Feb 2024 23:41 UTC

Scott Lurndal <scott@slp53.sl.home> wrote:

> The only place where function pointers (which generalize
> to functions in this context) have a performance impact
> is when using indirection into a shared object via a procedure
> linkage table. That overhead is independent upon weather
> you use a function or a function pointer.
>
> You example would only be affected by that performance
> impact if the function being called were in a different
> shared object or dynamically linked library.

Well, a couple of things in favor of using the function pointer
(in this context), its only called once per program invocation
& its not called within a loop. Must study more knuckehead that
I am.

--
:wq
Mike Sanders

Re: Function Pointers...

<uqrh8k$lhl1$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33403&group=comp.lang.c#33403

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo...@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sat, 17 Feb 2024 23:59:16 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <uqrh8k$lhl1$2@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me> <uqra8c$k3pf$4@dont-email.me>
<uqrg1c$l94a$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 17 Feb 2024 23:59:16 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ef266a3a922f269062f854d615e239ef";
logging-data="706209"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ehD/qGP+o1T+QOtukfZXH"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:OOWcfW7Asp/P/pXqqSz4VBkm15Y=
 by: Lawrence D'Oliv - Sat, 17 Feb 2024 23:59 UTC

On Sat, 17 Feb 2024 23:38:20 -0000 (UTC), Mike Sanders wrote:

> Lawrence D'Oliveiro <ldo@nz.invalid> wrote:
>
>> “Premature optimization is the root of all evil.”
>> -- variously attributed to Tony Hoare or Donald Knuth
>
> Aye, that's what I want to avoid, cute/crafty code might cause issues...

Write it in the straightforward, obvious way, then.

Re: Function Pointers...

<86le7iy4wm.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33406&group=comp.lang.c#33406

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sat, 17 Feb 2024 16:47:37 -0800
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <86le7iy4wm.fsf@linuxsc.com>
References: <uqr7ne$jkp9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="442286b2a18a4930a6a4f298195e1b68";
logging-data="725885"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vvfRopUA0EkGLrIQ08V7t8APSRtWSipI="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:2AwChHPstMAU7AwqS4Ib5uv8upA=
sha1:JjNkSROisFNZlxzOaK0BYdZmljA=
 by: Tim Rentsch - Sun, 18 Feb 2024 00:47 UTC

porkchop@invalid.foo (Mike Sanders) writes:

> For a good while I've been wanting to use function pointers
> & I finally found an opportunity to do so...
>
> Currently I have:
>
> switch(ops.export) {
> case 1: outputTXT(p, nodes, c); break;
> case 2: outputCSV(p, nodes, c); break;
> case 3: outputSQL(p, nodes, c); break;
> case 4: outputHTM(p, nodes, c); break;
> }
>
> But instead I could use:
>
> void (*output[])(FILE*, BLOCK[], int) =
> { outputTXT, outputCSV, outputSQL, outputHTM };
>
> // do stuff
>
> output[ops.export -1](p, nodes, c);
>
> Now here's where I'm unsure: Perhaps the overhead of an indirect
> call is miniscule, even negligible. But there *IS* additional
> overhead when using function pointers correct?

Let me give the conclusion first and the explanation second.

Conclusion: if you think the array-of-function-pointers approach
looks better or improves program structure then by all means use
it.

Explanation: I got interested in this question some years ago,
in a similar scenario. Calling through a pointer-to-function can
and sometimes does incur a performance penalty compared to
calling a function directly. What I was interested in though is
a comparison like the one you are interested in: should I use
a switch() with direct calls, or indirect calls using an array of
function pointers. So I did some measurements. The results were
very nearly the same for the two different schemes. Choosing one
approach over the other was a wash, performance-wise. I didn't
do an exhaustive study or anything like that, but the results I
did get showed that, at least to first order, there is no speed
preference for either choice. So pick the one that gives a more
nicely structured program, and don't worry about which one might
be faster.

(Later on you might want to do some measurements and actually see
which approach runs faster, but the evidence suggests there is no
reason to worry about that now.)

Re: Function Pointers...

<uqsphf$1116c$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33430&group=comp.lang.c#33430

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: porkc...@invalid.foo (Mike Sanders)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sun, 18 Feb 2024 11:26:40 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 34
Sender: Mike Sanders <busybox@sdf.org>
Message-ID: <uqsphf$1116c$1@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me> <86le7iy4wm.fsf@linuxsc.com>
Injection-Date: Sun, 18 Feb 2024 11:26:40 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a85606bf83a3d1f0ac62c79ba9d06c51";
logging-data="1082572"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+SGMvQCeGFhpcK6OOva2YA"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (NetBSD/9.3 (amd64))
Cancel-Lock: sha1:iy1O9S4RlsvJ6SOvj3GWfuMHkiI=
 by: Mike Sanders - Sun, 18 Feb 2024 11:26 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:

> Let me give the conclusion first and the explanation second.
>
> Conclusion: if you think the array-of-function-pointers approach
> looks better or improves program structure then by all means use
> it.
>
> Explanation: I got interested in this question some years ago,
> in a similar scenario. Calling through a pointer-to-function can
> and sometimes does incur a performance penalty compared to
> calling a function directly. What I was interested in though is
> a comparison like the one you are interested in: should I use
> a switch() with direct calls, or indirect calls using an array of
> function pointers. So I did some measurements. The results were
> very nearly the same for the two different schemes. Choosing one
> approach over the other was a wash, performance-wise. I didn't
> do an exhaustive study or anything like that, but the results I
> did get showed that, at least to first order, there is no speed
> preference for either choice. So pick the one that gives a more
> nicely structured program, and don't worry about which one might
> be faster.
>
> (Later on you might want to do some measurements and actually see
> which approach runs faster, but the evidence suggests there is no
> reason to worry about that now.)

Excellent reply Tim, its right where I'm at. And my love/hate
relationship with C continues =)

--
:wq
Mike Sanders

Re: Function Pointers...

<uqsr1a$11b8g$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33432&group=comp.lang.c#33432

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: malcolm....@gmail.com (Malcolm McLean)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sun, 18 Feb 2024 11:52:10 +0000
Organization: A noiseless patient Spider
Lines: 37
Message-ID: <uqsr1a$11b8g$1@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me> <86le7iy4wm.fsf@linuxsc.com>
<uqsphf$1116c$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 18 Feb 2024 11:52:10 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e79e7edcf30ccfea16e8456c84726305";
logging-data="1092880"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18p1JWS5niD/iftoEM8G9GwCo8srzIIVeg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:aPHpl6KsolXH5MDVv4M0ZMTZaQM=
Content-Language: en-GB
In-Reply-To: <uqsphf$1116c$1@dont-email.me>
 by: Malcolm McLean - Sun, 18 Feb 2024 11:52 UTC

On 18/02/2024 11:26, Mike Sanders wrote:
> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>
>> Let me give the conclusion first and the explanation second.
>>
>> Conclusion: if you think the array-of-function-pointers approach
>> looks better or improves program structure then by all means use
>> it.
>>
>> Explanation: I got interested in this question some years ago,
>> in a similar scenario. Calling through a pointer-to-function can
>> and sometimes does incur a performance penalty compared to
>> calling a function directly. What I was interested in though is
>> a comparison like the one you are interested in: should I use
>> a switch() with direct calls, or indirect calls using an array of
>> function pointers. So I did some measurements. The results were
>> very nearly the same for the two different schemes. Choosing one
>> approach over the other was a wash, performance-wise. I didn't
>> do an exhaustive study or anything like that, but the results I
>> did get showed that, at least to first order, there is no speed
>> preference for either choice. So pick the one that gives a more
>> nicely structured program, and don't worry about which one might
>> be faster.
>>
>> (Later on you might want to do some measurements and actually see
>> which approach runs faster, but the evidence suggests there is no
>> reason to worry about that now.)
>
> Excellent reply Tim, its right where I'm at. And my love/hate
> relationship with C continues =)
>
If you are writing a bytecode interpeter often there is a big switch in
the innermost loop, and so it is very important to optimise it.
--
Check out Basic Algorithms and my other books:
https://www.lulu.com/spotlight/bgy1mm

Re: Function Pointers...

<uqsuvo$12s17$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33437&group=comp.lang.c#33437

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sun, 18 Feb 2024 12:59:37 +0000
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <uqsuvo$12s17$1@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me> <86le7iy4wm.fsf@linuxsc.com>
<uqsphf$1116c$1@dont-email.me> <uqsr1a$11b8g$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 18 Feb 2024 12:59:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="eba916f2667773bbc8db5752ef8843dc";
logging-data="1142823"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+evSLUV/1qKiSCyYvPqPOa1/8TbNljY7g="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:mxQbJe0nhsYh95H1c3J2iCZJpxc=
Content-Language: en-GB
In-Reply-To: <uqsr1a$11b8g$1@dont-email.me>
 by: bart - Sun, 18 Feb 2024 12:59 UTC

On 18/02/2024 11:52, Malcolm McLean wrote:
> On 18/02/2024 11:26, Mike Sanders wrote:
>> Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>>
>>> Let me give the conclusion first and the explanation second.
>>>
>>> Conclusion:  if you think the array-of-function-pointers approach
>>> looks better or improves program structure then by all means use
>>> it.
>>>
>>> Explanation:  I got interested in this question some years ago,
>>> in a similar scenario.  Calling through a pointer-to-function can
>>> and sometimes does incur a performance penalty compared to
>>> calling a function directly.  What I was interested in though is
>>> a comparison like the one you are interested in:  should I use
>>> a switch() with direct calls, or indirect calls using an array of
>>> function pointers.  So I did some measurements.  The results were
>>> very nearly the same for the two different schemes.  Choosing one
>>> approach over the other was a wash, performance-wise.  I didn't
>>> do an exhaustive study or anything like that, but the results I
>>> did get showed that, at least to first order, there is no speed
>>> preference for either choice.  So pick the one that gives a more
>>> nicely structured program, and don't worry about which one might
>>> be faster.
>>>
>>> (Later on you might want to do some measurements and actually see
>>> which approach runs faster, but the evidence suggests there is no
>>> reason to worry about that now.)
>>
>> Excellent reply Tim, its right where I'm at. And my love/hate
>> relationship with C continues =)
>>
> If you are writing a bytecode interpeter often there is a big switch in
> the innermost loop, and so it is very important to optimise it.

In my main interpreter, bytecode dispatch is usually done with a simple
loop invoking a function from a table of function pointers.

But there is also a now unused module where it is done with a big switch
(where each case just calls the corresponding function).

I dusted off that module, transpiled the program to C, compiled with gcc
-O3, and got these results on the first 3 programs I tried:

With two of them, using the switch-based method was 8% faster. With the
third, it was 20% faster.

It would probably be faster still if, instead of function calls, I
manually inlined some of the switch cases.

(However I don't normally bother with that dispatch method, since for
speed, I use another method (involving inline assembly and threaded
code) that can give me speedups of 2-3x even over gcc-O3 using switch,
and that's not even using C or optimisation.)

Re: Function Pointers...

<uqt1dh$14ebj$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33439&group=comp.lang.c#33439

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sun, 18 Feb 2024 14:41:04 +0100
Organization: A noiseless patient Spider
Lines: 75
Message-ID: <uqt1dh$14ebj$1@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 18 Feb 2024 13:41:05 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="fcb604342cd4fa95e9d1316645143691";
logging-data="1194355"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+1M8bngTiNEdKkCwM/3Qx3NDbRWBP3lUw="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:hw+gLv5DFjzIVxZ8SjkiHCCL1wM=
In-Reply-To: <uqr7ne$jkp9$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Sun, 18 Feb 2024 13:41 UTC

On 17/02/2024 22:16, Mike Sanders wrote:
> For a good while I've been wanting to use function pointers
> & I finally found an opportunity to do so...
>
> Currently I have:
>
> switch(ops.export) {
> case 1: outputTXT(p, nodes, c); break;
> case 2: outputCSV(p, nodes, c); break;
> case 3: outputSQL(p, nodes, c); break;
> case 4: outputHTM(p, nodes, c); break;
> }
>
> But instead I could use:
>
> void (*output[])(FILE*, BLOCK[], int) =
> { outputTXT, outputCSV, outputSQL, outputHTM };
>
> // do stuff
>
> output[ops.export -1](p, nodes, c);
>
> Now here's where I'm unsure: Perhaps the overhead of an indirect call
> is miniscule, even negligible. But there *IS* additional overhead when
> using function pointers correct?
>

First off, only concern yourself about the overhead if the overhead is
relevant - not all code has to be at top efficiency. But all code
benefits from being written in a clear and maintainable manner, with
minimal risk of errors. So keep that in mind when making such design
decisions.

I personally would not use function pointers here. You can have
different balances and opinions, and the rest of your code can influence
things, but I will list my justifications here.

1. You can get function pointers wrong. You can, of course, get all
kinds of things wrong - but when a function pointer goes wrong, it can
be extremely difficult to figure out what is happening and debug the code.

2. Switches have range-checking built in, array lookups here do not.

3. Switches can work directly with enumerated types which are almost
certainly a much better choice here for the type of "ops.export". Drop
the "magic numbers" and use names from an enumerated type. This makes
the switch clearer, and easier to maintain. It lets you use
"-Wswitch-enums" in gcc/clang to warn if the switch does not handle all
the cases.

4. Your ordering in the switch can be whatever suits your preferences
and for maintenance, not the numerical ordering needed for array
initialisation.

5. Now that you have an order free from numbers, there are no
complications or risks if you want to remove one of the actions.

6. The actions are run from within the switch, not via pointers. That
means the compiler can optimise far better, and - often more importantly
- can do more static analysis to find potential problems at compile
time. It also means you don't have to squeeze everything into one set
of parameters - different actions can have different parameters.

7. Tools that deal with call tracing work much better with the switch
version. That includes tools to check stack depth (rarely important on
PC's, but useful in small embedded systems), code coverage,
documentation, etc., can do a better job. If you are debugging and need
to work backwards to find how you got to a particular point in the
program, it is vastly easier if there are no function pointers in the way.

There are certainly situations where function pointers make code better
- in particular, using callbacks avoids dependencies between different
parts of the code. But for my own use, I rarely find them the best choice.

Re: Function Pointers...

<uqt47n$15v17$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=33440&group=comp.lang.c#33440

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: Function Pointers...
Date: Sun, 18 Feb 2024 14:29:12 +0000
Organization: A noiseless patient Spider
Lines: 87
Message-ID: <uqt47n$15v17$1@dont-email.me>
References: <uqr7ne$jkp9$1@dont-email.me> <uqt1dh$14ebj$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 18 Feb 2024 14:29:12 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="eba916f2667773bbc8db5752ef8843dc";
logging-data="1244199"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18oERvCXFTcRq5PJJALRhLLLoBgKN3Pr4c="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:LBYC9hOoUo1nmqxm8hTFrbJeZzg=
Content-Language: en-GB
In-Reply-To: <uqt1dh$14ebj$1@dont-email.me>
 by: bart - Sun, 18 Feb 2024 14:29 UTC

On 18/02/2024 13:41, David Brown wrote:
> On 17/02/2024 22:16, Mike Sanders wrote:
>> For a good while I've been wanting to use function pointers
>> & I finally found an opportunity to do so...
>>
>> Currently I have:
>>
>> switch(ops.export) {
>>      case 1: outputTXT(p, nodes, c); break;
>>      case 2: outputCSV(p, nodes, c); break;
>>      case 3: outputSQL(p, nodes, c); break;
>>      case 4: outputHTM(p, nodes, c); break;
>> }
>>
>> But instead I could use:
>>
>> void (*output[])(FILE*, BLOCK[], int) =
>>      { outputTXT, outputCSV, outputSQL, outputHTM };
>>
>> // do stuff
>>
>> output[ops.export -1](p, nodes, c);
>>
>> Now here's where I'm unsure: Perhaps the overhead of an indirect call
>> is miniscule, even negligible. But there *IS* additional overhead when
>> using function pointers correct?
>>
>
> First off, only concern yourself about the overhead if the overhead is
> relevant - not all code has to be at top efficiency.  But all code
> benefits from being written in a clear and maintainable manner, with
> minimal risk of errors.  So keep that in mind when making such design
> decisions.
>
>
> I personally would not use function pointers here.  You can have
> different balances and opinions, and the rest of your code can influence
> things, but I will list my justifications here.
>
> 1. You can get function pointers wrong.  You can, of course, get all
> kinds of things wrong - but when a function pointer goes wrong, it can
> be extremely difficult to figure out what is happening and debug the code.
>
> 2. Switches have range-checking built in, array lookups here do not.
>
> 3. Switches can work directly with enumerated types which are almost
> certainly a much better choice here for the type of "ops.export".  Drop
> the "magic numbers" and use names from an enumerated type.  This makes
> the switch clearer, and easier to maintain.  It lets you use
> "-Wswitch-enums" in gcc/clang to warn if the switch does not handle all
> the cases.
>
> 4. Your ordering in the switch can be whatever suits your preferences
> and for maintenance, not the numerical ordering needed for array
> initialisation.
>
> 5. Now that you have an order free from numbers, there are no
> complications or risks if you want to remove one of the actions.
>
> 6. The actions are run from within the switch, not via pointers.  That
> means the compiler can optimise far better, and - often more importantly
> - can do more static analysis to find potential problems at compile
> time.  It also means you don't have to squeeze everything into one set
> of parameters - different actions can have different parameters.
>
> 7. Tools that deal with call tracing work much better with the switch
> version.  That includes tools to check stack depth (rarely important on
> PC's, but useful in small embedded systems), code coverage,
> documentation, etc., can do a better job.  If you are debugging and need
> to work backwards to find how you got to a particular point in the
> program, it is vastly easier if there are no function pointers in the way.
>
> There are certainly situations where function pointers make code better
> - in particular, using callbacks avoids dependencies between different
> parts of the code.  But for my own use, I rarely find them the best choice.
>

In the example posted, there is a clear pattern where you have four
near-identical lines; only the function name changes. Making it more
table-driven makes sense.

But with only four cases, there is little to choose from between those
two options.

The actual performance of function pointers vs. switch is irrelevant
here. (I think the OP said this is only ever executed once, but even a
million times per second wouldn't matter.)

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor