Skip to content

Commit 87cc110

Browse files
committed
Cleanup MethodImplCache code
1 parent 3054967 commit 87cc110

File tree

1 file changed

+60
-120
lines changed

1 file changed

+60
-120
lines changed

Clojure/Clojure/Lib/MethodImplCache.cs

Lines changed: 60 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -8,119 +8,60 @@
88
* You must not remove this notice, or any other, from this software.
99
**/
1010

11-
/**
12-
* Author: David Miller
13-
**/
14-
1511
using System;
1612
using System.Collections;
1713

1814
namespace clojure.lang
1915
{
2016
// TODO: This is a cache for a type=>IFn map. Should be replaced by the DLR CallSite mechanism
21-
public sealed class MethodImplCache
22-
{
23-
#region nested class
24-
25-
public sealed class Entry
26-
{
27-
#region Data
28-
29-
readonly Type _t;
30-
31-
public Type T
32-
{
33-
get { return _t; }
34-
}
35-
36-
readonly IFn _fn;
37-
38-
public IFn Fn
39-
{
40-
get { return _fn; }
41-
}
42-
43-
#endregion
44-
45-
#region C-tors
17+
public sealed class MethodImplCache
18+
{
19+
public sealed class Entry
20+
{
21+
readonly Type _t;
22+
public Type T => _t;
4623

47-
public Entry(Type t, IFn fn)
48-
{
49-
_t = t;
50-
_fn = fn;
51-
}
24+
readonly IFn _fn;
25+
public IFn Fn => _fn;
5226

53-
#endregion
54-
}
27+
public Entry(Type t, IFn fn)
28+
{
29+
_t = t;
30+
_fn = fn;
31+
}
32+
}
5533

56-
#endregion
34+
#region Data
5735

58-
#region Data
36+
private readonly IPersistentMap _protocol;
37+
private readonly Keyword _methodk;
38+
private readonly Symbol _sym;
39+
public readonly int _shift;
40+
public readonly int _mask;
41+
private readonly object[] _table; //[class, entry. class, entry ...]
42+
public readonly IDictionary _map;
43+
Entry _mre;
5944

60-
private readonly IPersistentMap _protocol;
45+
// Accessors
6146

6247
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "ClojureJVM name match")]
63-
public IPersistentMap protocol
64-
{
65-
get { return _protocol; }
66-
}
67-
68-
private readonly Keyword _methodk;
48+
public IPersistentMap protocol => _protocol;
6949

7050
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "ClojureJVM name match")]
71-
public Keyword methodk
72-
{
73-
get { return _methodk; }
74-
}
51+
public Keyword methodk => _methodk;
7552

76-
private readonly Symbol _sym;
7753
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "ClojureJVM name match")]
78-
public Symbol sym
79-
{
80-
get { return _sym; }
81-
}
82-
83-
public readonly int _shift;
84-
public readonly int _mask;
85-
private readonly object[] _table; //[class, entry. class, entry ...]
86-
public readonly IDictionary _map;
54+
public Symbol sym => _sym;
8755

8856
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "ClojureJVM name match")]
89-
public IDictionary map
90-
{
91-
get { return _map; }
92-
}
93-
94-
Entry _mre;
57+
public IDictionary map => _map;
9558

9659
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "ClojureJVM name match")]
97-
public object[] table
98-
{
99-
get { return _table; }
100-
}
101-
102-
103-
// //these are not volatile by design
104-
// private object _lastType;
105-
106-
//// core_deftype.clj compatibility
107-
// public object lastClass
108-
// {
109-
// get { return _lastType; }
110-
// set { _lastType = value; }
111-
// }
112-
// private IFn _lastImpl;
60+
public object[] table => _table;
11361

114-
// // core_deftype.clj compatibility
115-
// public IFn lastImpl
116-
// {
117-
// get { return _lastImpl; }
118-
// set { _lastImpl = value; }
119-
//}
120-
121-
#endregion
62+
#endregion
12263

123-
#region C-tors
64+
#region C-tors
12465

12566
public MethodImplCache(Symbol sym, IPersistentMap protocol, Keyword methodk)
12667
: this(sym, protocol, methodk, 0, 0, RT.EmptyObjectArray)
@@ -138,7 +79,6 @@ public MethodImplCache(Symbol sym, IPersistentMap protocol, Keyword methodk, int
13879
_map = null;
13980
}
14081

141-
14282
public MethodImplCache(Symbol sym, IPersistentMap protocol, Keyword methodk, IDictionary map)
14383
{
14484
_sym = sym;
@@ -149,42 +89,42 @@ public MethodImplCache(Symbol sym, IPersistentMap protocol, Keyword methodk, IDi
14989
_table = null;
15090
_map = map;
15191
}
152-
#endregion
15392

93+
#endregion
15494

15595
#region Implementation
15696

15797
// initial lowercase for core.clj compatibility
15898
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "ClojureJVM name match")]
15999
public IFn fnFor(Type t)
160-
{
161-
Entry last = _mre;
162-
if (last != null && last.T == t)
163-
return last.Fn;
164-
return FindFnFor(t);
165-
}
166-
167-
IFn FindFnFor(Type t)
168-
{
169-
if (_map != null)
170-
{
171-
Entry e = (Entry)_map[t];
172-
_mre = e;
173-
return e?.Fn;
174-
}
175-
else
176-
{
177-
int idx = ((Util.hash(t) >> _shift) & _mask) << 1;
178-
if (idx < _table.Length && ((Type)_table[idx]) == t)
179-
{
180-
Entry e = ((Entry)table[idx + 1]);
181-
_mre = e;
182-
return e?.Fn;
183-
}
184-
return null;
185-
}
100+
{
101+
Entry last = _mre;
102+
if (last != null && last.T == t)
103+
return last.Fn;
104+
return FindFnFor(t);
186105
}
187106

188-
#endregion
189-
}
107+
IFn FindFnFor(Type t)
108+
{
109+
if (_map != null)
110+
{
111+
Entry e = (Entry)_map[t];
112+
_mre = e;
113+
return e?.Fn;
114+
}
115+
else
116+
{
117+
int idx = ((Util.hash(t) >> _shift) & _mask) << 1;
118+
if (idx < _table.Length && ((Type)_table[idx]) == t)
119+
{
120+
Entry e = ((Entry)table[idx + 1]);
121+
_mre = e;
122+
return e?.Fn;
123+
}
124+
return null;
125+
}
126+
}
127+
128+
#endregion
129+
}
190130
}

0 commit comments

Comments
 (0)