﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Ayende Rahien commented on Stupid smart code</title><description>Nitin,
No, it doesn't do that</description><link>http://ayende.com/147457/stupid-smart-code#comment52</link><guid>http://ayende.com/147457/stupid-smart-code#comment52</guid><pubDate>Thu, 22 Dec 2011 11:09:30 GMT</pubDate></item><item><title>Nitin commented on Stupid smart code</title><description>Internally before counting the number of characters?</description><link>http://ayende.com/147457/stupid-smart-code#comment51</link><guid>http://ayende.com/147457/stupid-smart-code#comment51</guid><pubDate>Thu, 22 Dec 2011 10:47:53 GMT</pubDate></item><item><title>Ayende Rahien commented on Stupid smart code</title><description>Nitin,
GetBytesCount does NOT create an array</description><link>http://ayende.com/147457/stupid-smart-code#comment50</link><guid>http://ayende.com/147457/stupid-smart-code#comment50</guid><pubDate>Thu, 22 Dec 2011 10:33:11 GMT</pubDate></item><item><title>Nitin commented on Stupid smart code</title><description>var byteCount = Encoding.UTF8.GetByteCount(data);
GetByteCount creates one more array of characters?
</description><link>http://ayende.com/147457/stupid-smart-code#comment49</link><guid>http://ayende.com/147457/stupid-smart-code#comment49</guid><pubDate>Thu, 22 Dec 2011 10:30:59 GMT</pubDate></item><item><title>Torvin commented on Stupid smart code</title><description>req.GetRequestStream() returns an instance of internal ConnectStream class, whose Flush() method for some reason does exactly nothing...</description><link>http://ayende.com/147457/stupid-smart-code#comment47</link><guid>http://ayende.com/147457/stupid-smart-code#comment47</guid><pubDate>Wed, 21 Dec 2011 18:57:15 GMT</pubDate></item><item><title>Ayende Rahien commented on Stupid smart code</title><description>Tobi,
That is why I moved to stream writer there in the first place.
And the flushing merely ensures that the data goes out. Without it, you are reliant on the Dispose calling Flush, and there are good reasons not of call it, sometimes.
For example, you might call Dispose on the writer, which will dispose the stream, but you don't want that.
One example why you want that.</description><link>http://ayende.com/147457/stupid-smart-code#comment46</link><guid>http://ayende.com/147457/stupid-smart-code#comment46</guid><pubDate>Wed, 21 Dec 2011 13:11:28 GMT</pubDate></item><item><title>tobi commented on Stupid smart code</title><description>Ayende,
but data consistency is not one. Memory usage should be controlled by configuring the buffer size. You cannot release memory by flushing. The OS will flush to disk but still have the data cached. Network latency: true. Rarely needed and reduces throughput.</description><link>http://ayende.com/147457/stupid-smart-code#comment45</link><guid>http://ayende.com/147457/stupid-smart-code#comment45</guid><pubDate>Wed, 21 Dec 2011 13:07:53 GMT</pubDate></item><item><title>Ayende Rahien commented on Stupid smart code</title><description>Tobi,
No, it isn't. There are a lot of reasons to flush. Reducing memory usage, for example, ensuring lower latency in network scenarios, for another.
</description><link>http://ayende.com/147457/stupid-smart-code#comment44</link><guid>http://ayende.com/147457/stupid-smart-code#comment44</guid><pubDate>Wed, 21 Dec 2011 11:52:16 GMT</pubDate></item><item><title>tobi commented on Stupid smart code</title><description>It is almost always wrong to flush. The only reason I can come up with is to harden data on disk immediately for durability semantics. Flush = fsync. It is _always_ slower and does not change the data written at all.</description><link>http://ayende.com/147457/stupid-smart-code#comment43</link><guid>http://ayende.com/147457/stupid-smart-code#comment43</guid><pubDate>Wed, 21 Dec 2011 11:37:28 GMT</pubDate></item><item><title>meisinger commented on Stupid smart code</title><description>hmm... don't know; this seems a little like "select ain't broken" but...
clearly i'm not the smartest guy in the room but i would think that you would want to flush the internal state of the encoder

of course i have been bitten by this type of post in the past to where it could be that there was no check for the "data" parameter being null or empty</description><link>http://ayende.com/147457/stupid-smart-code#comment41</link><guid>http://ayende.com/147457/stupid-smart-code#comment41</guid><pubDate>Wed, 21 Dec 2011 07:50:42 GMT</pubDate></item><item><title>Ryan commented on Stupid smart code</title><description>My guess is that since you instantiate a new "bytes" var in every loop it actually ends up holding everything in memory twice anyways(at least until the GC kicks in).
</description><link>http://ayende.com/147457/stupid-smart-code#comment40</link><guid>http://ayende.com/147457/stupid-smart-code#comment40</guid><pubDate>Wed, 21 Dec 2011 00:56:15 GMT</pubDate></item><item><title>Ben commented on Stupid smart code</title><description>data.CopyTo(...)

