在审查一些散列功能时,我发现 Clojure 内部有许多地方使用 PersistentHashMap.EMPTY 初始化的映射。其中许多映射可能只有少量条目,因此 PersistentArrayMap 可能更有效。
以下是候选者
`
src/jvm/clojure/lang/ARef.java
19:private volatile IPersistentMap watches = PersistentHashMap.EMPTY;
src/jvm/clojure/lang/Compiler.java
3009: IPersistentMap m = PersistentHashMap.EMPTY;
3819: KEYWORDS, PersistentHashMap.EMPTY,
3820: VARS, PersistentHashMap.EMPTY,
3964: IPersistentMap closes = PersistentHashMap.EMPTY;
3977: IPersistentMap keywords = PersistentHashMap.EMPTY;
3978: IPersistentMap vars = PersistentHashMap.EMPTY;
5121: ,CLEAR_SITES, PersistentHashMap.EMPTY
7259: KEYWORDS, PersistentHashMap.EMPTY,
7260: VARS, PersistentHashMap.EMPTY
7418: IPersistentMap opts = PersistentHashMap.EMPTY;
7475: IPersistentMap fmap = PersistentHashMap.EMPTY;
7522: KEYWORDS, PersistentHashMap.EMPTY,
7523: VARS, PersistentHashMap.EMPTY,
7912: ,CLEAR_SITES, PersistentHashMap.EMPTY
src/jvm/clojure/lang/LispReader.java
755: RT.map(GENSYM_ENV, PersistentHashMap.EMPTY));
src/jvm/clojure/lang/MultiFn.java
39: this.methodTable = PersistentHashMap.EMPTY;
41: this.preferTable = PersistentHashMap.EMPTY;
49: methodTable = methodCache = preferTable = PersistentHashMap.EMPTY;
src/jvm/clojure/lang/Var.java
48: final static Frame TOP = new Frame(PersistentHashMap.EMPTY, null);
175: setMeta(PersistentHashMap.EMPTY);
341: IPersistentMap ret = PersistentHashMap.EMPTY;
`
方法:两种可能的方法 - 初始化为 PersistentArrayMap.EMPTY 或调用 RT.map()。后者需要函数调用,因此速度稍微慢一些,但优点是集中在单个位置构建映射。