473,883 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mystery: static variables & performance

I've encountered a troublesome inconsistency in the C-language Perl
extension I've written for CPAN (Digest::SHA). The problem involves the
use of a static array within a performance-critical transform function.
When compiling under gcc on my big-endian PowerPC (Mac OS X),
declaring this array as "static" DECREASES the transform throughput by
around 5%. However, declaring it as "static" on gcc/Linux/Intel
INCREASES the throughput by almost 30%.

I would prefer that the array not be "static" so that the underlying C
function will be thread-safe. However, giving up close to 30%
performance on gcc/Linux/Intel is unacceptable for a digest routine,
whose value is often closely tied to speed.

Can anyone enlighten me on this mystery, and recommend a simple, clean,
portable way to assure good performance on all host types?

TIA, Mark

Nov 14 '05
115 7680
CBFalconer wrote:
Mark Shelor wrote:
So, if your true desire is to protect the users (as you allude to in
your opening paragraph), then you have the full means to achieve that.


PLONK Your parents methods of rearing their offspring are suspect.

With respect, CBFalconer, resorting to ad-hominem attacks does not
strengthen your position.

Nov 14 '05 #91
nrk wrote:
FWIW, Mark's original question was a marginal case IMO. For instance,
declaring his variable static inside a function has the effect that the
array is initialized exactly once.

Bravo, nrk! And I DO mean that sincerely. You've used your knowledge
of the C programming language to analyze a situation that's not specific
to any one compiler implementation. And, below, you've suggested ways
to ameliorate the issues mentioned in my original post.

Making it non-static would change this.
Also, he could remove the static and try getting the same effect by passing
in the array in question as a parameter to the function. This way he gets
to maintain state across calls.

Now, on to the details. First bear in mind that there's no necessity
for the array (hereafter referred to as "the message schedule") to have
the storage class "static". The complete contents of the array are
built from scratch each time the function is executed, so the
prior-initialization property of static variables is not relevant in
this case. Furthermore, there's no need to maintain the values of the
message schedule across repeated function calls. In short, the array
can be made into an automatic variable with absolutely no change in the
observed behavior of the program.

But, making this variable automatic has the effect of dramatically
decreasing the function's performance on some platforms, while
significantly increasing it on others. So, the ideal solution would be
to find a consistent way to represent the message schedule such that the
function's performance is at least near-optimal on all platforms. The
goal here is to devise a simple, clean, portable solution that works
consistently, thereby avoiding the maintenance problems associated with
creating a series of #ifdef's that sense each underlying platform and
tweak the message schedule accordingly.

He could also try hinting to the compiler that
certain variables inside the function will be accessed very frequently by
making their storage class "register" (IMHO, most modern compilers
completely ignore this and do a better job of deciding how to allocate
registers).

Yes, your IMHO is correct, at least in this case. The compiler does a
much better job of optimizing compared to a manual use of the register
storage class.

Mark simply refuses to accept that the generic *all*-platform-encompassing
answers to his question (barring algorithmic improvements) are borderline
useless and that he should perhaps restrict the scope to a handful of
architectures that dominate in today's world and optimize his
implementation for them.

You are correct that I do refuse to accept your conjecture, but I also
respect your and other's right to regard this conjecture as fact. Your
interests center primarily on the formal properties of C programs as
they relate to the standard. I regard this as highly important as well,
but in the final analysis have to place primary emphasis on de-facto
portability, clean design, and high performance.

Once he does that, he can then ask experts in
those relevant architectures as to what he can do to optimize his code.

Having a fairly high comfort level with processor architectures and
assembly language programming, I'm not too concerned about how to
optimize performance for specific targets. What I am concerned about,
however, is proceeding to this step before everything possible has been
done to make the clean, portable, C-only version as efficient as possible.

