辞書アプリの改良:高速化(5)

対策4:メタインデックスである程度絞り込んだら、あとはメモリ上で

メタインデックス検索+弱い論理積+出現位置チェックで1000件未満まで絞り込めたら、あとは直接辞書インデックス取得し、メモリ上で絞り込んでみた。

変更点はこんな感じ。
Dictionary#searchの最後の部分を

if (metaIndexList != null && metaIndexList.empty) {
	break
} else if (metaIndexList != null && metaIndexList.size() < 1000) {
	def resultList = metaIndexList.collect {getIndex(it.offset, it.length)}
	resultList = resultList.findAll {it.word.indexOf(key) >= 0}
	stopTimer()
	return resultList.sort {l, r-> l.word <=> r.word}
}

としただけ。

また、弱い論理性、出現位置チェックの計測部分が不正確だったのでそこも修正。
Timer.groovy

class Timer {
	def name = ""
	def total = 0
	def time = null
	def Timer(n) {name = n}

	def start() {
		time = new Date()
	}
	def pause() {
		total += (new Date().getTime() - time.getTime())
		time = null
	}
	def stop() {
		if (time != null) total += (new Date().getTime() - time.getTime())
		println "TIME[${name}]:${total}" 
	}
}

結果

だいぶ高速化された。
だけど、どれも最初の検索片についてのメタインデックス検索+出現位置チェックをしているだけ。
1つの検索片でしかメタインデックス検索をしていないので、弱い論理積はしていない。
ちょっと切ない(´・ω・`)