morfologik/morfologik-stemming

DictionaryLookup thread safe

Closed this issue · 1 comments

kosaa commented

Is there needed a lot of work for makes the DictionaryLookup thread safety?

I created mvp project and my lookup method takes 60% cpu because of synchronization is needed.

Or maybe my code isn't optimal and there is better way to get WordDatas list.

private List<WordData> lookup(String word) {
	List<WordData> list = new ArrayList<>();
	synchronized (lock) {
		for (WordData wordData : dictionaryLookup.lookup(word)) {
			list.add(wordData.clone());
		}
	}
	return list;
}

Where is the above snippet of code from?

DictionaryLookup isn't thread-safe but is lightweight. You should be creating a dictionary lookup for use per-thread (or even on-demand) and it should be cheap. The underlying FST (Dictionary) is thread-safe and can be loaded and shared (once published safely, if you're running in a non-locking environment).