ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/eio.c
(Generate patch)

Comparing libeio/eio.c (file contents):
Revision 1.40 by root, Sat Jun 13 14:58:33 2009 UTC vs.
Revision 1.41 by root, Sun Jun 14 20:36:59 2009 UTC

998 return b->score - a->score ? b->score - a->score /* works because our signed char is always 0..100 */ 998 return b->score - a->score ? b->score - a->score /* works because our signed char is always 0..100 */
999 : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0; 999 : a->inode < b->inode ? -1 : a->inode > b->inode ? 1 : 0;
1000} 1000}
1001 1001
1002#define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0 1002#define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0
1003#define EIO_DENT_NUM_SCORES 9
1003 1004
1004#define EIO_QSORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ 1005#define EIO_QSORT_CUTOFF 30 /* quite high, but performs well on many filesystems */
1005#define EIO_QSORT_SKIP 60 /* when to skip qsort completely */ 1006#define EIO_QSORT_SKIP 60 /* when to skip qsort completely */
1006 1007
1007static void 1008static void
1008eio_dent_sort (eio_dirent *dents, int size) 1009eio_dent_sort (eio_dirent *dents, int size)
1009{ 1010{
1010 int i, j;
1011
1012 if (size <= 1) 1011 if (size <= 1)
1013 return; /* our insertion sort relies on size > 0 */ 1012 return; /* our insertion sort relies on size > 0 */
1014 1013
1015 if (size > EIO_QSORT_SKIP) /* skip quicksort for small directories */ 1014 if (size > EIO_QSORT_SKIP) /* skip quicksort for small directories */
1016 { 1015 {
1017 /* first, use quicksort */ 1016 /* first, use quicksort */
1018 /* should be good for 2**31 entries */ 1017 /* should be good for 2**31 entries */
1018 int i;
1019 struct rng { int l, r; } rng [32]; 1019 struct rng { eio_dirent *l, *r; } rng [32];
1020 1020
1021 i = 0; 1021 i = 0;
1022 rng[0].l = 0; 1022 rng[0].l = dents;
1023 rng[0].r = size; 1023 rng[0].r = dents + size;
1024 1024
1025 while (expect_true (i >= 0)) 1025 while (expect_true (i >= 0))
1026 { 1026 {
1027 int L = rng [i].l; 1027 eio_dirent *L = rng [i].l;
1028 int R = rng [i].r - 1; 1028 eio_dirent *R = rng [i].r - 1;
1029 1029
1030 if (expect_false (L + EIO_QSORT_CUTOFF < R)) 1030 if (expect_false (L + EIO_QSORT_CUTOFF < R))
1031 { 1031 {
1032 eio_dirent piv = dents [L]; 1032 eio_dirent *mid = &dents [((L - dents) + (R - dents)) >> 1];
1033 eio_dirent piv = *mid; *mid = *L; *L = piv;
1033 1034
1034 while (L < R) 1035 while (L < R)
1035 { 1036 {
1036 while (EIO_DENT_CMP (dents [R], >=, piv) && L < R) 1037 while (EIO_DENT_CMP (*R, >=, piv) && L < R)
1037 --R; 1038 --R;
1038 1039
1039 if (L < R) 1040 if (L < R)
1040 dents [L++] = dents [R]; 1041 *L++ = *R;
1041 1042
1042 while (EIO_DENT_CMP (dents [L], <=, piv) && L < R) 1043 while (EIO_DENT_CMP (*L, <=, piv) && L < R)
1043 ++L; 1044 ++L;
1044 1045
1045 if (L < R) 1046 if (L < R)
1046 dents [R--] = dents [L]; 1047 *R-- = *L;
1047 } 1048 }
1048 1049
1049 dents [L] = piv; 1050 *L = piv;
1050 1051
1051 ++i; 1052 ++i;
1052 rng [i].l = L + 1; 1053 rng [i].l = L + 1;
1053 rng [i].r = rng [i - 1].r; 1054 rng [i].r = rng [i - 1].r;
1054 rng [i - 1].r = L; 1055 rng [i - 1].r = L;
1066 } 1067 }
1067 1068
1068 /* use an insertion sort after qsort, or for small arrays */ 1069 /* use an insertion sort after qsort, or for small arrays */
1069 /* first move the smallest element to the front, to act as a sentinel */ 1070 /* first move the smallest element to the front, to act as a sentinel */
1070 { 1071 {
1071 int min = 0; 1072 int i;
1072 1073 eio_dirent *min = dents;
1074
1073 for (i = size > EIO_QSORT_SKIP ? EIO_QSORT_CUTOFF + 1 : size; --i; ) 1075 for (i = size > EIO_QSORT_SKIP ? EIO_QSORT_CUTOFF + 1 : size; --i; )
1074 if (EIO_DENT_CMP (dents [i], <, dents [min])) 1076 if (EIO_DENT_CMP (dents [i], <, *min))
1075 min = i; 1077 min = &dents [i];
1076 1078
1077 /* swap elements 0 and j (minimum) */ 1079 /* swap elements 0 and j (minimum) */
1078 { 1080 {
1079 eio_dirent tmp = dents [0]; dents [0] = dents [min]; dents [min] = tmp; 1081 eio_dirent tmp = *dents; *dents = *min; *min = tmp;
1080 } 1082 }
1081 } 1083 }
1082 1084
1085 {
1083 /* then do standard insertion sort */ 1086 /* then do standard insertion sort */
1087 eio_dirent *i, *j;
1088
1084 for (i = 1; i < size; ++i) 1089 for (i = dents + 1; i < dents + size; ++i)
1085 { 1090 {
1086 eio_dirent value = dents [i]; 1091 eio_dirent value = *i;
1087 1092
1088 for (j = i - 1; EIO_DENT_CMP (dents [j], >, value); --j) 1093 for (j = i - 1; EIO_DENT_CMP (*j, >, value); --j)
1089 { 1094 j [1] = j [0];
1090 assert (j >= 0); 1095
1091 dents [j + 1] = dents [j]; 1096 j [1] = value;
1092 } 1097 }
1093
1094 dents [j + 1] = value;
1095 } 1098 }
1096} 1099}
1097 1100
1098/* read a full directory */ 1101/* read a full directory */
1099static void 1102static void
1100eio__scandir (eio_req *req, etp_worker *self) 1103eio__scandir (eio_req *req, etp_worker *self)
1157 /* now move dirs to the front, and non-dirs to the back */ 1160 /* now move dirs to the front, and non-dirs to the back */
1158 /* by walking from both sides and swapping if necessary */ 1161 /* by walking from both sides and swapping if necessary */
1159 while (ent > dir) 1162 while (ent > dir)
1160 { 1163 {
1161 if (dir->type == DT_DIR) 1164 if (dir->type == DT_DIR)
1165 {
1166 dir->score = 0;
1162 ++dir; 1167 ++dir;
1168 }
1163 else 1169 else
1164 { 1170 {
1165 --ent; 1171 --ent;
1166 1172
1167 if (ent->type == DT_DIR) 1173 if (ent->type == DT_DIR)
1168 { 1174 {
1169 eio_dirent tmp = *dir; 1175 eio_dirent tmp = *dir;
1170 *dir = *ent; 1176 *dir = *ent;
1171 *ent = tmp; 1177 *ent = tmp;
1172 1178
1179 dir->score = 0;
1173 ++dir; 1180 ++dir;
1174 } 1181 }
1175 } 1182 }
1176 } 1183 }
1177 1184
1230 1237
1231 ent = dents + dentoffs; 1238 ent = dents + dentoffs;
1232 1239
1233 ent->name = (char *)(size_t)namesoffs; /* rather dirtily we store the offset in the pointer */ 1240 ent->name = (char *)(size_t)namesoffs; /* rather dirtily we store the offset in the pointer */
1234 ent->namelen = len - 1; 1241 ent->namelen = len - 1;
1235 ent->inode = D_INO (entp); 1242 ent->inode = D_INO (entp) ? D_INO (entp) : dentoffs;
1236 1243
1237 switch (D_TYPE (entp)) 1244 switch (D_TYPE (entp))
1238 { 1245 {
1239 default: 1246 default:
1240 ent->type = EIO_DT_UNKNOWN; 1247 ent->type = EIO_DT_UNKNOWN;
1290 if (flags & EIO_READDIR_DIRS_FIRST) 1297 if (flags & EIO_READDIR_DIRS_FIRST)
1291 { 1298 {
1292 if (ent->type == EIO_DT_UNKNOWN) 1299 if (ent->type == EIO_DT_UNKNOWN)
1293 { 1300 {
1294 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ 1301 if (*name == '.') /* leading dots are likely directories, and, in any case, rare */
1295 ent->score = 98; 1302 ent->score = 7;
1296 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ 1303 else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */
1297 ent->score = len <= 2 ? len + 6 : len <= 4 ? 5 : len <= 7 ? 4 : 1; /* shorter == more likely dir, but avoid too many classes */ 1304 ent->score = len <= 2 ? len + 4 : len <= 4 ? 3 : len <= 7 ? 2 : 1; /* shorter == more likely dir, but avoid too many classes */
1298 } 1305 }
1299 else if (ent->type == EIO_DT_DIR) 1306 else if (ent->type == EIO_DT_DIR)
1300 ent->score = 100; 1307 ent->score = 8;
1301 } 1308 }
1302 } 1309 }
1303 1310
1304 namesoffs += len; 1311 namesoffs += len;
1305 ++dentoffs; 1312 ++dentoffs;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines