🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.privacy.anon-server
Article #47279

Re: Exemplary ANSI C programming, an example

#47279
From: Thomas J. Boschloo
Date: Wed, 18 Dec 2002 10:26
174 lines
9089 bytes
DaveK wrote:
> 
> "Thomas J. Boschloo" <nospam@hccnet.nl.invalid> wrote in message
> news:3DFB07E9.5D961C57@hccnet.nl.invalid...
> > Since 99.8% of the world's professional programmers can't code, I
> > thought I would show them. Check out this word frequency counting
> > utility at <http://home.hccnet.nl/t.j.boschloo/wordcnt> and flame me
> > into the pits of hell.
> >
> 
>   Hi Thomas, hope you don't mind but I'll try and be constructive instead!

I *love* it! This almost never happens to me on usenet (people are much
more polite in e-mail to me, but then I get slack from the same people
in usenet sometimes, that's live). Now to business..

> ---snip---
> Some of the design principles I used were:
> 
>   a.. no memory allocation
> ---snip---
>   j.. no gulping up system resources in a multi-tasking environment so the
> program could be run from a script on a (usenet-) server
> ---snip---
> 
>   These two design principles don't really go together.  In your program (32
> bit version) you have a huge static array; that memory space needs to be
> reserved (if not committed) by the virtual memory system of whatever the
> program runs on each time the program starts up.  It would be less load and
> more mt-friendly to only allocate a small buffer to start with and only grow
> it if necessary.

I do realize this. In fact, I graduately increased the size of my 8
octed structure from 10000 elements to 200000! The netscape files I ran
my utility on (and the windows swapfile even moreso), would quickly fill
up my 7000 * 8 octed memory space at first. I stared it a 5000 words
because that seemed to be the amount of words in my Oxford Dictionary
(no, it was 50000 I think, well, doesn't matter anymore).

The reason I use a static array of 200000 elements, is that my
experience from running programs like Netscape 3 under Windows 3.1 with
8mb of memory has learned me that when things go wrong, they all go
wrong at the same time. At the time Netscape needs most memory, other
programs also seem to do so and the system just grinds to a halt.

The same holds for stack space that grows downward to the data segment
in traditional operating systems. At the time you nested deepest in your
recursive loops and stuff your data segment is also fullest (because
that recursion is nesting for some reason after all, and that reason is
data!). So my preferred choice would be to put the stack *beneat* the
data segment in memory and there would be no more buffer overflow
exploits! Just coredumps.

Now on static memory hurting system performance, I think it does not. If
a user (or script) uses only 10% of the array at the top of its load,
the rest can be safely swapped to disk as it will be never accessed by
the (my) program. But in addition to this, my process won't leave any
orphan memory (even though the comments say so, I didn't think at first
my code would be this fast without a B-Tree and stuff) for the simple
reason that it doesn't allocate any memory which might be orphaned! So I
can guaranty that there will be no memory leaks caused by my program!
Now that is sweet I think.

>   Also, your use of the hash value in your program is rather unconventional
> to say the least.

Thank you :-) It should fit inside a 32 bit 386+ register word if all is
ok. It should also be able to run on a 64 bit processor in the (near)
future as well as compile fine on a 16 bit 8086 processor :-))

> Hashes are most efficient when the hashvalue itself (or
> some derivative of it; often just the n least significant bits) is used to
> directly index the array or table being searched.  Doing a linear search,
> albeit with only a single comparison of a long (rather than the entire
> string)

It is better! When memory allows, I put the value I am looking for on
top of the array of hashes, so I do not need any bounds checking :-))
Richard Heathfields C++ code inspired me to do this (unwittingly I might
add).

    if(numwords<ARRAYSIZE) /* do we have one memory space left? */
    {
        arrayh[numwords]=hash; /* yes! no boundscheck is needed */
        i=0; while(arrayh[i]!=hash) i++; /* this is FAST!! */
        arrayh[numwords]=0; /* clear up any mess we made */
    }
    else /* no free memory left, do it the slow and secure way */
    {
        i=0; while((i<numwords) && (arrayh[i]!=hash)) i++;
    }

I did hope that the compiler would optimize this code in assembly by
putting the unsigned long 'hash' variable into a register outside of the
(fast) while loop. Then the assembly code could be like this:

	mov edx, hash
	mov ebx, -1
whileloop:
	inc ebx
	cmp arrayh[ebx], edx
	jnz whileloop

I would like to see any C++ library try to outperform that!!

