Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query compilation performance #430

Merged
merged 3 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 138 additions & 15 deletions src/EntityFramework/Core/Query/InternalTrees/VarMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,37 @@

namespace System.Data.Entity.Core.Query.InternalTrees
{
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

// <summary>
// Helps map one variable to the next.
// </summary>
internal class VarMap : Dictionary<Var, Var>
internal class VarMap : IDictionary<Var, Var>
{
#region public surfaces

private Dictionary<Var, Var> map;
private Dictionary<Var, Var> reverseMap;

internal VarMap GetReverseMap()
{
var reverseMap = new VarMap();
foreach (var kv in this)
{
Var x;
// On the odd chance that a var is in the varMap more than once, the first one
// is going to be the one we want to use, because it might be the discriminator
// var;
if (!reverseMap.TryGetValue(kv.Value, out x))
{
reverseMap[kv.Value] = kv.Key;
}
}
return reverseMap;
return new VarMap(reverseMap, map);
}

public bool ContainsValue(Var value)
{
return reverseMap.ContainsKey(value);
}

public override string ToString()
{
var sb = new StringBuilder();
var separator = string.Empty;

foreach (var v in Keys)
foreach (var v in map.Keys)
{
sb.AppendFormat(CultureInfo.InvariantCulture, "{0}({1},{2})", separator, v.Id, this[v].Id);
separator = ",";
Expand All @@ -45,8 +42,134 @@ public override string ToString()

#endregion

#region IDictionary

public Var this[Var key]
{
get
{
return map[key];
}
set
{
map[key] = value;
}
}

public ICollection<Var> Keys
{
get
{
return map.Keys;
}
}

public ICollection<Var> Values
{
get
{
return map.Values;
}
}

public int Count
{
get
{
return map.Count;
}
}

public bool IsReadOnly
{
get
{
return false;
}
}

public void Add(Var key, Var value)
{
if (!reverseMap.ContainsKey(value))
{
reverseMap.Add(value, key);
}
map.Add(key, value);
}

public void Add(KeyValuePair<Var, Var> item)
{
if (!reverseMap.ContainsKey(item.Value))
{
((IDictionary<Var, Var>)reverseMap).Add(new KeyValuePair<Var, Var>(item.Value, item.Key));
}
((IDictionary<Var, Var>)map).Add(item);
}

public void Clear()
{
map.Clear();
reverseMap.Clear();
}

public bool Contains(KeyValuePair<Var, Var> item)
{
return ((IDictionary<Var, Var>)map).Contains(item);
}

public bool ContainsKey(Var key)
{
return map.ContainsKey(key);
}

public void CopyTo(KeyValuePair<Var, Var>[] array, int arrayIndex)
{
((IDictionary<Var, Var>)map).CopyTo(array, arrayIndex);
}

public IEnumerator<KeyValuePair<Var, Var>> GetEnumerator()
{
return map.GetEnumerator();
}

public bool Remove(Var key)
{
reverseMap.Remove(map[key]);
return map.Remove(key);
}

public bool Remove(KeyValuePair<Var, Var> item)
{
reverseMap.Remove(map[item.Value]);
return ((IDictionary<Var, Var>)map).Remove(item);
}

public bool TryGetValue(Var key, out Var value)
{
return ((IDictionary<Var, Var>)map).TryGetValue(key, out value);
}

IEnumerator IEnumerable.GetEnumerator()
{
return map.GetEnumerator();
}

#endregion

#region constructors

public VarMap()
{
map = new Dictionary<Var, Var>();
reverseMap = new Dictionary<Var, Var>();
}

private VarMap(Dictionary<Var, Var> map, Dictionary<Var, Var> reverseMap)
{
this.map = map;
this.reverseMap = reverseMap;
}

#endregion
}
}
Loading