No further algorithmic improvements are obvious at this point. In fact,
my C-only version has already attained the theoretical maximum SHA-1
throughput of 1.7 cycles per bit when built with the Intel compiler on
Pentium platforms. My guess is that only the most deft uses of the
assembler will be successful in squeezing out additional cycles, and any
real advantage of doing so may turn out to be marginal and inconsistent.

Mark

Nov 14 '05 #92
Mark McIntyre wrote:
And to borrow another *threadplonk*

I must confess ... that's a relief.

Nov 14 '05 #93
nrk wrote:
.... snip ...
PS: Your book, section 4.2: The WAR style example of strcmp is
atrocious IMO. If you must insist on a single return, here's a
clearer version of strcmp:

int strcmp(const char *s1, const char *s2) {
while ( *s1 == *s2 && *s1 )
++s1, ++s2;

return *s1 - *s2;
}


if (sizeof int == 1) that code has an incipient bug.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #94
nrk <ra*********@de vnull.verizon.n et> wrote in message news:<Wi******* ************@nw rddc01.gnilink. net>...
R. Rajesh Jeba Anbiah wrote:
Mark Shelor <ms*****@comcas t.removeme.net> wrote in message
news:<rN******* *************@c omcast.com>...
<snip>
And, my *personal* view is that this group would be richer and more
broadly helpful if it oriented itself around the "computer language c"
instead of the "computer standard c". But these matters are ultimately
up to the newsgroup "in toto" to decide; they're not up to selected
groups of individuals to dictate, no matter how loud their voices.

From a practical viewpoint, however, it is far more effective in the
long-run for participants to simply ignore posts that they don't
consider topical. Then, if most or all participants feel the same way,
no responses will be issued to questions that are thereby de-facto
off-topic. This is the way to manifest a true consensus. Off-topic
posters will quickly realize the situation when they receive no
responses to their questions. And, they may even get the motivation at
that point to read the newsgroup FAQ if they can't understand the
silence.

In my own case, I received helpful responses to the query on static
variables and performance. And, these responses stand boldly in the
thread, so the "lurkers support me in email" disbelievers will have to
remain silent this time. Were my query truly off-topic, this wouldn't
have happened. Given the length of (and diversity of responses on) this
thread, it's obvious that there's no de-facto consensus on what's
strictly topical. Some people are simply louder and more aggressive
than others.
I'm the idiot who read c.l.c almost frequently for about 4-years.
If I recall correct, many of your comments are sometimes implied by
many people and most of them are gone/plonked/few of them joined. The
interesting thing I have found here is many regulars are respecting
your views or at least listening to your views---which hardly happend
before.


Rajesh, it is unfortunate that inspite of spending around 4 years in
CLC, you haven't realized why the regulars are sticklers for topicality and
to questions related to the ANSIC C Standard. It is not because C is a toy
language. It is not because all (or even most) regulars here are old
people who don't want to dig any more. It simply has to do with the fact
that C is a very versatile and pervasive language. If one were to start
answering any and all questions merely on the basis that it involved C in
some way, the group will become well near useless. As they say, you can
either be a rather poor jack of all trades or be a good master of one.
This group is a good master at answering questions related to the C
language as it is described in the ANSI/ISO standards. By being so picky
about topicality, CLC actually does a favor to off-topic posters. By
forcing them to go to a domain that's richer with experts in their specific
issues it helps them get better quality help than they would get here.
Interestingly, in this thread I've found some good views about the
topicality of c.l.c. For a very longtime, I was thinking myself: K&R
says C is used to write OS/compilers/etc, but many people here why
flame if such questions came here. I was also thinking myself: is ISO
C is a toy language; sometimes ago someone posted a script for itoa()
claiming that it's a "pure ISO C" code; I used to wonder if there is
any compilers/OS written in ISO C; also I would think, this standard
is for the old people who don't like to dig more. And,... of course
yet I haven't got any valid answers.


The answers are right in front of your eyes. Don't wonder why you're not
able to see them as long as you have your eyes shut.


Respected Mr.Ram Kumar,