Of course, in Turbo C 2.01 the code would be different because it
doesn't use the 386 registers :-( Something like this:

	mov si, hash
	mov di, hash+2
	mov bx, -1
whileloop:	
	inc bx
	cmp arrayh[bx], si
	jnz whileloop
	cmp arrayh[bx+2], di
	jnz whileloop

Which should be about as fast as you are suggesting below. I don't feel
the need to mess up the optimization provided by a good ANSI C compiler.
At least the hash should be moved outside of the while loop since the
value does never change. I expect every compiler to do this (except gcc
perhaps ;-). Maybe on a very modern compiler the code could be like:

	cmp long ptr arrayh[ebx], [ebp+4]

I wouldn't like that, but I think modern processors should be able to
manage or at least put the local long 'hash' in the L1 cache, which I
think should be about as fast as a register anyway.
	
> at each entry, loses almost all the efficiency you can gain from
> using a good hash.  If you're processing a million words, and there are ten
> thousand unique words in the set, looping through a large subset of those
> words each of a million times will be horribly slow.

I know, I did plan to write some clever memory management for this
wordcnt utility, but it seemed to run fast enough without it. What
really slows down this program is when I run out of memory, and it will
stay that way until the whole file is read. Other than that, it is
pretty fast I think :-) I ran it on my swapfile of 100mb yesterday
testing for words of 6 octets and above. It finished (while running out
of memory at new words arrayh[7200-1]) in ten minutes. Since this
program was written to run on a daily basis, I am pretty satisfied with
these results. Oh, my processor is an 150mmx! This program should really
overheat an overclocked P4 with Rambus RIMM memory ;-))) Try with huge
random files (like consecutive numbers). This program should be able to
profit greatly from high-bandwidth memory busses.

>   Here's a really good site I stumbled across a while ago when I was
> researching hashes myself, you might find it interesting.
> 
> http://burtleburtle.net/bob/hash/index.html
> 
>       DaveK

I am checking it out right now! (in addition to printing out the FAQ of
this group).. Print.. Print..

> --

> Proud Member of the Exclusive "I have been plonked by Davee because he
> thinks I'm interesting" List Member #<insert number here>

What's this? It looks fun :-) I am still getting a graphical artist to
design me an "I have been bitten by a Boschloo" button to put on their
homepages <grin>. I have made loads of victims on usenet
(<home.hccnet.nl/t.j.boschloo/netsafer> for my best kill).

Regards, I am enjoying the attention you are granting me ;-)
Thomas
-- 
Douglas Adams, 1978: "The Greek ferry timetables are the greatest work
of sublime fiction ever written this century".
T.J. Boschloo <home.hccnet.nl/t.j.boschloo>
Technical Details
Message-ID: <3E004D69.8704CC00@hccnet.nl.invalid>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-alt.fan.cult-dead-cow.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.online.be!bnewspeer01.bru.ops.eu.uu.net!emea.uu.net!news.hccnet.nl!not-for-mail
References: <3DFB07E9.5D961C57@hccnet.nl.invalid> <%0oL9.1742$zF4.69397@newsfep1-gui.server.ntli.net>

Show Full Headers
Complete Article Headers
Message-ID: <3E004D69.8704CC00@hccnet.nl.invalid>
Subject: Re: Exemplary ANSI C programming, an example
Date: Wed, 18 Dec 2002 11:26:49 +0100
From: "Thomas J. Boschloo" <nospam@hccnet.nl.invalid>
References: <3DFB07E9.5D961C57@hccnet.nl.invalid> <%0oL9.1742$zF4.69397@newsfep1-gui.server.ntli.net>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-alt.fan.cult-dead-cow.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.online.be!bnewspeer01.bru.ops.eu.uu.net!emea.uu.net!news.hccnet.nl!not-for-mail
Newsgroups: comp.lang.c,alt.fan.cult-dead-cow,alt.privacy.anon-server
Organization: Hobby Computer Club News Network
Lines: 175
NNTP-Posting-Host: uds41-35.dial.hccnet.nl
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Trace: news.hccnet.nl 1040207732 26439 62.251.35.41 (18 Dec 2002 10:35:32 GMT)
X-Complaints-To: abuse@hccnet.nl
NNTP-Posting-Date: Wed, 18 Dec 2002 10:35:32 +0000 (UTC)
Cancel-Lock: sha1:awDASHGJkPTGBeE2PDlmB++QvH8=
X-Mailer: Mozilla 4.8 [en] (Win98; U)
X-Accept-Language: en,nl,de
X-NFilter: 1.2.1-b1