copies the value and doesn't reference the original char?</description><link>http://ayende.com/147457/stupid-smart-code#comment39</link><guid>http://ayende.com/147457/stupid-smart-code#comment39</guid><pubDate>Tue, 20 Dec 2011 22:35:04 GMT</pubDate></item><item><title>Steve Py commented on Stupid smart code</title><description>Jarret + 0x1000!

Seeing as these are strings being passed back on a request I cannot see them being HUGE. (many MBs) I'd expect if anything was a memory drain it would be from the server serving many requests in which case the complex code would solve nothing. Temporarily allocating a few extra K for byte arrays to maintain readable code is certainly worth it.</description><link>http://ayende.com/147457/stupid-smart-code#comment38</link><guid>http://ayende.com/147457/stupid-smart-code#comment38</guid><pubDate>Tue, 20 Dec 2011 21:36:00 GMT</pubDate></item><item><title>Jay commented on Stupid smart code</title><description>Use Stream.Copy</description><link>http://ayende.com/147457/stupid-smart-code#comment37</link><guid>http://ayende.com/147457/stupid-smart-code#comment37</guid><pubDate>Tue, 20 Dec 2011 20:05:58 GMT</pubDate></item><item><title>dotnetchris commented on Stupid smart code</title><description>Tell us what you see Ayende</description><link>http://ayende.com/147457/stupid-smart-code#comment36</link><guid>http://ayende.com/147457/stupid-smart-code#comment36</guid><pubDate>Tue, 20 Dec 2011 20:00:42 GMT</pubDate></item><item><title>Karep commented on Stupid smart code</title><description>I'm not sure I understand.
In first solution you say memory is allocated twice. One for the string (parameter?) and for the buffer.

In second solution memory is allocated for string and then for buffer but multiple times.
var buffer...
var charBuffer... 
and in a while loop 
var bytes

So what you gain is that when data is very big you don't end up allocating a lot of memory on LOH. Instead you do a lot of small allocations if data is big.

Don't know what you mean by being stupid. I don't like the code before and after modification if data is very big. If it is not then why bother changing original code?
</description><link>http://ayende.com/147457/stupid-smart-code#comment35</link><guid>http://ayende.com/147457/stupid-smart-code#comment35</guid><pubDate>Tue, 20 Dec 2011 19:00:07 GMT</pubDate></item><item><title>Michael McDaniel commented on Stupid smart code</title><description>How about flushing inside the loop if you want the data streamed?  Let's call it lots of "courtesy flushes"</description><link>http://ayende.com/147457/stupid-smart-code#comment34</link><guid>http://ayende.com/147457/stupid-smart-code#comment34</guid><pubDate>Tue, 20 Dec 2011 18:33:50 GMT</pubDate></item><item><title>João Angelo commented on Stupid smart code</title><description>As other pointed out I'm on the side that the "I'm stupid" epiphany was due to realizing that you just reimplemented logic that is already implemented by StreamWriter.</description><link>http://ayende.com/147457/stupid-smart-code#comment33</link><guid>http://ayende.com/147457/stupid-smart-code#comment33</guid><pubDate>Tue, 20 Dec 2011 18:32:59 GMT</pubDate></item><item><title>James_2JS commented on Stupid smart code</title><description>I believe the problem is two-fold:

1) GetByteCount(string s) converts the string into char[s.Length] - so nothing has been gained there

2) worse still, GetByteCount has to encode the characters to produce its result, then the characters are encoded a 2nd time to write to the stream</description><link>http://ayende.com/147457/stupid-smart-code#comment32</link><guid>http://ayende.com/147457/stupid-smart-code#comment32</guid><pubDate>Tue, 20 Dec 2011 16:54:15 GMT</pubDate></item><item><title>James_2JS commented on Stupid smart code</title><description>@Frank Quednau - the first version is like it is due to the need to set ContentLength before writing to the stream</description><link>http://ayende.com/147457/stupid-smart-code#comment31</link><guid>http://ayende.com/147457/stupid-smart-code#comment31</guid><pubDate>Tue, 20 Dec 2011 16:51:25 GMT</pubDate></item><item><title>Bart Verkoeijen commented on Stupid smart code</title><description>What's the gain in performance compared to the extra complexity of the code?</description><link>http://ayende.com/147457/stupid-smart-code#comment30</link><guid>http://ayende.com/147457/stupid-smart-code#comment30</guid><pubDate>Tue, 20 Dec 2011 16:42:22 GMT</pubDate></item><item><title>Dennis commented on Stupid smart code</title><description>Jarrett indeed is the first one who mentions the first thing that popped into my mind when reading this: "why change something which intent initially was perfectly obvious into something that you need to read multiple times and is a lot harder to understand for a micro-optimization that may not even necessarily be an issue?" 

