Article View: news.software.nntp
Article #62427Re: xBin (Power-Post 2003 - requesting input from authorities..)
From: Curt Welch
Date: Tue, 24 Dec 2002 01:36
Date: Tue, 24 Dec 2002 01:36
101 lines
6279 bytes
6279 bytes
Rene <invalid@email.addr> wrote:
> No that's wrong Curt. SHA-1, like the MD4 and MD5 algorithms with which I
> worked, don't care at all what the input size is. Actually, they measure
> it in bit, not byte. You can feed them 0 bit input data, you can feed
> them 1 bit input data or you can feed them 324463452344523bit input data.
> They simply don't care. They *do* an internal alignment but in the case
> of SHA-1, I just skimmed through the documentation
> (http://www.itl.nist.gov/fipspubs/fip180-1.htm) and SHA-1 works
> internally with a block size of 512bits or 64 bytes.
I could easilly be wrong about SHA-1. I don't think I've ever looked into
it. I just made the assumption it worked on 20 byte blocks in a similar
way to how MD5 works.
MD5 however is a block hash which works on 16 byte blocks. It is
structured to run though it's algorithm once for each 16 byte block. If
you give it 17 bytes to hash, it pads the last block and runs the hash
algortihm twice making it take twice as long as for 16 bytes.
If you selected a 17 byte "tree hash" vs 16 bytes, it would take close to
2X the CPU time to clacuate the hash. But that's a worse case issue which
is not at all significant when you are talking about blocks sizes 1K and
up.
If SHA-1 does use 64 byte blocks internally, then a 4500 byte hash block
would be mis-alligned for its blocks. But like we both said, that's not
going to make a notable difference in CPU usage.
> For the sake of speed using file i/o, I'd recommend a multiple of 1024 or
> 4096, whatever the OS' are best suited to process atomically.
> > 4500 seemed to be a good number for uuencode as well as a reasonable
> > balance between granularity and hash signature size.
>
> Well as I said, I'd make it a multiple of the OS' file io system.
Write the code, set the block size to 4096 and time it. Change the block
size to 4500 and see if you can measure _any_ difference. You won't be
able to.
Here are three runs of my code calculating the tree hash of a 50MB file
(where the file is all in the memory cache of the server so no disk IO
happened):
1.838u 0.739s 0:02.72 94.1% 21+1168k 0+0io 0pf+0w
1.865u 0.640s 0:02.58 96.8% 21+1151k 0+0io 0pf+0w
1.910u 0.577s 0:02.60 95.3% 21+1167k 0+0io 0pf+0w
And here are three runs of the same code with a 4096 byte buffer:
1.880u 0.592s 0:02.58 95.7% 21+1242k 0+0io 0pf+0w
1.877u 0.719s 0:02.72 94.8% 21+1248k 0+0io 1pf+0w
1.908u 0.599s 0:02.57 96.8% 21+1246k 0+0io 0pf+0w
Looking at the user time (u), the 4500 block code seemed to run faster than
the 4096 byte code. But more important, the difference is so small as to
be unimportant. It takes 2.5 seconds to calcuate the hash for an entire 50
MB file on this 1GHz machine no matter what the block alignment is. How
long will it take to just read that from the disk when the blocks are
pre-cached in memory like this example? How long will it take to post it?
How long will it take to download it?
From the above numbers, it can be estimated that the CPU time requried to
verify a 500KB article on a 1Ghz machine will be about 25 ms. How much
savings are you going to get when you are talking about 25 ms of "wasted
CPU" to start with?
The users will never ever notice a difference in performance if the block
size is 4500 vs 4096. If we pick a block size which is not a multiple of
the uuencode 45 bytes per line, this forces the last line in most uuencoded
articles to be short. If there is only one poorly written uudecode utility
in the world which is unable to correctly deal with that (for example it
falsely assums a short line means "end of file"), and one user has to spend
hours trying to undersand why the files he is downloading "don't work", how
much CPU time will you have to "save" for the other millions of users to
make up for the problems you caused that 1 user?
If you write code like this:
while (fread(hash_buf, 1, HASHBLOCKSIZE, file_stream)) {
...
}
Then every byte has to be copied from the stream input buffers to your
hash_buf, and the alignment has very little to do with how fast those
copies are. The main speed issues is how many total bytes you have to
move, and how many times you have to call fread(). The aligment just
doesn't make a measurable difference. fread() works with blocks which are
power-of-two sized to match the OS, so the overhead work of the OS is
already being isolated from the application and the work of re-aliging 4096
sream buffers to 4500 byte buffers is all dealt with by fread() so a
typical application programmer has nothing to deal with. The code is
identical no matter what the block size is.
I know a block size which is not a power of two seems "wrong". But I think
it's more than justified for this application.
--
Curt Welch http://CurtWelch.Com/
curt@kcwc.com Webmaster for http://NewsReader.Com/
Technical Details
Message-ID:
<20021223203608.351$Qp@newsreader.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!feed3.newsreader.com!newsreader.com!news2.newsreader.com!ellis.newsreader.com!not-for-mailReferences:
<cdda0v4h6t52ov7abf1j0qkpo843itf4se@4ax.com> <cfka0vsbjdlbdmtv2gg8qnehok033qh5f1@4ax.com> <20021223041127.986$6M_-_@newsreader.com> <1rfzsoaacf.fsf@vingodur.ifi.uio.no> <20021223180543.105$Wn@newsreader.com> <20021223185652.700$BS@newsreader.com>Show Full Headers
Complete Article Headers
Message-ID: <20021223203608.351$Qp@newsreader.com>
Subject: Re: xBin (Power-Post 2003 - requesting input from authorities..)
Date: Tue, 24 Dec 2002 01:36:08 +0000
From: curt@kcwc.com (Curt Welch)
References: <cdda0v4h6t52ov7abf1j0qkpo843itf4se@4ax.com> <cfka0vsbjdlbdmtv2gg8qnehok033qh5f1@4ax.com> <20021223041127.986$6M_-_@newsreader.com> <1rfzsoaacf.fsf@vingodur.ifi.uio.no> <20021223180543.105$Wn@newsreader.com> <20021223185652.700$BS@newsreader.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!feed3.newsreader.com!newsreader.com!news2.newsreader.com!ellis.newsreader.com!not-for-mail
Organization: NewsReader.Com
Newsgroups: news.software.nntp
X-Trace: NewsReader.Com IkYytyb5@FXJbKxRd,JrVxJi-QLV,u,uk0Y9Z9fM5nL0xWSO
X-Face: "p}G*1KH{+F7EYGKLb>ogDguabZ+%,?^epeFB!nzu`)`$=QcvL1KF6<0GH!Tbc!Sqo[|tV5
%IW48mQf3K=Ci&gZ7]]aazx@]Y-nq!r5{yH/#,?@lDdUDvOfByB2hVW0.@OM%{l/{cT'{w
X-Url: http://CurtWelch.Com/
User-Agent: NewsReader.Com
Lines: 100