Thanks for your reply. But, it seems that you didn't understand
what I meant or vice-versa. As you know, we have already received
these explanations before, but this is not what I meant as the
valid/rational explanation. Perhaps I should rephrase my slugish
English with better one---which I don't know now. Anyway, thanks a lot
for your concerns.
<snip> PS: Your book, section 4.2: The WAR style example of strcmp is atrocious
IMO. If you must insist on a single return, here's a clearer version of
strcmp:

int strcmp(const char *s1, const char *s2) {
while ( *s1 == *s2 && *s1 )
++s1, ++s2;

return *s1 - *s2;
}


Thanks a lot for your interest in the quality of the book. As you
see, it has it's bug reporting corner; please don't take c.l.c as the
one. Thanks for your help; thanks for your understanding.

With lots & lots of wishes.

--
"I don't believe in the God who doesn't give me food, but shows me
heaven!" -- Swami Vivekanandha
If you live in USA, please support John Edwards.
http://guideme.itgo.com/atozofc/ - "A to Z of C" Project
Email: rrjanbiah-at-Y!com
Nov 14 '05 #95
R. Rajesh Jeba Anbiah wrote:
nrk <ra*********@de vnull.verizon.n et> wrote in message
news:<Wi******* ************@nw rddc01.gnilink. net>...
<snip>
PS: Your book, section 4.2: The WAR style example of strcmp is
atrocious
IMO. If you must insist on a single return, here's a clearer version of
strcmp:

int strcmp(const char *s1, const char *s2) {
while ( *s1 == *s2 && *s1 )
++s1, ++s2;

return *s1 - *s2;
}


Thanks a lot for your interest in the quality of the book. As you
see, it has it's bug reporting corner; please don't take c.l.c as the
one. Thanks for your help; thanks for your understanding.


Since he /did/ post his "clearer version" to comp.lang.c, you should at
least get some feedback as to what is wrong with his correction. Do you see
the flaw? If not, then how do can you do quality control on the bug reports
you receive?

comp.lang.c is good at this sort of thing.

(Hint: the problem I can see has nothing to do with the loop.)

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #96
Richard Heathfield wrote:

R. Rajesh Jeba Anbiah wrote:
nrk <ra*********@de vnull.verizon.n et> wrote in message
news:<Wi******* ************@nw rddc01.gnilink. net>...
<snip>
PS: Your book, section 4.2: The WAR style example of strcmp is
atrocious
IMO. If you must insist on a single return, here's a clearer version of
strcmp:

int strcmp(const char *s1, const char *s2) {
while ( *s1 == *s2 && *s1 )
++s1, ++s2;

return *s1 - *s2;
}


Thanks a lot for your interest in the quality of the book. As you
see, it has it's bug reporting corner; please don't take c.l.c as the
one. Thanks for your help; thanks for your understanding.


Since he /did/ post his "clearer version" to comp.lang.c, you should at
least get some feedback as to what is wrong with his correction. Do you see
the flaw? If not, then how do can you do quality control on the bug reports
you receive?

comp.lang.c is good at this sort of thing.

(Hint: the problem I can see has nothing to do with the loop.)


Udefined behavior from integer overflow ?

--
pete
Nov 14 '05 #97
"pete" <pf*****@mindsp ring.com> wrote in message
news:40******** ***@mindspring. com...
Richard Heathfield wrote:

R. Rajesh Jeba Anbiah wrote:
nrk <ra*********@de vnull.verizon.n et> wrote in message
news:<Wi******* ************@nw rddc01.gnilink. net>...
>

<snip>
> PS: Your book, section 4.2: The WAR style example of strcmp is
> atrocious
> IMO. If you must insist on a single return, here's a clearer version
> of strcmp:
>
> int strcmp(const char *s1, const char *s2) {
> while ( *s1 == *s2 && *s1 )
> ++s1, ++s2;
>
> return *s1 - *s2;
> }

