🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #64585

Re: Newsreader programming - sorting one million lines ?

#64585
From: Curt Welch
Date: Sat, 29 Mar 2003 00:46
165 lines
11023 bytes
archiver@i3w.com (Juergen Helbing) wrote:
> So here finally my question:
> How do you sort your header-lists if they dont fit into memory ?
>
> I believe that sorting "1.500.000" lines simply by 'users-click'
> is a very demanding job - and such quantities appear only on
> the binary usenet today....

I've never had to solve that problem with my newsreaders, but I can throw
out a few things for you to think about.

With all interactive software, pure software speed isn't important.  Don't
get lost focusing on algorithms.  What's important is the responsivness of
the application.  This has as much to do with UI design as it does with
optimizing performance of algorithms.  Most good applications work not
because they simply "are" fast, but because they "seem" fast.  Most the
time, it's just a magic trick, and illusion.  Your problem is not that it
takes 60 seconds to sort 1.5M records, your problem is that the user is
being forced to wait 60 seconds doing nothing after they click a button
asking for the list to be resorted.  So, many times, the best soltion is
not to make the sort run in 10 seconds instead of 60, the solution is to
find a way that the user is not forced to set their waiting at all.  One
option for example, is to remove the button.  Don't laugh.  :) An
interactive application that takes 60 seconds to respond to a direct
request is unuseable.  It is almost always better to leave the feature out
of the application than to put one in which is unusable.

However, the far better approach than leaving a feature out is to find a
way to make it useable.

Finding a faster sort algortihm is one options which you have asked about
here.  The idea of "trust the vm system" is probably stupid.  nothing I've
seen about PCs tells me that you can put any trust into the vm system.
I've not done in real windows programming, but from my use of applications
on PCs, the worst thing you can do is trust the vm system.  that's because
to control responsifness of the applicsiton, you have to control what gets
swapped out, and when.  You never for example want UI code or data (like
the code for drawing buttons, or refreshing a window) to be swapped out.
YOu never want to user to have to wait 10 seconds for the vm to swap the IU
code back in before the application responds to you hitting an abort
button.  Applications that look to me like they took the "trust the vm"
option constantly have problems like that and they are, in my book,
unusable.  So don't trust the vm.  Make your appliction do the disk IO so
you can control when it happens and so that your disk IO "workload" never
gets in the way of UI code.

Second, sorting data that is stored on disk, and which must be written to
disk after the sort is easy.  You just do merge sort.  You read the data
sequential from disk, stoping when you have good sized chunk, then you sort
that chunk, and write it out to the disk.  Repeat until the entire data set
is sorted into N chunks.  Then, do a merge of X chunks at a time (where X
is N if possible and resonable), writing the outputfile as you merge.  When
correctly optimized, the time it takes to to this is limited by disk IO
speed, not CPU or sort speed.  The CPU is sorting/merging while the disks
are busy reading or writing data.  So the time is based on how many times
you have to read and write the data, and that's based on the memory size
based on the set size.  But, for your applacation, running on a smaller PC,
I doubt this will ever be fast enough to justify doing all that after they
hit the "sort by date" button.

So, you need too look for magic tricks.

The first and probably most useful trick is do the work at some other time
instead of waiting until they hit the "sort" button.  For a newsreader, the
data you are talking about is header data, and all header data has to be
downloaded.  Downloading headers always takes a lot of time and there's no
way around that, so an obvious thing to look at is doing all the sorting
work while it's being downloaded and you have time to waste as it is.  So
look at options of how to do as much work as you can while the data is
being downloaded.  And as always, you must keep the needs of the aplication
in mind, don't focus on "the sorting problem" and loose site of the big
picuture.

So, a newsreader needs to download headers, and store them in some type of
pre-sorted fashion on the disks.  But then, it needs to add more headers
the next time you download.  So, do you re-sort the entire batch (causing
you to re-read the entire set of data from the disk), or can you store it
some clever way to prevent you from having to do all that work every time
you download new headers?

For example, if you wanted to sort by author, and lets pretend that authors
names are created only from the letters a to z, then as you download the
headers, you can sort them into one of 26 buckets (a to z) based on the
first letter of their name.  You can then have 26 files (or logical files)
on the disk, so as you download, you collect the names in memory in these
buckets until a bucket gets enough data, then you write all the T's for
example out to the end of the T file, not bothering to sort the names
within the file.  So when you download new headers, you do part of the sort
work, but not all of it.  You just get all the Ts into the same file.

