Re: [問卦] ptt是用什麼搜尋演算法

作者: grapherd (GrD)   2017-10-11 01:33:04
TL;DR (太長,講結論):
文章標題使用: DBCS_strcasestr
時間複雜度: O(n ^ 2)
※ 引述《shiburin (>\\\<)》之銘言:
: 肥宅我發現
: 如果把搜尋的字串加長
: 搜尋時間就會明顯變長
: 所以ptt的搜尋是用什麼演算法呢
: 該不會是linear search吧
: 跑超久的
: 有沒有卦?
ptt source code: https://github.com/ptt/pttbbs
追下去
|-> 有關文章的程式碼:
| https://github.com/ptt/pttbbs/blob/master/mbbsd/read.c
|
|-> 有關按鍵按下去的觸發 (Ctrl+X, r, y, ...etc):
|
| static int i_read_key(const onekey_t *rcmdlist, keeploc_t *locmem,
| int bid, int bottom_line, int pending_darws)
|
| https://github.com/ptt/pttbbs/blob/master/mbbsd/read.c#L874
|
|-> 有關 z (搜尋) 按下去的程式碼:
| ...
| case '/':
| case '?':
| mode = select_read(locmem, RS_KEYWORD);
| break;
|
| case 'S:
| ...
|
| https://github.com/ptt/pttbbs/blob/master/mbbsd/read.c#L979
|
|-> 先建立出下面那排問你要搜尋啥的輸入欄:
|
| static int
| ask_filter_predicate(filter_predicate_t *pred, int prev_modes, int sr_mode,
| const fileheader_t *fh, int *success)
| ...
| } else if (sr_mode & RS_KEYWORD) {
| if(!getdata(b_lines, 0,
| currmode & MODE_SELECT ? "增加條件 標題: ":"搜尋標題: ",
| keyword, TTLEN, DOECHO) || trim_blank(keyword))
| return READ_REDRAW;
|
| LOG_IF(LOG_CONF_KEYWORD, log_filef("keyword_search_log", LOG_CREAT,
| "%s:%s\n", currboard, keyword));
|
| https://github.com/ptt/pttbbs/blob/master/mbbsd/read.c#L593
|
|-> 然後再來搜尋:
|
| if (select_read_should_build(newdirect, bid, &resume_from, &count) &&
| (count = select_read_build(currdirect, newdirect, !first_select,
| force_full_build ? 0 : resume_from, count,
| match_filter_predicate, &predicate)) < 0) {
|
| https://github.com/ptt/pttbbs/blob/master/mbbsd/read.c#L855
|
|-> 如果沒有被 shortcut, 就會去執行後面的 select_read_build:
|
| static int
| select_read_build(const char *src_direct, const char *dst_direct,
| int src_direct_has_reference, time4_t resume_from, int count,
| int (*match)(const fileheader_t *fh, void *arg), void *arg)
|
| 這邊的 match 是個函式指標,用來搜尋用的。前面傳入的是 match_filter_predicate,
| 因此可以得知是使用這個函式來搜尋。
|
| https://github.com/ptt/pttbbs/blob/master/mbbsd/read.c#L691
|
|-> 執行 match (match_filter_predicate)
|
| if (!match(&fhs[i], arg))
| continue;
|
| https://github.com/ptt/pttbbs/blob/master/mbbsd/read.c#L731
|
|-> 在 pool 中比對
|
| ...
| else if (sr_mode & RS_KEYWORD)
| return DBCS_strcasestr(fh->title, keyword) != NULL;
| ...
|
| DBCS: Double-byte Character Sets 雙位元組字元集。
| 這東西應該是 ptt 內部的字串編碼方式。
| man strcasestr: strstr, strcasestr, strnstr
作者: hankdai (hank)   2017-10-11 01:34:00
推一個
作者: Yukari000 (十塊錢)   2017-10-11 01:34:00
作者: Qoo20811 (我沒有暱稱)   2017-10-11 01:34:00
恩 我也這麼覺得
作者: formatted (ゴミ丼 わがんりんにゃれ)   2017-10-11 01:34:00
大推
作者: BoBoooM (蹦蹦)   2017-10-11 01:35:00
看不懂啦幹
作者: thanksmr (dsajoid)   2017-10-11 01:35:00
嗯 跟我想得很接近了
作者: chien20145 (☺)   2017-10-11 01:35:00
專業
作者: zxasqw0246 (yoyo)   2017-10-11 01:36:00
嗯 跟我猜的一樣
作者: a3831038 (哭哭傑)   2017-10-11 01:37:00
搞不好沒在維護
作者: karta1897830 (冰嵐)   2017-10-11 01:37:00
跟我剛剛說的差不多
作者: starfishkira (死搭魚)   2017-10-11 01:37:00
嗯嗯 我要說的差不多就這樣
作者: wasijohn (咖咩哈咩哈)   2017-10-11 01:38:00
這是很早期的BBS現在看來當然有很多可以改的地方,不過你很用心分析給推
作者: z1976 (z1976)   2017-10-11 01:38:00
有trace有推
作者: BECHI (CHAT)   2017-10-11 01:40:00
快推 不然人家以為我們看不懂
作者: karta1897830 (冰嵐)   2017-10-11 01:42:00
作者: MiLuDaiBoom (臉粗脖子紅)   2017-10-11 01:44:00
跟我想的一樣呢
作者: jeffery95099 (哈哈肥宅哈哈)   2017-10-11 01:44:00
原來是這樣 我早就想到了
作者: tim9527 (是個肥宅)   2017-10-11 01:47:00
看懂給推
作者: GTO106 (J.Y)   2017-10-11 01:47:00
差不多就這樣
作者: jhjhs33504 ( )   2017-10-11 01:49:00
原來是主機威能...
作者: mhtvpz   2017-10-11 01:54:00
好強R
作者: gagalala (嘎啦)   2017-10-11 01:59:00
竟然是linear太神啦
作者: reexamor (gtc)   2017-10-11 01:59:00
文組看不懂給推 請問理組的大家都看得懂嗎?
作者: CobeBryant (老大-摳比布萊恩)   2017-10-11 02:00:00
結果真的是linear
作者: shenguan (申申不息)   2017-10-11 02:00:00
你演算法系?
作者: ap954212 (death is like the wings)   2017-10-11 02:03:00
嗯嗯跟我想的一樣
作者: sz (宅)   2017-10-11 02:07:00
才不是O(n^2)字串長度要看做有上限 算常數
作者: g5637128 (幫QQ)   2017-10-11 02:10:00
看不懂給推
作者: bomda (蹦大)   2017-10-11 02:14:00
嗯嗯完全看不懂
作者: andyillusion (空空)   2017-10-11 02:14:00
這證明style都是虛的
作者: balcony5566 (陽台五六)   2017-10-11 02:15:00
恩恩 跟我想得差不多
作者: zzzz8931 (肥宅)   2017-10-11 02:16:00
樓下開發 O(lgn) 版本
作者: imren1214 (大肚女孩)   2017-10-11 02:16:00
好猛的主機
作者: gump2015 (阿甘)   2017-10-11 02:21:00
資管看不懂 正常嗎
作者: mixzero (混0)   2017-10-11 02:21:00
現在看八卦版還要懂程式了?
作者: MADAOTW (MADAO)   2017-10-11 02:31:00
程式能跑就是王道…
作者: tkufc (阿東)   2017-10-11 02:45:00
跟我想得很接近
作者: pepro (peproisgood)   2017-10-11 02:56:00
要改善就把文章標題加上index吧 把她弄成儲存各字元所對應的數字 再儲存所在位置,然後照大小排序。比對的時候就直接比大小 可以使用binarySearch,有找到並且是連續就繼續搜尋這樣可以使比對字串時間變成logn
作者: tw2000 (打個冷顫)   2017-10-11 02:57:00
上底+下底X高÷2 啦
作者: wayneh1556 (啊文)   2017-10-11 03:17:00
文組怒噓
作者: cy4v (o'_'o)   2017-10-11 03:26:00
主機超神的
作者: cowardlyman (爽呆嚕~~>"<)   2017-10-11 03:41:00
恩恩@@
作者: positMIT (MarineQueen)   2017-10-11 03:48:00
你幹嘛呢

Links booklink

Contact Us: admin [ a t ] ucptt.com