xaizek / hstr (License: Apachev2) (since 2018-12-07)
Bash and Zsh shell history suggest box - easily view, navigate, search and manage your command history.
Commit 68a4956839dc38a73303071a42eadb8e38d1c1c9

Dynamic allocation of the main radix sort slot based on the max key estimate.
Author: Martin Dvorak
Author date (UTC): 2014-01-31 07:40
Committer name: Martin Dvorak
Committer date (UTC): 2014-01-31 07:40
Parent(s): 5bda0921347ff156f186c8c284c25bd8e314a030
Signing key:
Tree: 9025e67521118121cc95473c5e9c5d364a8f2843
File Lines added Lines deleted
src/hstr_history.c 7 5
src/include/radixsort.h 4 1
src/radixsort.c 20 12
File src/hstr_history.c changed (mode: 100644) (index 922b4b0..e9b08c4)
... ... static const char *commandBlacklist[] = {
28 28 "ls ", "pwd ", "cd ", "cd .. ", "hh ", "mc " "ls ", "pwd ", "cd ", "cd .. ", "hh ", "mc "
29 29 }; };
30 30
31 #define DEBUG_RADIX
31 32 #ifdef DEBUG_RADIX #ifdef DEBUG_RADIX
32 #define DEBUG_RADIXSORT() radixsort_stat(&rs); exit(0)
33 #define DEBUG_RADIXSORT() radixsort_stat(&rs, false); exit(0)
33 34 #else #else
34 35 #define DEBUG_RADIXSORT() #define DEBUG_RADIXSORT()
35 36 #endif #endif
36 37
37 38 unsigned history_ranking_function(unsigned rank, int newOccurenceOrder, size_t length) { unsigned history_ranking_function(unsigned rank, int newOccurenceOrder, size_t length) {
38 // long metrics = rank+newOccurenceOrder/10+length;
39 39 long metrics=rank+(log(newOccurenceOrder)*10.0)+length; long metrics=rank+(log(newOccurenceOrder)*10.0)+length;
40 // alternative metrics:
41 // rank+newOccurenceOrder/10+length
40 42 assert(metrics<UINT_MAX); assert(metrics<UINT_MAX);
41 43 return metrics; return metrics;
42 44 } }
 
... ... HistoryItems *get_prioritized_history()
90 92 } }
91 93
92 94 RadixSorter rs; RadixSorter rs;
93 // TODO quick fix to enable loading of huge history files (2x800kB allocated) > malloc this
94 // based on the expected max value (inferred from history file size, # of rows, row size)
95 radixsort_init(&rs, 100000000);
95 unsigned radixMaxKeyEstimate=historyState->size*1000;
96 radixsort_init(&rs, (radixMaxKeyEstimate<100000?100000:radixMaxKeyEstimate));
97 rs.optFloorAndInsertBigKeys=true;
96 98
97 99 RankedHistoryItem *r; RankedHistoryItem *r;
98 100 RadixItem *radixItem; RadixItem *radixItem;
File src/include/radixsort.h changed (mode: 100644) (index 1b4b6bc..5b2b607)
... ... typedef struct {
37 37 unsigned keyLimit; unsigned keyLimit;
38 38 RadixItem ***topDigits; RadixItem ***topDigits;
39 39
40 bool optFloorAndInsertBigKeys;
41 bool optIgnoreBigKeys;
42
40 43 RadixSlot **_slotDescriptors; RadixSlot **_slotDescriptors;
41 44 unsigned _slotsCount; unsigned _slotsCount;
42 45 unsigned _topIndexLimit; unsigned _topIndexLimit;
 
... ... void radixsort_add(RadixSorter *rs, RadixItem *item);
47 50 RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data); RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data);
48 51 RadixItem **radixsort_dump(RadixSorter *rs); RadixItem **radixsort_dump(RadixSorter *rs);
49 52 void radixsort_destroy(RadixSorter *rs); void radixsort_destroy(RadixSorter *rs);
50 void radixsort_stat(RadixSorter *rs);
53 void radixsort_stat(RadixSorter *rs, bool listing);
51 54
52 55 #endif /* RADIXSORT_H_ */ #endif /* RADIXSORT_H_ */
File src/radixsort.c changed (mode: 100644) (index 895cee4..fb11ec8)
12 12 #define GET_TOP_INDEX(KEY) KEY/SLOT_SIZE #define GET_TOP_INDEX(KEY) KEY/SLOT_SIZE
13 13 #define GET_LOW_INDEX(KEY) KEY%SLOT_SIZE #define GET_LOW_INDEX(KEY) KEY%SLOT_SIZE
14 14
15 #define RADIX_DEBUG 0
16 #define RADIX_STRICT 0
15 #define RADIX_DEBUG 1
17 16
18 17 void radixsort_init(RadixSorter *rs, unsigned keyLimit) void radixsort_init(RadixSorter *rs, unsigned keyLimit)
19 18 { {
19 rs->optFloorAndInsertBigKeys=false;
20 rs->optIgnoreBigKeys=false;
21
20 22 rs->_topIndexLimit=GET_TOP_INDEX(keyLimit); rs->_topIndexLimit=GET_TOP_INDEX(keyLimit);
21 23 rs->size=0; rs->size=0;
22 24 rs->topDigits=malloc(rs->_topIndexLimit * sizeof(RadixItem ***)); rs->topDigits=malloc(rs->_topIndexLimit * sizeof(RadixItem ***));
 
... ... RadixItem **radixsort_get_slot(RadixSorter *rs, unsigned topIndex)
46 48 void radixsort_add(RadixSorter *rs, RadixItem *item) void radixsort_add(RadixSorter *rs, RadixItem *item)
47 49 { {
48 50 if(item->key > rs->keyLimit) { if(item->key > rs->keyLimit) {
49 if(RADIX_DEBUG) {
50 fprintf(stderr, "ERROR: Radix sort overflow - value to be inserted in radix is too big: %i (limit: %i)\n", item->key, rs->keyLimit);
51 }
52 if(RADIX_STRICT) {
53 exit(0);
51 if(RADIX_DEBUG) fprintf(stderr, "ERROR: Radix sort overflow - inserted key is bigger than limit (%i): %i\n", rs->keyLimit, item->key);
52 if(rs->optFloorAndInsertBigKeys) {
53 item->key = rs->keyLimit;
54 54 } else { } else {
55 return;
55 if(rs->optIgnoreBigKeys) {
56 return;
57 } else {
58 exit(0);
59 }
56 60 } }
57 61 } }
58 62
 
... ... void radix_dec_slot_descriptor_size(RadixSorter *rs, RadixSlot *descriptor, unsi
100 104 RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data) RadixItem *radix_cut(RadixSorter *rs, unsigned key, void *data)
101 105 { {
102 106 // TODO optimization: fix min/max on cut of a value // TODO optimization: fix min/max on cut of a value
103 if(key<=rs->maxKey) {
107 if(key <= rs->maxKey) {
104 108 unsigned topIndex = GET_TOP_INDEX(key); unsigned topIndex = GET_TOP_INDEX(key);
105 109 unsigned lowIndex = GET_LOW_INDEX(key); unsigned lowIndex = GET_LOW_INDEX(key);
106 110
 
... ... RadixItem **radixsort_dump(RadixSorter *rs)
165 169 return NULL; return NULL;
166 170 } }
167 171
168 void radixsort_stat(RadixSorter *rs)
172 void radixsort_stat(RadixSorter *rs, bool listing)
169 173 { {
170 printf("\n Radixsort (size/max/limit): %u %u %u", rs->size, rs->maxKey, rs->keyLimit);
171 if(rs->size>0) {
174 printf("\n Radixsort (size/max/limit/slot count): %u %u %u %u", rs->size, rs->maxKey, rs->keyLimit, rs->_slotsCount);
175 unsigned memory=rs->_topIndexLimit * sizeof(RadixItem ***);
176 memory+=memory;
177 memory+=rs->_slotsCount*(SLOT_SIZE * sizeof(RadixItem *));
178 printf("\n Memory: %u\n", memory);
179 if(listing && rs->size>0) {
172 180 int t = GET_TOP_INDEX(rs->maxKey); int t = GET_TOP_INDEX(rs->maxKey);
173 181 int slotMin, slotSize, slotCount, l; int slotMin, slotSize, slotCount, l;
174 182 unsigned items=0; unsigned items=0;
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/hstr

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/hstr

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master