Bug 2051 - Use _stp_map_sortn(), via iteration-limited foreach
Summary: Use _stp_map_sortn(), via iteration-limited foreach
Status: RESOLVED FIXED
Alias: None
Product: systemtap
Classification: Unclassified
Component: translator (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: David Smith
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-12-13 21:16 UTC by Martin Hunt
Modified: 2006-11-06 19:20 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
Patch that adds foreach "limit N" support (2.28 KB, patch)
2006-11-03 19:03 UTC, David Smith
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Hunt 2005-12-13 21:16:27 UTC
See http://sourceware.org/bugzilla/show_bug.cgi?id=1379 comment 4

In a polling loop, we generally don't need or want to print out an entire sorted
array. sortn() just finds the top or bottom n elements. In a typical array of
1000 elements, sortn(10) runs in less than a tenth the time that sort() takes.
Plus, if the array is not cleared, sortn() will have to do little work and the
speed advantage will become even larger. 

Sorts are expensive and lock maps for a long time. So reducing the time the
sorts take will improve accuracy by reducing the number of drops caused by
attempted locks on maps failing.
Comment 1 Frank Ch. Eigler 2005-12-13 21:26:41 UTC
This naturally ties in to iteration-limited array iteration.  To express this in
the language, one way is to extend the grammar in a way reminiscent of SQL:

foreach ([i1,i2] in ray limit N) { 
}

where N is a numeric expression evaluated at the beginning, and doesn't need to
be a literal.  The sorting +/- directives would stay as/where they are with
current rules.

How does the runtime sortn compare with sort for N approaching the map size?
Comment 2 Martin Hunt 2005-12-13 21:38:17 UTC
Subject: Re:  Use _stp_map_sortn(), via
	iteration-limited foreach

On Tue, 2005-12-13 at 21:26 +0000, fche at redhat dot com wrote:
> How does the runtime sortn compare with sort for N approaching the map size?

For small map sizes, both sorts are close in speed. But sortn()
approaches n**2 as N approaches the size of the map. I'd say just limit
N to 30 for sortn(), otherwise go with sort().


Comment 3 wei kong 2005-12-14 02:22:47 UTC
Some time what I want to sort is not the value, but some key. 
 
How? can?  
Comment 4 David Smith 2006-11-03 19:03:37 UTC
Created attachment 1399 [details]
Patch that adds foreach "limit N" support

Here's a patch that adds "foreach (key in array limit N)" support.  The "limit"
expression is evaluated once.
Comment 5 Frank Ch. Eigler 2006-11-03 23:47:34 UTC
Nice work.  A few parser & execution test cases, man page updates,
searches for existing scripts that use the newly keywordized
identifier "limit", and it's done.

BTW there is no real need to post patches to bugzilla.  Please feel
free to commit straight to CVS or if you deem review required, then
post to the mailing list.
Comment 6 David Smith 2006-11-06 19:20:13 UTC
Slightly modified patch checked in.  Test cases added and man page updated.