From e210323174242519976888844266376962c6d4bd Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 18 Feb 2013 19:41:28 +0400 Subject: [PATCH] VFS names stat --- .../ComputeVirtualFileNameStatAction.java | 229 ++++++++++++++++++ resources/src/idea/RichPlatformActions.xml | 2 +- 2 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 platform/lang-impl/src/com/intellij/internal/ComputeVirtualFileNameStatAction.java diff --git a/platform/lang-impl/src/com/intellij/internal/ComputeVirtualFileNameStatAction.java b/platform/lang-impl/src/com/intellij/internal/ComputeVirtualFileNameStatAction.java new file mode 100644 index 000000000000..2d8ce5ec93de --- /dev/null +++ b/platform/lang-impl/src/com/intellij/internal/ComputeVirtualFileNameStatAction.java @@ -0,0 +1,229 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * User: anna + * Date: 28-Jun-2007 + */ +package com.intellij.internal; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.project.DumbAware; +import com.intellij.openapi.util.Pair; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.newvfs.ManagingFS; +import com.intellij.openapi.vfs.newvfs.impl.VirtualFileSystemEntry; +import com.intellij.util.Function; +import gnu.trove.TObjectIntHashMap; +import gnu.trove.TObjectIntProcedure; + +import java.util.*; + +public class ComputeVirtualFileNameStatAction extends AnAction implements DumbAware { + public ComputeVirtualFileNameStatAction() { + super("Compute VF name statistics"); + } + + public static void main(String[] args) { + new ComputeVirtualFileNameStatAction().actionPerformed(null); + } + @Override + public void actionPerformed(AnActionEvent e) { + suffixes.clear(); + nameCount.clear(); + VirtualFile[] roots = ManagingFS.getInstance().getRoots(LocalFileSystem.getInstance()); + for (VirtualFile root : roots) { + compute(root); + } + + final List> names = new ArrayList>(nameCount.size()); + nameCount.forEachEntry(new TObjectIntProcedure() { + @Override + public boolean execute(String name, int count) { + names.add(Pair.create(name, count)); + return true; + } + }); + Collections.sort(names, new Comparator>() { + @Override + public int compare(Pair o1, Pair o2) { + return o2.second - o1.second; + } + }); + + System.out.println("Most frequent names ("+names.size()+" total):"); + int saveByIntern = 0; + for (Pair pair : names) { + int count = pair.second; + String name = pair.first; + System.out.println(name + " -> " + count); + saveByIntern += count * name.length(); + if (count == 1) break; + } + System.out.println("Total save if names were interned: "+saveByIntern+"; ------------"); + + //System.out.println("Prefixes: ("+prefixes.size()+" total)"); + //show(prefixes); + System.out.println("Suffix counts:("+suffixes.size()+" total)"); + show(suffixes); + + + final TObjectIntHashMap save = new TObjectIntHashMap(); + // compute economy + suffixes.forEachEntry(new TObjectIntProcedure() { + @Override + public boolean execute(String s, int count) { + save.put(s, count * s.length()); + return true; + } + }); + + System.out.println("Supposed save by stripping suffixes: ("+save.size()+" total)"); + final List> saveSorted = show(save); + + + final List picked = new ArrayList(); + //List candidates = new ArrayList(); + //int i =0; + //for (Pair pair : sorted) { + // if (i++>1000) break; + // candidates.add(pair.first); + //} + + //final TObjectIntHashMap counts = new TObjectIntHashMap(); + //suffixes.forEachEntry(new TObjectIntProcedure() { + // @Override + // public boolean execute(String a, int b) { + // counts.put(a, b); + // return true; + // } + //}); + + while (picked.size() != 15) { + Pair cp = saveSorted.get(0); + final String candidate = cp.first; + picked.add(candidate); + System.out.println("Candidate: '"+candidate+"', save = "+cp.second); + Collections.sort(picked, new Comparator() { + @Override + public int compare(String o1, String o2) { + return o2.length() - o1.length(); // longer first + } + }); + saveSorted.clear(); + + // adjust + suffixes.forEachEntry(new TObjectIntProcedure() { + @Override + public boolean execute(String s, int count) { + for (int i = picked.size() - 1; i >= 0; i--) { + String pick = picked.get(i); + if (pick.endsWith(s)) { + count -= suffixes.get(pick); + break; + } + } + saveSorted.add(Pair.create(s, s.length() * count)); + return true; + } + }); + Collections.sort(saveSorted, new Comparator>() { + @Override + public int compare(Pair o1, Pair o2) { + return o2.second.compareTo(o1.second); + } + }); + } + + System.out.println("Picked: "+ StringUtil.join(picked, new Function() { + @Override + public String fun(String s) { + return "\""+s+"\""; + } + }, ",")); + Collections.sort(picked, new Comparator() { + @Override + public int compare(String o1, String o2) { + return o2.length() - o1.length(); // longer first + } + }); + + int saved = 0; + for (int i = 0; i < picked.size(); i++) { + String s = picked.get(i); + int count = suffixes.get(s); + for (int k=0; k 100) { + System.out.println("\n.......<" + count + "...\n"); + break; + } + } + return prefs; + } + + //TObjectIntHashMap prefixes = new TObjectIntHashMap(); + TObjectIntHashMap suffixes = new TObjectIntHashMap(); + TObjectIntHashMap nameCount = new TObjectIntHashMap(); + private void compute(VirtualFile root) { + String name = root.getName(); + if (!nameCount.increment(name)) nameCount.put(name, 1); + for (int i=1; i<=name.length(); i++) { + //String prefix = name.substring(0, i); + //if (!prefixes.increment(prefix)) prefixes.put(prefix, 1); + + String suffix = name.substring(name.length()-i); + if (!suffixes.increment(suffix)) suffixes.put(suffix, 1); + } + Collection cachedChildren = ((VirtualFileSystemEntry)root).getCachedChildren(); + //VirtualFile[] cachedChildren = ((VirtualFileSystemEntry)root).getChildren(); + for (VirtualFile cachedChild : cachedChildren) { + compute(cachedChild); + } + } +} diff --git a/resources/src/idea/RichPlatformActions.xml b/resources/src/idea/RichPlatformActions.xml index 910d9db0ca6a..a94da99860a8 100644 --- a/resources/src/idea/RichPlatformActions.xml +++ b/resources/src/idea/RichPlatformActions.xml @@ -140,7 +140,7 @@ - +