Doesn't seem like it's worth the extra complexity it introduces at a cost of read- and maintainability. </description><link>http://ayende.com/147457/stupid-smart-code#comment29</link><guid>http://ayende.com/147457/stupid-smart-code#comment29</guid><pubDate>Tue, 20 Dec 2011 15:54:10 GMT</pubDate></item><item><title>Frank Quednau commented on Stupid smart code</title><description>Confused by the first version in the first place. The simplest abstraction to write a string to a stream is a streamwriter, is it not?

using (var w = new StreamWriter(r.GetRequestStream(), Encoding.UTF8))
  sw.Write(data);</description><link>http://ayende.com/147457/stupid-smart-code#comment28</link><guid>http://ayende.com/147457/stupid-smart-code#comment28</guid><pubDate>Tue, 20 Dec 2011 15:35:11 GMT</pubDate></item><item><title>Mike commented on Stupid smart code</title><description>I'm guessing this code is stupid because you probably don't have any profiling metrics that show it actually needs optimization in the first place. So, rather than fixing true bottlenecks or implementing new features, you have needlessly invested a lot of time and introduced much more complexity.</description><link>http://ayende.com/147457/stupid-smart-code#comment27</link><guid>http://ayende.com/147457/stupid-smart-code#comment27</guid><pubDate>Tue, 20 Dec 2011 15:09:15 GMT</pubDate></item><item><title>Dennis commented on Stupid smart code</title><description>Jarrett is the only one who's got it so far. Do you really need the extra code complexity and lack of maintainability for that performance gain? Do you have evidence that the extra memory allocation was really hurting the system?</description><link>http://ayende.com/147457/stupid-smart-code#comment26</link><guid>http://ayende.com/147457/stupid-smart-code#comment26</guid><pubDate>Tue, 20 Dec 2011 14:44:54 GMT</pubDate></item><item><title>Rajeesh C V commented on Stupid smart code</title><description>If the method is not going to write big chunk of data, then
you have increased the complexity instead of keeping it simple</description><link>http://ayende.com/147457/stupid-smart-code#comment25</link><guid>http://ayende.com/147457/stupid-smart-code#comment25</guid><pubDate>Tue, 20 Dec 2011 14:26:21 GMT</pubDate></item><item><title>IgorM commented on Stupid smart code</title><description>You can use the lambda expression and execute it in one line:

Var reader = new StringReader(data);
reader.CopyTo(req.GetRequestStream();</description><link>http://ayende.com/147457/stupid-smart-code#comment24</link><guid>http://ayende.com/147457/stupid-smart-code#comment24</guid><pubDate>Tue, 20 Dec 2011 12:45:01 GMT</pubDate></item><item><title>Patrick Huizinga commented on Stupid smart code</title><description>@tobi, missed that one, although I only looked inside the loop

@JD, bytes is an int, not much GC-ing there :)</description><link>http://ayende.com/147457/stupid-smart-code#comment23</link><guid>http://ayende.com/147457/stupid-smart-code#comment23</guid><pubDate>Tue, 20 Dec 2011 11:56:05 GMT</pubDate></item><item><title>Sam commented on Stupid smart code</title><description>@JD - 'bytes' is actually an int. The joys of var and ambiguous variable names!</description><link>http://ayende.com/147457/stupid-smart-code#comment22</link><guid>http://ayende.com/147457/stupid-smart-code#comment22</guid><pubDate>Tue, 20 Dec 2011 11:56:00 GMT</pubDate></item><item><title>Sam commented on Stupid smart code</title><description>@tobi - my understanding is that the optimisation was intended to avoid allocating memory for the (potentially large) request body twice, not to minimise encoding time. </description><link>http://ayende.com/147457/stupid-smart-code#comment21</link><guid>http://ayende.com/147457/stupid-smart-code#comment21</guid><pubDate>Tue, 20 Dec 2011 11:35:04 GMT</pubDate></item></channel></rss>