Thanks a lot for your interest in the quality of the book. As you
see, it has it's bug reporting corner; please don't take c.l.c as the
one. Thanks for your help; thanks for your understanding.


Since he /did/ post his "clearer version" to comp.lang.c, you should at
least get some feedback as to what is wrong with his correction. Do you
see the flaw? If not, then how do can you do quality control on the bug
reports you receive?

comp.lang.c is good at this sort of thing.

(Hint: the problem I can see has nothing to do with the loop.)


Udefined behavior from integer overflow ?


That's one. But there is also the fact that it does not make comparisons
based on unsigned char values. [A requirement for the real strcmp().]

--
Peter
Nov 14 '05 #98
nrk
R. Rajesh Jeba Anbiah wrote:

<snip>
Respected Mr.Ram Kumar,

Please feel free to address me as just ram or nrk.
Thanks for your reply. But, it seems that you didn't understand
what I meant or vice-versa. As you know, we have already received
these explanations before, but this is not what I meant as the
valid/rational explanation. Perhaps I should rephrase my slugish
English with better one---which I don't know now. Anyway, thanks a lot
for your concerns.

Very well. I guess we have to just agree to disagree then.

<snip>
PS: Your book, section 4.2: The WAR style example of strcmp is
atrocious
IMO. If you must insist on a single return, here's a clearer version of
strcmp:

int strcmp(const char *s1, const char *s2) {
while ( *s1 == *s2 && *s1 )
++s1, ++s2;

return *s1 - *s2;
}
Thanks a lot for your interest in the quality of the book. As you
see, it has it's bug reporting corner; please don't take c.l.c as the
one. Thanks for your help; thanks for your understanding.


Apologies. I will send future reports through your web form. FWIW, this
seems to be a fine effort, although lacking a bit in polish (nothing that
can't be fixed by having enough eyes go through it).

-nrk.
With lots & lots of wishes.


--
Remove devnull for email
Nov 14 '05 #99
nrk
Richard Heathfield wrote:
R. Rajesh Jeba Anbiah wrote:
nrk <ra*********@de vnull.verizon.n et> wrote in message
news:<Wi******* ************@nw rddc01.gnilink. net>...
<snip>
PS: Your book, section 4.2: The WAR style example of strcmp is
atrocious
IMO. If you must insist on a single return, here's a clearer version of
strcmp:

int strcmp(const char *s1, const char *s2) {
while ( *s1 == *s2 && *s1 )
++s1, ++s2;

return *s1 - *s2;
}


Thanks a lot for your interest in the quality of the book. As you
see, it has it's bug reporting corner; please don't take c.l.c as the
one. Thanks for your help; thanks for your understanding.


Since he /did/ post his "clearer version" to comp.lang.c, you should at
least get some feedback as to what is wrong with his correction. Do you
see the flaw? If not, then how do can you do quality control on the bug
reports you receive?

comp.lang.c is good at this sort of thing.

(Hint: the problem I can see has nothing to do with the loop.)


Ok, here's my take on this:

a) sizeof(int) > 1 for hosted implementations .
http://www.google.com/groups?selm=bu...unnews.cern.ch
So, integer overflow not an issue, yes?

b) Peter's concern still remains. So, does changing the last line to:

return *(unsigned char *)s1 - *(unsigned char *)s2;

make it alright?

-nrk.

--
Remove devnull for email
Nov 14 '05 #100

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

109
4215
by: MSG | last post by:
Michel Bardiaux <michel.bardiaux@peaktime.be> wrote in message news:<G4idnfgZ0ZfCWbrdRVn2jQ@giganews.com>... > Mark Shelor wrote: > > > > > OK, Sidney, I am considering it. I can certainly understand the premise > > that a group might choose to entertain ONLY those questions that can be > > resolved purely by a reading or clarification of (drum roll please) The > > Standard. But how utterly boring, and what a waste of talent. It > >...
0
9932
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11109
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7957
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5979
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.