Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / Microsoft / Scripting / Ast / IArgumentProvider.cs / 1305376 / IArgumentProvider.cs
/* **************************************************************************** * * Copyright (c) Microsoft Corporation. * * This source code is subject to terms and conditions of the Microsoft Public License. A * copy of the license can be found in the License.html file at the root of this distribution. If * you cannot locate the Microsoft Public License, please send an email to * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound * by the terms of the Microsoft Public License. * * You must not remove this notice, or any other, from this software. * * * ***************************************************************************/ using System; using System.Collections.Generic; using System.Text; namespace System.Linq.Expressions { ////// Provides an internal interface for accessing the arguments that multiple tree /// nodes (DynamicExpression, ElementInit, MethodCallExpression, InvocationExpression, NewExpression, /// and InexExpression). /// /// This enables two optimizations which reduce the size of the trees. The first is it enables /// the nodes to hold onto an IList of T instead of a ReadOnlyCollection. This saves the cost /// of allocating the ReadOnlyCollection for each node. The second is that it enables specialized /// subclasses to be created which hold onto a specific number of arguments. For example Block2, /// Block3, Block4. These nodes can therefore avoid allocating both a ReadOnlyCollection and an /// array for storing their elements saving 32 bytes per node. /// /// Meanwhile the nodes can continue to expose the original LINQ properties of ReadOnlyCollections. They /// do this by re-using 1 field for storing both the array or an element that would normally be stored /// in the array. /// /// For the array case the collection is typed to IList of T instead of ReadOnlyCollection of T. /// When the node is initially constructed it is an array. When the compiler accesses the members it /// uses this interface. If a user accesses the members the array is promoted to a ReadOnlyCollection. /// /// For the object case we store the 1st argument in a field typed to object and when the node is initially /// constructed this holds directly onto the Expression. When the compiler accesses the members /// it again uses this interface and the accessor for the 1st argument uses Expression.ReturnObject to /// return the object which handles the Expression or ReadOnlyCollection case. When the user accesses /// the ReadOnlyCollection then the object field is updated to hold directly onto the ReadOnlyCollection. /// /// It is important that the Expressions consistently return the same ReadOnlyCollection otherwise the /// re-writer will be broken and it would be a breaking change from LINQ v1. The problem is that currently /// users can rely on object identity to tell if the node has changed. Storing the readonly collection in /// an overloaded field enables us to both reduce memory usage as well as maintain compatibility and an /// easy to use external API. /// internal interface IArgumentProvider { Expression GetArgument(int index); int ArgumentCount { get; } } static class ArgumentProviderOps { internal static T[] Map(this IArgumentProvider collection, Func select) { int count = collection.ArgumentCount; T[] result = new T[count]; count = 0; for (int i = 0; i < count; i++) { result[i] = select(collection.GetArgument(i)); } return result; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. /* **************************************************************************** * * Copyright (c) Microsoft Corporation. * * This source code is subject to terms and conditions of the Microsoft Public License. A * copy of the license can be found in the License.html file at the root of this distribution. If * you cannot locate the Microsoft Public License, please send an email to * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound * by the terms of the Microsoft Public License. * * You must not remove this notice, or any other, from this software. * * * ***************************************************************************/ using System; using System.Collections.Generic; using System.Text; namespace System.Linq.Expressions { /// /// Provides an internal interface for accessing the arguments that multiple tree /// nodes (DynamicExpression, ElementInit, MethodCallExpression, InvocationExpression, NewExpression, /// and InexExpression). /// /// This enables two optimizations which reduce the size of the trees. The first is it enables /// the nodes to hold onto an IList of T instead of a ReadOnlyCollection. This saves the cost /// of allocating the ReadOnlyCollection for each node. The second is that it enables specialized /// subclasses to be created which hold onto a specific number of arguments. For example Block2, /// Block3, Block4. These nodes can therefore avoid allocating both a ReadOnlyCollection and an /// array for storing their elements saving 32 bytes per node. /// /// Meanwhile the nodes can continue to expose the original LINQ properties of ReadOnlyCollections. They /// do this by re-using 1 field for storing both the array or an element that would normally be stored /// in the array. /// /// For the array case the collection is typed to IList of T instead of ReadOnlyCollection of T. /// When the node is initially constructed it is an array. When the compiler accesses the members it /// uses this interface. If a user accesses the members the array is promoted to a ReadOnlyCollection. /// /// For the object case we store the 1st argument in a field typed to object and when the node is initially /// constructed this holds directly onto the Expression. When the compiler accesses the members /// it again uses this interface and the accessor for the 1st argument uses Expression.ReturnObject to /// return the object which handles the Expression or ReadOnlyCollection case. When the user accesses /// the ReadOnlyCollection then the object field is updated to hold directly onto the ReadOnlyCollection. /// /// It is important that the Expressions consistently return the same ReadOnlyCollection otherwise the /// re-writer will be broken and it would be a breaking change from LINQ v1. The problem is that currently /// users can rely on object identity to tell if the node has changed. Storing the readonly collection in /// an overloaded field enables us to both reduce memory usage as well as maintain compatibility and an /// easy to use external API. /// internal interface IArgumentProvider { Expression GetArgument(int index); int ArgumentCount { get; } } static class ArgumentProviderOps { internal static T[] Map(this IArgumentProvider collection, Func select) { int count = collection.ArgumentCount; T[] result = new T[count]; count = 0; for (int i = 0; i < count; i++) { result[i] = select(collection.GetArgument(i)); } return result; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WebBrowsableAttribute.cs
- MeasureData.cs
- MobileDeviceCapabilitiesSectionHandler.cs
- ConfigXmlWhitespace.cs
- ProcessMessagesAsyncResult.cs
- ParseHttpDate.cs
- FixedSOMElement.cs
- InteropEnvironment.cs
- DesignerCommandAdapter.cs
- StreamAsIStream.cs
- DefaultObjectSerializer.cs
- TimeSpan.cs
- UnauthorizedWebPart.cs
- PermissionRequestEvidence.cs
- LineVisual.cs
- GeneralTransform3D.cs
- Typography.cs
- MenuItemStyleCollection.cs
- DataGridViewRowHeightInfoNeededEventArgs.cs
- ImmComposition.cs
- ValidatorCollection.cs
- SerialPort.cs
- SequenceRange.cs
- FixedSOMLineCollection.cs
- DataSourceXmlSubItemAttribute.cs
- dataobject.cs
- OracleTimeSpan.cs
- StateRuntime.cs
- TextTreeInsertUndoUnit.cs
- WriteableBitmap.cs
- TypeDescriptionProvider.cs
- HwndTarget.cs
- SimpleBitVector32.cs
- EventDescriptor.cs
- XmlWriterTraceListener.cs
- AutomationElement.cs
- X509UI.cs
- WindowsStartMenu.cs
- SessionStateModule.cs
- Setter.cs
- UnionCodeGroup.cs
- HtmlInputSubmit.cs
- EntityObject.cs
- ParameterRefs.cs
- PropertyPanel.cs
- ZoneIdentityPermission.cs
- SdlChannelSink.cs
- DetailsView.cs
- ControlAdapter.cs
- _ProxyRegBlob.cs
- EnterpriseServicesHelper.cs
- PropertyChange.cs
- CngProvider.cs
- BuiltInPermissionSets.cs
- ControlCommandSet.cs
- QueryCacheEntry.cs
- Expressions.cs
- FunctionParameter.cs
- SequenceRange.cs
- HtmlButton.cs
- ObjectDisposedException.cs
- ColorContextHelper.cs
- HttpValueCollection.cs
- Automation.cs
- SQLGuid.cs
- StaticResourceExtension.cs
- EntityTemplateUserControl.cs
- SimpleTypeResolver.cs
- DoubleAnimationClockResource.cs
- HtmlPanelAdapter.cs
- BooleanAnimationUsingKeyFrames.cs
- RootBuilder.cs
- DocumentReference.cs
- DataGridRelationshipRow.cs
- MaterialGroup.cs
- EntityTypeEmitter.cs
- MarkerProperties.cs
- AuthenticationService.cs
- HtmlEncodedRawTextWriter.cs
- CallbackDebugElement.cs
- AssociationTypeEmitter.cs
- CodeArgumentReferenceExpression.cs
- SymbolEqualComparer.cs
- UInt16Storage.cs
- Soap12FormatExtensions.cs
- GeneralTransform3DGroup.cs
- UnsafeNativeMethods.cs
- NavigationWindow.cs
- Int32KeyFrameCollection.cs
- SqlError.cs
- ParseChildrenAsPropertiesAttribute.cs
- LinkUtilities.cs
- RepeatButtonAutomationPeer.cs
- MultiByteCodec.cs
- ConfigurationLocationCollection.cs
- HtmlCalendarAdapter.cs
- SQLInt32Storage.cs
- ConfigurationSectionCollection.cs
- VisualCollection.cs
- GifBitmapEncoder.cs