Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 6026ec5

Browse files
committed
Combine is-operator-then-cast sequences.
Convert some cases of the form: if (value is T) ((T)value).DoSomething(); Etc. Into: T tValue = value as T; if (tValue != null) tValue.DoSomething(); Slightly smaller IL output, and removes double-cast in the case that uses it.
1 parent 1144415 commit 6026ec5

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

src/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/LightCompiler.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,17 +2229,19 @@ private ByRefUpdater CompileAddress(Expression node, int index)
22292229
_instructions.EmitStoreLocal(memberTemp.Value.Index);
22302230
}
22312231

2232-
if (member.Member is FieldInfo)
2232+
FieldInfo field = member.Member as FieldInfo;
2233+
if (field != null)
22332234
{
2234-
_instructions.EmitLoadField((FieldInfo)member.Member);
2235-
return new FieldByRefUpdater(memberTemp, (FieldInfo)member.Member, index);
2235+
_instructions.EmitLoadField(field);
2236+
return new FieldByRefUpdater(memberTemp, field, index);
22362237
}
2237-
else if (member.Member is PropertyInfo)
2238+
PropertyInfo property = member.Member as PropertyInfo;
2239+
if (property != null)
22382240
{
2239-
_instructions.EmitCall(((PropertyInfo)member.Member).GetGetMethod(true));
2240-
if (((PropertyInfo)member.Member).CanWrite)
2241+
_instructions.EmitCall(property.GetGetMethod(true));
2242+
if (property.CanWrite)
22412243
{
2242-
return new PropertyByRefUpdater(memberTemp, (PropertyInfo)member.Member, index);
2244+
return new PropertyByRefUpdater(memberTemp, property, index);
22432245
}
22442246
return null;
22452247
}

src/System.Linq.Queryable/src/System/Linq/Queryable.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@ public static IQueryable<TElement> AsQueryable<TElement>(this IEnumerable<TEleme
1414
{
1515
if (source == null)
1616
throw Error.ArgumentNull("source");
17-
if (source is IQueryable<TElement>)
18-
return (IQueryable<TElement>)source;
19-
return new EnumerableQuery<TElement>(source);
17+
return source as IQueryable<TElement> ?? new EnumerableQuery<TElement>(source);
2018
}
2119

2220
public static IQueryable AsQueryable(this IEnumerable source)
2321
{
2422
if (source == null)
2523
throw Error.ArgumentNull("source");
26-
if (source is IQueryable)
27-
return (IQueryable)source;
24+
IQueryable queryable = source as IQueryable;
25+
if (queryable != null) return queryable;
2826
Type enumType = TypeHelper.FindGenericType(typeof(IEnumerable<>), source.GetType());
2927
if (enumType == null)
3028
throw Error.ArgumentNotIEnumerableGeneric("source");

src/System.Linq/src/System/Linq/Enumerable.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> sour
1414
{
1515
if (source == null) throw Error.ArgumentNull("source");
1616
if (predicate == null) throw Error.ArgumentNull("predicate");
17-
if (source is Iterator<TSource>) return ((Iterator<TSource>)source).Where(predicate);
18-
if (source is TSource[]) return new WhereArrayIterator<TSource>((TSource[])source, predicate);
19-
if (source is List<TSource>) return new WhereListIterator<TSource>((List<TSource>)source, predicate);
17+
Iterator<TSource> iterator = source as Iterator<TSource>;
18+
if (iterator != null) return iterator.Where(predicate);
19+
TSource[] array = source as TSource[];
20+
if (array != null) return new WhereArrayIterator<TSource>(array, predicate);
21+
List<TSource> list = source as List<TSource>;
22+
if (list != null) return new WhereListIterator<TSource>(list, predicate);
2023
return new WhereEnumerableIterator<TSource>(source, predicate);
2124
}
2225

@@ -41,9 +44,12 @@ public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSo
4144
{
4245
if (source == null) throw Error.ArgumentNull("source");
4346
if (selector == null) throw Error.ArgumentNull("selector");
44-
if (source is Iterator<TSource>) return ((Iterator<TSource>)source).Select(selector);
45-
if (source is TSource[]) return new WhereSelectArrayIterator<TSource, TResult>((TSource[])source, null, selector);
46-
if (source is List<TSource>) return new WhereSelectListIterator<TSource, TResult>((List<TSource>)source, null, selector);
47+
Iterator<TSource> iterator = source as Iterator<TSource>;
48+
if (iterator != null) return iterator.Select(selector);
49+
TSource[] array = source as TSource[];
50+
if (array != null) return new WhereSelectArrayIterator<TSource, TResult>(array, null, selector);
51+
List<TSource> list = source as List<TSource>;
52+
if (list != null) return new WhereSelectListIterator<TSource, TResult>(list, null, selector);
4753
return new WhereSelectEnumerableIterator<TSource, TResult>(source, null, selector);
4854
}
4955

@@ -119,14 +125,14 @@ public IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
119125
//
120126
// This is a workaround implementation that does the "virtual dispatch" manually.
121127

122-
if (this is WhereEnumerableIterator<TSource>)
123-
return ((WhereEnumerableIterator<TSource>)this).SelectImpl<TResult>(selector);
128+
WhereEnumerableIterator<TSource> wei = this as WhereEnumerableIterator<TSource>;
129+
if (wei != null) return wei.SelectImpl<TResult>(selector);
124130

125-
if (this is WhereArrayIterator<TSource>)
126-
return ((WhereArrayIterator<TSource>)this).SelectImpl<TResult>(selector);
131+
WhereArrayIterator<TSource> wai = this as WhereArrayIterator<TSource>;
132+
if (wai != null) return wai.SelectImpl<TResult>(selector);
127133

128-
if (this is WhereListIterator<TSource>)
129-
return ((WhereListIterator<TSource>)this).SelectImpl<TResult>(selector);
134+
WhereListIterator<TSource> wli = this as WhereListIterator<TSource>;
135+
if (wli != null) return wli.SelectImpl<TResult>(selector);
130136

131137
// If we got here, then "this" is one of these types:
132138
//

0 commit comments

Comments
 (0)