Mass file changes

In our plugin I have to change a lot of files in "real-time" mode (see macros support topic). We implemented such behaviour, but faced some lags when editing code, for example. I tried to understand why it is happening and realized that there's something I don't understand in IDEA architecture.

Giving to documentation, there are two main methods for working with PSI (to keep it consistent) : runReadAction and runWriteAction. First one can be runned in any thread, and the second one only in UI thread (as far as i understand the second restriction is made to avoid deadlocks, because text and PSI can become inconsistent anyway, because there is commit document process). But there is no separate write thread, what leads to some not very good consequences. If I have some time consuming process, this process creates read lock, and that way blocks any event that needs write access, which in it's turn blocks UI thread (and it is called "lag" :)

For example when I need to mark macros usages to be reinlined, I have to use search usages and resolve mechanisms, and they use stub and file based indexes, which also have to be invalidated. And the last process can be rather heavy (for example if save all documents was triggered, and all files were marked as dirty). The only way I see this to be solved, is to prevent very often write changes (for example do not allow more than 2 documents to be modified in last 100 ms )

                        long current = System.currentTimeMillis();                         long lastTime = 0;                         if(!(changedDocs.size() < 2 || changedDocs.containsKey(document))) {                             Map.Entry<Document, Long> lastChanged = changedDocs.entrySet().iterator().next();                             lastTime = lastChanged.getValue();                             if(lastTime >= current - 200) {                                 ApplicationManager.getApplication().invokeLater(this);                                 return;                             }                             changedDocs.remove(lastChanged.getKey());                         }                         changedDocs.put(document, current);

So I wonder if there is more elegant solution in IDEA architecture for mass file changes.

And one more thing. As far as I understand, indexes are updated, only if they are added to myFilesToUpdate in FileBaseIndexImpl.ChangedFilesCollector, but it doesn't happen before, for example, saving document. But resolving works right after text\PSI changes (what means that StubIndex.get() is relevant), so maybe there is some "local incremental" mechanism (which seems to be quite fast), and I wonder why it is not used for saving.

Please sign in to leave a comment.