An efficient memory stream implementation that sacrifices the ability to get access to the internal byte buffer in order to drastically improve performance.
Inheritance: Stream
        public override void Flush()
        {
            if (_isClosing && !_isClosed)
            {
                try
                {
                    //Before the memory stream is closed we have a full copy of the page
                    //we can now put it into the document dom 
                    Seek(0, SeekOrigin.Begin);

                    PageContext currentContext = PublicApi.Url.CurrentContext;

                    if (currentContext != null && !string.IsNullOrWhiteSpace(currentContext.UrlName))
                    {
                        using (MemoryBlockStream tmpStream = new MemoryBlockStream())
                        {
                            CopyTo(tmpStream);

                            tmpStream.Seek(0, SeekOrigin.Begin);

                            //Config.DomIndexProvider = DomIndexProviders.None; this will improve performance but requires csquery 1.3.5-beta and above
                            Config.DomRenderingOptions = DomRenderingOptions.RemoveComments;

                            CQ document = CQ.CreateDocument(tmpStream, HttpContext.Current.Response.ContentEncoding);

                            _renderingObserverPlugin.NotifyObservers(document);

                            using (StreamWriter sw = new StreamWriter(_sink, HttpContext.Current.Response.ContentEncoding,(int) Length + 1000))
                            {
                                document.Render(OutputFormatters.HtmlEncodingNone, sw);
                            }
                        }
                    }
                    else
                    {
                        CopyTo(_sink);
                    }

                }
                catch (Exception ex)
                {
                    new CSException(CSExceptionType.UnknownHttpError, "Rendering Helper Failed", ex).Log();
                }
            }
            else if (!_isClosing && !_isClosed)
            {
                base.Flush();
            }
        }
        public override void Flush()
        {
            if (_isClosing && !_isClosed)
            {
                try
                {
                    //Before the memory stream is closed we have a full copy of the page
                    //we can now put it into the document dom
                    Seek(0, SeekOrigin.Begin);

                    PageContext currentContext = PublicApi.Url.CurrentContext;

                    if (currentContext != null && !string.IsNullOrWhiteSpace(currentContext.UrlName))
                    {
                        using (MemoryBlockStream tmpStream = new MemoryBlockStream())
                        {
                            CopyTo(tmpStream);

                            tmpStream.Seek(0, SeekOrigin.Begin);

                            //Config.DomIndexProvider = DomIndexProviders.None; this will improve performance but requires csquery 1.3.5-beta and above
                            Config.DomRenderingOptions = DomRenderingOptions.RemoveComments;

                            CQ document = CQ.CreateDocument(tmpStream, HttpContext.Current.Response.ContentEncoding);

                            _renderingObserverPlugin.NotifyObservers(document);

                            using (StreamWriter sw = new StreamWriter(_sink, HttpContext.Current.Response.ContentEncoding, (int)Length + 1000))
                            {
                                document.Render(OutputFormatters.HtmlEncodingNone, sw);
                            }
                        }
                    }
                    else
                    {
                        CopyTo(_sink);
                    }
                }
                catch (Exception ex)
                {
                    new CSException(CSExceptionType.UnknownHttpError, "Rendering Helper Failed", ex).Log();
                }
            }
            else if (!_isClosing && !_isClosed)
            {
                base.Flush();
            }
        }