Then, when you want to disply the headers, you read and sort the A file to
get the first records.  Once you have done that, you can display all the
"A" authors even though you haven't sorted the rest of the files.  Depnding
on how the rest of the UI works, you may not even need to sort the rest
yet.  You might be able to put off the sort until they scroll down in the
list and try to look at other records.

So, this idea has issues, but the point of the example is trying to get you
to realize that there are a lot of tricks you can used based on the needs
of the application to do the work at different times so the user never has
to wait for the application to sort the entire 1.5M records.

There's also a psychological effect that happens when you spread the work
out over a long time instead of doing it all at once.  For example, an
applicaiton which sorts a little bit and causes small .5 second pauses
while downloading can seem much "faster", than one which waits until the
end of the download and "hangs" for 2 mintues doing what looks like
"nothing" while it does all the sort work after the download of new
headers.

Genreally, what you have said in your other posts I belive is the direction
you will have to go in.  You need to build indexes of some type which
represents the sorted data on the different fields you want to sort on
(date, author, size, whatever).  But look for creative ways to represt this
data so that it's both fast and easy to update while downloading, and
remeber that you can put of some of the work for later as long as what you
leave for later doesn't make it too slow when they actually hit a "sort by
date" button.

Also, when working with data on disk, and issues of sort times that are a
function not of CPU speed, but of disk speed and memory size vs data set
size, it's important to reduce the size of the data as much as possible.
For example, if you simply store 1.5M xover records in their normall wire
format, the time to sort that entire set by date will mostly be a function
not of the number of records you are sorting, but the total number of bytes
of data you have to sort.  1.5M xover records will be about 500 bytes per
record or 750 MBytes of data to sort.  If you want to sort by date, you can
for example pre-convert the date to a 32 bit unix time value, and store
that along with some pointer or reference to the real article.  If the
pointer is 32 bits, you then have 8 bytes per record instead of 500, then
the data set size gets reduced by a factor of 62 and the sort time is
reduced by more than that.  So what might have been a 60 second delay to
sort the entire data (unworkable) becomes a .5 second delay to sort the
date index alone (very workable).

And even if you find the need to sort the entire data set, if you can find
a way to store it that reduces the dataset size by a factor of 10, the sort
times are going to be reduced by more than a factor of 10.  And these gains
are far more important than the issue of "how to sort 1.5 m records when
they don't fit in memory".  And header data is very compactable because
there is lots of redundancy in the subjects, the authors, the groups, and
the references.

And back to the "magic" tricks, another good one to use is to make the user
think the job is done when it's not.  When they hit sort, you don't in fact
need to sort the entire set, you only need to figure out the part that's
going to be displayed in the current window.  If it's showing the first 50
articles in the set, then all you have to figure out is which 50 articles
out of the 1.5M are the ones which will show up in the window, and then
update that first.  Once the window is update, then the software can go
about doing the rest of the work while the user looks at that and thinks
about what button to hit next.  This can give you 5 seconds of "free" time
to work with.  This stuff however requies complex event driving
multi-threaded stuff that's real tricky to get right because if they hit
the "sort by author" button before your code is done sorting by date, you
need to abort the sort-by-date work, and start on the sort-by-author work.

So, maybe you didn't want to hear all this, and it's no simple answer for
how to huge amounts of article data quickly, but it may give you some
things to think about that will help you solve your problem....

-- 
Curt Welch                                            http://CurtWelch.Com/
curt@kcwc.com                          Webmaster for http://NewsReader.Com/
Technical Details
Message-ID: <20030328194656.482$xw@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!logbridge.uoregon.edu!news.maxwell.syr.edu!feed1.newsreader.com!newsreader.com!news2.newsreader.com!ellis.newsreader.com!not-for-mail
References: <4540d.o33203@archiver.winews.net>

Show Full Headers
Complete Article Headers
Message-ID: <20030328194656.482$xw@newsreader.com>
Subject: Re: Newsreader programming - sorting one million lines ?
Date: Sat, 29 Mar 2003 00:46:56 +0000
From: curt@kcwc.com (Curt Welch)
References: <4540d.o33203@archiver.winews.net>
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!logbridge.uoregon.edu!news.maxwell.syr.edu!feed1.newsreader.com!newsreader.com!news2.newsreader.com!ellis.newsreader.com!not-for-mail
Organization: NewsReader.Com
Newsgroups: news.software.nntp
X-Trace: NewsReader.Com dK=qq@V0zLokBcV2YnwZ6P-su@Lgwzuc+BVV@5g_mrrr+.NPU
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: 164