<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0"  xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Andy Tech Talk</title>
    <description>Have the courage to follow your heart and intuition, they somehow already know what you truly want to become.</description>
    <link>http://andyhu1007.iteye.com</link>
    <language>zh-CN</language>
    <copyright>Copyright 2003-2014, ITeye.com</copyright>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>ITeye - 做最棒的软件开发交流社区</generator>
    <atom:link href="http://andyhu1007.iteye.com/rss" rel="self" type="application/rss+xml" />
              <item>
            <title>Footprint: An Experimental HTML5 Application</title>
            <description>
              <![CDATA[
              <p>  为了参加HTML5大赛所编写的一个HTML5应用：<a href="http://fp.huzhenbo.name">Footprint</a>
。</p>
<p> </p>
<p>  Footprint的实现让我有机会体验了HTML5的一些特性，比如Geo Location，Local DB，File API等，另外，对node.js也进行了尝试，实现了一个简单（还不完整）的WebServer。</p>
<p> </p>
<p>  更重要的，是在JavaScript实践上尝试和印证了一些想法，比如ActiveRecord的JS实现，引入Custom Data Attributes之后对数据流的重新定向等等。</p>
<p> </p>
<p>  但这次作业的教训也很深刻，作为一个技术人员，在产品设计和UI设计上缺乏太多的常识和感觉。最近看了《<a href="http://book.douban.com/subject/3886044/">瞬间之美</a>
》后感触更深。这方面也是最近三个月的一个提升目标。</p>
<p> </p>
<p>  关于这个应用更详细的介绍请到这里：https://github.com/andyhu1007/bapi。</p>
<p> </p>
<p>----EOF-----</p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://andyhu1007.iteye.com/blog/866275#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              ]]>
            </description>
            <pubDate>Sun, 09 Jan 2011 12:41:35 +0800</pubDate>
            <link>http://andyhu1007.iteye.com/blog/866275</link>
            <guid isPermaLink="false">http://andyhu1007.iteye.com/blog/866275</guid>
          </item>
                  <item>
            <title>Scala和并发编程</title>
            <description>
              <![CDATA[
              <p>  Scala语言有很多优点，比如简洁、强大的表现力等等。但我最关注的，是它在并发编程方面的优势。</p>
<p> </p>
<p>  Scala通过强调<strong>对象的不变性</strong>
以及<strong>使用基于事件的模型进行线程间通信</strong>
使得实现并发应用变得简单。</p>
<h3> </h3>
<h3>  不变对象</h3>
<p>  并发编程之所以这么困难，很大一个原因在于对象的可变性。要在充斥着大量可变对象的程序里面实现安全并发，需要非常繁琐以及复杂易错的同步操作来保证状态更新的同步。</p>
<p> </p>
<p>  比如下面这段代码（java的例子），可能你会认为它已经是线程安全的了，因为所有的方法都已经被同步。</p>
<p> </p>
<pre class="java" name="code">class Account {
    private int balance;
    
    synchronized public int getBalance() {
      return balance;
    }
    
    synchronized public void incrementBalance() {
      balance++;
    }
}</pre>
 
<p>  但是问题来了，当顺序调用这两个方法的时候，比如：</p>
<p> </p>
<pre class="java" name="code">account.incrementBalance();
account.getBalance();</pre>
 
<p>  这时并不是线程安全的，在第一个方法调用结束之后，可能会被其它线程获取对象的锁，修改account的balance。</p>
<p> </p>
<p>  在命令式编程语言里面，命令查询分离是一个普遍使用的原则。意即：一个方法要么进行一次命令（执行一个操作，通常会修改状态），要么进行一次查询（返回一些状态， 但并不修改状态），而不应该同时执行命令以及查询。从面向对象的角度看，这是一个良好的设计；但从并发编程的角度看，它却带来了一些问题，使得并发编程实现更加困难。上面就是一个很好的例子。</p>
<p> </p>
<p>  要使得上面这段代码变得线程安全，可以通过破坏命令查询分离原则来实现：</p>
<p> </p>
<pre class="java" name="code">synchronized public int incrementAndGetBalance() {
  balance++;
  return balance;
}</pre>
<p> </p>
<p>  从上面的例子看到，在可变对象的环境中实现并发编程是困难的。而不变对象，意即线程安全的对象，使得我们不必担心共享对象会被多个线程同时修改而无法保持它的正确性：因为它本身就不可修改。我们可以把不变对象很放心地扔到多线程环境中，任意使用。</p>
<p> </p>
<p>  问题又来了，如果要“修改状态”怎么办？很简单，创建一个新的对象。还是上面那个例子，我们把它修改成一个Scala的不变对象类。并在伴生对象里面实现increment方法，此方法会返回一个新的account对象，balance值为原有对象的值加1所得。</p>
<p> </p>
<pre class="java" name="code">class Account (val balance: Integer) {
  def getBalance() = balance
}

object Account {
  def increment(account: Account): Account {
    new Account(account.getBalance() + 1)
  }
}</pre>
<p> </p>
<p> 通过强调不变对象的使用，并发编程变得简单了很多。</p>
<h3> </h3>
<h3> Actor</h3>
<p> 传统的并发是通过线程（thread）来实现的。在传统的并发模型中，程序被分成若干份同时执行的任务，并且所有任务都对一块共享的内存进行操作。在传统的并发模型会引起竞争问题，可以采取锁机制避免竞争问题，但同时这可能带来死锁等问题。</p>
<p> </p>
<p> Actor模型是另一种不同的并发模型，它很好地解决了在传统并发模型中竞争和死锁等问题。我们可以把一个由actor模型实现的并发程序看成是一个星系一样，星系里面有很多星球，每个星球都是一个actor，星球之间不共享任何资源，但是它们之间有通道来相互传递信息。</p>
<p> </p>
<p> 每个星球（actor）都有一个信箱来接受来自其它星球的任意信息，它会按照信息接收的顺序来处理，处理完一个信息然后接着处理下一个信息。可以按照信息类型来触发不同的行为。</p>
<p> </p>
<p> 同时，每个星球（actor）可以异步地（也可以同步，但不是这里谈论的重点）向其它任意星球发送任意消息，就是说，它发送消息之后不会等待返回信息而是直接执行接下来的操作。</p>
<p> </p>
<p>  下面是一个Actor的例子：</p>
<p> </p>
<pre class="java" name="code">import scala.actors.Actor
import scala.actors.Actor._
 
case class Increment(amount: Int)
case class Balance
 
class Account extends Actor {
    var balance: Int = 0;
 
    def act() = {
        while (true) {
            receive {
                case Increment(amount) =&gt;
                    balance += amount
                case Balance =&gt;
                    println("Balance is " + balance)
                    exit()
            }
        }
    }
}</pre>
<p> </p>
<p>  我们可以看到，程序里面定义两种不同的消息类型：Increment和Balance。Account是一个Actor，它跟外界的交互都是通过消息传递来实现：不论是increment，还是获取balance都是通过消息的方式来实现。当接受到不同的消息时，它会执行不同的行为。</p>
<p> </p>
<p>  我们也可以看到，Account的内部状态完全是自己控制的，接收到的消息是顺序执行的，所以我们不需要担心竞争问题。</p>
<h3> 小结</h3>
<p>  Scala就是这样，通过“使用基于事件的模型进行线程间通信”，并且“把不变对象作为消息进行传递”来实现一个并发编程模型。</p>
<p> </p>
<p>--EOF--</p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://andyhu1007.iteye.com/blog/858313#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              ]]>
            </description>
            <pubDate>Tue, 04 Jan 2011 00:38:12 +0800</pubDate>
            <link>http://andyhu1007.iteye.com/blog/858313</link>
            <guid isPermaLink="false">http://andyhu1007.iteye.com/blog/858313</guid>
          </item>
                  <item>
            <title>Ext JS框架初探</title>
            <description>
              <![CDATA[
              <p><a href="http://www.sencha.com/products/js/">Ext JS</a>
原先是YUI的一个扩展，如今独立发展成一个关注表现和行为的JS框架。</p>
<p> </p>
<p>Ext JS的主要特点是组件化，Web客户端的常见显示和行为，数据的处理以及服务器端的通信都通过组件的方式进行包装。比如表单、toolbar、下拉框、viewport以及tree等页面常见表现和行为，JSON和XML等数据类型的处理以及AJAX等行为的封装，都有相应的专门组件。</p>
<p> </p>
<p>这种处理方式的目的是对“数据”和“表现行为”进行隔离，让程序员摆脱HTML，CSS和JavaScript，而只需要关注跟强大的Ext JS组件接口的交互。但接口的学习和熟悉其实也需要一定的成本。</p>
<p> </p>
<p>这种方式带来了一些很大的问题：</p>
<ol>
<li>对“表现以及行为” -- 既HTML, CSS和JavaScript -- 的隔离和封装，使程序员失去了对这些代码的直接控制。</li>
<li>也是由于隔离，不得已造成了“邪恶的代码生成”。</li>
<li>这种方式也造成了HTML，CSS和JavaScript的高度耦合。</li>
</ol>
<p>个人非常不推崇这种绑架用户的实现理念。Ext JS虽然强大，但却以失去自由为代价。</p>
<p> </p>
<p>而相反，JQuery中多数插件以“注入方式”实现，比如在HTML tag添加一个class并会引入相应的表现和行为。这种低耦合的实现方式才是正确并值得推崇的。</p>
<p> </p>
<p>最后，<a href="http://book.douban.com/subject/3348792/">《Learning Ext JS》</a>
中有一句话感同深受：Web应用发展的一个奇观是，经过一个轮回之后，又回归到客户端/服务端的应用模型。</p>
<p> </p>
<p>--EOF--</p>
<p> </p>
<p> </p>
<p> </p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://andyhu1007.iteye.com/blog/769660#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              ]]>
            </description>
            <pubDate>Wed, 22 Sep 2010 18:29:57 +0800</pubDate>
            <link>http://andyhu1007.iteye.com/blog/769660</link>
            <guid isPermaLink="false">http://andyhu1007.iteye.com/blog/769660</guid>
          </item>
                  <item>
            <title>Flex中State和ViewStack的区别</title>
            <description>
              <![CDATA[
              <p>最近在一个Flex遗留系统上工作，Flex部分承担的主要是用户注册的业务。</p>
<p> </p>
<p>用户注册需要多个步骤，比如填写完基本信息，通过验证之后，来到联系信息填写表单，等等。步骤之间的切换通过改变当前页面的state来实现：State控制不同控件的显示、掩藏等。</p>
<p> </p>
<p>直觉不应该这么实现，今天看了<a href="http://book.douban.com/subject/3581107/">《Flex 3权威指南》</a>

之后，终于找到了答案：ViewStack才是这种场景的正确实现方式。</p>
<p> </p>
<p>State和ViewStack虽然都可以用来实现上述的场景，但使用state方式的问题在于它不是为这种场景设计的实现方式。所以，在实现过程中可能会非常痛苦。</p>
<p> </p>
<p>以下是State和ViewStack的一些区别：</p>
<p> </p>
<p> </p>
<table border="2">
<tr>
<td></td>
<td>State</td>
<td>ViewStack</td>
</tr>
<tr>
<td>使用场景</td>
<td>
<p>一个View的不同状态。比如一个页面在普通用户登录</p>
<p>时只能看到概要信息，而当管理用户登录时可以看到</p>
<p>详细信息，这是同一个view的两种不同状态。</p>
</td>
<td>
<p>用来控制差异很大的View之间的跳转和变迁。比如一个用户注册过程中的各个步骤。</p>
</td>
</tr>
<tr>
<td>技术实现</td>
<td>
<p>1. 需要一次初始化所有的components。</p>
<p>2. 基本上背后是一个MXML，内部component的较小变化。</p>
<p>3. 同上，基本对应一个数据。</p>
</td>
<td>
<p>1. 可以使用creationPolicy参数来控制延迟初始化。比如可以让stack中的每个view</p>
<p>在导航到它时才初始化。</p>
<p>2. 多个MXML之间的转移。</p>
<p>3. 同上，对应多个数据。</p>
<p>（当然，多个MXML和多个数据都不是必须的）</p>
</td>
</tr>
</table>
<p> </p>
<p> </p>
<p>－－ EOF －－ </p>
<p> </p>
<p>PS： 我还是Flex菜鸟一个，理解不当之处请朋友们指正。</p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://andyhu1007.iteye.com/blog/767721#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              ]]>
            </description>
            <pubDate>Mon, 20 Sep 2010 00:45:48 +0800</pubDate>
            <link>http://andyhu1007.iteye.com/blog/767721</link>
            <guid isPermaLink="false">http://andyhu1007.iteye.com/blog/767721</guid>
          </item>
                  <item>
            <title>Flex初探</title>
            <description>
              <![CDATA[
              <p>今天把<a href="http://book.douban.com/subject/3581107/">《Flex 3权威指南》</a>

快速读了一下，总结一下。</p>
<p> </p>
<h3>Web技术集的复制</h3>
<p>可以说，Flex中的所有技术都是Web技术集的一次复制。如下表</p>
<h4><br></h4>
<table border="2">
<tr>
<td>
<p> </p>
</td>
<td>
<p>Web技术</p>
</td>
<td>
<p>Flex技术</p>
</td>
</tr>
<tr>
<td>通信</td>
<td>HTTP</td>
<td>
<p>AMF －－ 一种在Flash和服务端之间交换数据的二进制格式，在服务端往往需要特定的adapter来解析数据。（Flex也可以通过Http Service和Web Service跟服务器端通信）。以上所有（包括AMF）的都是基于HTTP的协议，此外Flex还可以通过其它一些协议跟服务器通信。</p>
</td>
</tr>
<tr>
<td>标记语言</td>
<td>HTML</td>
<td>MXML －－ Flash的标记语言，拥有丰富的控件，比如DataGrid，Tree， 以及各种可视化图表等等。还可以自定义控件。</td>
</tr>
<tr>
<td>样式语言</td>
<td>CSS</td>
<td>除了使用标签的属性来设置样式之外，Flex也可通过CSS来控制样式</td>
</tr>
<tr>
<td>脚本语言</td>
<td>JavaScript</td>
<td>ActionScript －－ 跟JavaScript一样，是ECMAScript语系下的一种脚本语言。拥有更强的面向对象特质。</td>
</tr>
<tr>
<td>客户端数据</td>
<td>Cookie</td>
<td>SharedObject －－ 跟Cookie的功能一样，用来保存客户端的一些共享数据。但它可以处理更加复杂的数据格式。</td>
</tr>
</table>
<p> </p>
<p>此外，Flex 3开始，事件处理的机制也开始采用和DOM（JavaScript）一样的冒泡事件流机制。不过，它还可以自定义事件。</p>
<h3>RIA的设计理念</h3>
<p>RIA越加流行，但Flex可能不是大多数人的选择。而它的设计思想却是RIA的范本：客户端MVC。</p>
<p> </p>
<p>此外，模块化等思想也值得借鉴。</p>
<p> </p>
<h3>桌面部署<br>
</h3>
<p>Flex应用也可以通过AIR部署成一个桌面应用。</p>
<h3>小结</h3>
<p>随着HTML5和CSS3时代的迅速来临（浏览器的日益强大），Flex必将成为一个鸡肋。不知道什么时候回过头来看，它可能已湮没在历史长河中，被人遗忘。</p>
<p> </p>
<p>突然想起，在此书的第一章中，介绍了多种RIA技术。其中有一段对AJAX的描述：一种尚未被淘汰的技术。</p>
<p> </p>
<p>－－EOF－－</p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://andyhu1007.iteye.com/blog/767693#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              ]]>
            </description>
            <pubDate>Sun, 19 Sep 2010 23:32:33 +0800</pubDate>
            <link>http://andyhu1007.iteye.com/blog/767693</link>
            <guid isPermaLink="false">http://andyhu1007.iteye.com/blog/767693</guid>
          </item>
                  <item>
            <title>[Rails 3] 几个小问题</title>
            <description>
              <![CDATA[
              <p>1.  UJS怎么得到IE支持？</p>
<p> </p>
<p>     因为data attribute刚好可以在IE使用，但这决不是说IE支持HTML5。</p>
<p> </p>
<p>     有人在stackoverflow里面做了解答，具体请移步：<a href="http://stackoverflow.com/questions/2412947/do-html5-custom-data-attributes-work-in-ie-6">http://stackoverflow.com/questions/2412947/do-html5-custom-data-attributes-work-in-ie-6</a>
</p>
<p> </p>
<p> </p>
<p>2. header中生成的 csrf_meta_tag 有何用？</p>
<p> </p>
<p>    刚开始以为是HTML支持的新特性，能在提交的表单中自动加上它。在感叹我竟然落伍这么多的时候，突然一拍脑袋想起来，还是为了UJS嘛。</p>
<p> </p>
<p>3.  表单中的hidden input: _snowman </p>
<p> </p>
<p>     注意到表单中有一个hidden input：</p>
<p> </p>
<pre class="html" name="code">&lt;input name="_snowman" type="hidden" value="&amp;#9731;" /&gt;</pre>
<p> </p>
<p>     这个不平凡的东西，竟然是为了这么一个普世大众都熟知的问题：IE惹得祸。</p>
<p> </p>
<p>     有人还为了这个问题专门建了个网站，一个只有一个页面的网站。</p>
<p> </p>
<p>     具体请移步：<a href="http://railssnowman.info/">http://railssnowman.info/</a>
</p>
<p> </p>
<p> </p>
<p>     不过，我真的喜爱Rails 3对UTF-8的强有力支持。</p>
<p> </p>
<p>初探Rails 3，发现Rails的变化和进步真的很大。但也一下子陌生了很多。</p>
<p> </p>
<p>--EOF--</p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://andyhu1007.iteye.com/blog/737579#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              ]]>
            </description>
            <pubDate>Sun, 15 Aug 2010 00:19:39 +0800</pubDate>
            <link>http://andyhu1007.iteye.com/blog/737579</link>
            <guid isPermaLink="false">http://andyhu1007.iteye.com/blog/737579</guid>
          </item>
                  <item>
            <title>ActiveRecord的ORM问题域 Q&amp;A</title>
            <description>
              <![CDATA[
              <p><a href="http://blog.csdn.net/chelsea/archive/2009/12/28/5094652.aspx">切尔斯基的ORM问题域</a>
，他提供了Hibernate的解答，现在来看看在ActiveRcord中是如何解决的。大多数解决方案都大同小异，但有些完全不同。</p>
<h4>1. 加载根对象时如何避免加载大半个数据库</h4>
<p>    同样，“更多的时候，这是一个建模问题”。使用eager load还是lazy load是用户的选择，根据特定场景而定。</p>
<p> </p>
<p>    ActiveRecord和Hibernate一样，即可以在模型之间指定load方式，也可以在特定查询里面eager load，根据上句话，后者是更好的选择。</p>
<p> </p>
<p>    <strong>Session per request, Open Session in View pattern</strong>
</p>
<p> </p>
<p> </p>
<p> </p>
<p>    <strong>Database Connection</strong>
</p>
<p> </p>
<p>     在非线程安全时，每个application instance都会被分配一个database connection。实现线程安全之后：</p>
<p> </p>
<div class="quote_title"> 写道</div>
<div class="quote_div">Instead of a single database connection for a given Rails instance, there will be a pool of connections, allowing N database connections to be used by the M requests executing concurrently. It also means allowing requests to potentially execute without consuming a connection, so the number of live, active connections usually will be lower than the number of requests you can handle concurrently.</div>
 
<h4>2. 存储时如何更新整个对象图</h4>
<p>    切尔斯基已经做了解答：</p>
<div class="quote_title"> 切尔斯基 写道</div>
<div class="quote_div">框架支持级联更新. 是否应该级联更新, 哪些操作可以级联, 哪些不可以, 对象之间的哪些类型的关联可以级联, 哪些不可以, 则是程序员的责任。</div>
<h4>3. 存储时如何高效地更新整个对象图</h4>
<p>    答案未知。</p>
<p> </p>
<h4>4. 何时同步对象的内存状态和持久存储状态</h4>
<p>    基本上在transaction commit的时候。另外reload方法也会导致内存状态和持久存储状态的更新。</p>
<p> </p>
<p><strong>5. 如何确保在出错时保持对象内存状态和持久存储状态之间的一致性</strong>
</p>
<p> </p>
<div class="quote_title">切尔斯基 写道</div>
<div class="quote_div">数据库事务回滚, 清空内存缓存, 重新加载 </div>
<p> </p>
<h4>6. 如何保证引用的唯一性以避免可能的更新冲突</h4>
<p>    这个答案跟Hibernate的实现有很大不同（至少目前，Rails2.3.6）</p>
<p> </p>
<p>    ActiveRecord没有实现Identity Map。当然你要是想用，可以在这里找到一个简单实现：<a href="http://github.com/pjdavis/identity-map">http://github.com/pjdavis/identity-map</a>
 。不过ActiveRecord的identity map好像已经在计划之中了。<a href="http://github.com/pjdavis/identity-map"><br></a>
</p>
<p> </p>
<p>    所以，如果两个对象持有同一个record，则互相并不会知道对方的存在。如下：</p>
<p> </p>
<pre class="ruby" name="code">user1 = User.find(1)
user2 = User.find(1)

user1.update_attribute(:name =&gt; 'fuck')
user2.save  // 覆盖上个更新 </pre>
<p> </p>
<h4>7. 性能优化问题</h4>
<ol>
<li>
<p>N+1查询问题</p>
</li>
<li>
<p>分离查询模型和存储模型</p>
</li>
<li>
<p>尽量减少查询语句</p>
</li>
</ol>
<p>    关于1和3，在我的这篇文章中有详细解释：<a href="http://huzhenbo.name/blog/2010/01/16/rails-performance-tuning/">http://huzhenbo.name/blog/2010/01/16/rails-performance-tuning</a>
</p>
<p> </p>
<h4>8. ActiveRecord Query Cache</h4>
<div class="quote_title"> 写道</div>
<div class="quote_div">Query caching is a Rails feature that caches the result set returned by each query. If Rails encounters the same query again during the current request, it will used the cached result set as opposed to running the query against the database.</div>
<p> </p>
<p>     ActiveRecord的query cache起到和Hibernate中的session一样的第一级缓存的作用。</p>
<p> </p>
<h4>
<span style="color: #000000; font-size: x-small;">Reference：</span>
</h4>
<p><a href="http://martinfowler.com/eaaCatalog/identityMap.html">http://martinfowler.com/eaaCatalog/identityMap.html</a>
</p>
<p><span style="font-size: x-small;"><a href="http://takacsot.freeblog.hu/Files/martinfowler/identityMap.html">http://takacsot.freeblog.hu/Files/martinfowler/identityMap.html</a>
</span>
</p>
<p> </p>
<p> </p>
<p>----EOF----</p>
<p> </p>
<p> </p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://andyhu1007.iteye.com/blog/636063#comments" style="color:red;">已有 <strong>1</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              ]]>
            </description>
            <pubDate>Wed, 07 Apr 2010 13:02:02 +0800</pubDate>
            <link>http://andyhu1007.iteye.com/blog/636063</link>
            <guid isPermaLink="false">http://andyhu1007.iteye.com/blog/636063</guid>
          </item>
                  <item>
            <title>Ruby Fiber</title>
            <description>
              <![CDATA[
              <p>Ruby 1.9 带来了Fiber: <a href="http://www.infoq.com/news/2007/08/ruby-1-9-fibers">http://www.infoq.com/news/2007/08/ruby-1-9-fibers<br></a>
</p>
<p> </p>
<p>Ruby中的Fiber是一种semi-coroutine，第一次看到这个东西挺难理解。</p>
<p> </p>
<p> </p>
<p><strong>Subroutine VS Coroutine</strong>
</p>
<p> </p>
<p>理解semi-coroutine之前先来了解下什么是coroutine，子程序（subroutine）很容易理解，通过比较能看出它们之间的区别。</p>
<p> </p>
<table border="0">
<tr>
<td>Subroutine</td>
<td>Coroutine</td>
</tr>
<tr>
<td>
<p>The lifespan of subroutines is dictated by last in, first out (the last 
subroutine called is the first to return)</p>
</td>
<td>The lifespan of coroutines is dictated entirely by their use and need.</td>
</tr>
<tr>
<td>The start of a subroutine is the only point of entry</td>
<td>The start of a coroutine is the first point of entry and subsequent 
points of entry are following yield commands. </td>
</tr>
<tr>
<td>Subroutines can return only once</td>
<td>coroutines can return (yield) several times</td>
</tr>
</table>
<p> </p>
<p>Practically, yielding returns the result to the calling coroutine and 
gives it back control, like a usual subroutine. However, the next time 
the coroutine is called, the execution does not start at the beginning 
of the coroutine but just after the yield call.</p>
<p> </p>
<p>那什么是semi-coroutine：<span style="font-weight: bold;">Semi-Coroutines </span>
are <span style="font-weight: bold;">asymmetric Coroutines</span>
 which are 
limited in their choice of transfer of control. Asymmetric Coroutines 
can only transfer control <span style="font-style: italic;">back</span>
 
to their caller, where Coroutines are free to  transfer control to any 
other Coroutine, as long as they have a handle to it.</p>
<p> </p>
<p><strong>Fiber VS Thread</strong>
</p>
<p> </p>
<p>Ruby的Fiber是一种控制结构（后面会说到Fiber::Core），它的运行方式和操作方法很像thread。</p>
<p><strong><br></strong>
</p>
<table border="0">
<tr>
<td>Fiber</td>
<td>Thread</td>
</tr>
<tr>
<td>not concurrency</td>
<td>concurrency</td>
</tr>
<tr>
<td>resume, suspend(call Fiber.yield或者等到block结束)</td>
<td>resume, suspend</td>
</tr>
<tr>
<td>return and give control back to caller</td>
<td>not return or give control back unless join it<br>
</td>
</tr>
</table>
<p> </p>
<p><strong><br></strong>
</p>
<p><strong>Fiber VS Closure</strong>
</p>
<p> </p>
<p>看下面这个方法：</p>
<p> </p>
<pre class="ruby" name="code">fib = Fiber.new do
   f1 = f2 = 1
   loop do
     Fiber.yield f1 (每次resume，在遇到Fiber.yield或者block结尾时结束)
     f1, f2 = f2, f1 + f2 （第二次resume，从这里开始）
   end
end

10.times { puts fib.resume }</pre>
 
<p> </p>
<p>很容易想到用closure去实现它：</p>
<p> </p>
<pre class="ruby" name="code">def local_proc
    f1 = f2 = 1
    return Proc.new {f1, f2 = f2, f1 + f2; p f1;}
end

proc1 = local_proc

10.times {proc1.call}</pre>
<p> </p>
<p> </p>
<p>上面两个方法的实现方式是很不相同的。Fiber方式通过 enable the automatic conversion of internal iterators, 
such as each, to enumerator or external iterators来实现。</p>
<p><strong><br></strong>
</p>
<p><strong>Fiber::Core VS Fiber</strong>
</p>
<p> </p>
<p>相较于Fiber，Fiber::Core是真正的coroutine -- 一种可以实现userspace thread的轻量级线程。</p>
<p> </p>
<div class="quote_title"> 写道</div>
<div class="quote_div">Next to implementing control structures, Coroutines provide a way to use <strong>lightweight concurrency</strong>
. In effect, they allow to <strong>implement userspace threads</strong>
 with cooperative scheduling. The Coroutines can either yield control to each other, or have centralized scheduling by handing off control to one scheduler Coroutine which then decides who gets scheduled next.</div>
<p> </p>
<p>Ruby1.9实现了kernal threads（sometimes too heavy），所以提供一种轻量级的并行化方式是必要的。</p>
<p> </p>
<div class="quote_title"> 写道</div>
<div class="quote_div">Nevertheless, creating a lot of kernel threads still has a lot of overhead, or might simply cause problems on OSes that have hard thread limits or struggle with large numbers of threads. It's in these cases, when a lightweight alternative is useful. It allows the code to be split among  threads, if that is the logic, straightforward solution, but keeps the overhead down. </div>
<p> </p>
<p><strong>Fiber for JRuby</strong></p>
<p> </p>
<p>实现JRuby等其它ruby实现的Fiber会遇到一些困难：</p>
<p> </p>
<div class="quote_title"> 写道</div>
<div class="quote_div">If Fibers get adopted in Ruby, this will create headaches for Ruby implementations targeting the JVM or CLR, such as JRuby, XRuby, Ruby.NET or IronRuby. <strong>None of them currently support Continuations because manipulating or reading the callstack is hard or impossible to do in these VMs</strong>
. The lack of Continuations is a controversial issue, but doesn't seem to have caused problems with e.g. JRuby, because they are not widely used in Ruby. The only use of them in the Ruby 1.8 standard library is the Generator implementation, but, for instance, JRuby 1.0, solved this by implementing the same functionality without using Continuations.<br>
</div>
<p> </p>
<p>即使用workarounds，也有一些值得担心的性能问题：</p>
<p> </p>
<div class="quote_title"> 写道</div>
<div class="quote_div">While it's certainly possible to implement these features using workarounds, the question is whether these workaround will cause performance regressions. If, for instance, <strong>call stacks must be emulated on the heap, instead of using the VM's stack, this can lead to lower performance or prevent (JIT) compiler optimizations from being applied.</strong>
 Workarounds for asymmetric Coroutines would be easier to do, as they could make use of the VM's stack for method invocations.</div>
 
<p> </p>
<p><strong>用途</strong>
</p>
<p> </p>
<p>Coroutines are well-suited for implementing more familiar program 
components such as <a class="mw-redirect" href="http://en.wikipedia.org/wiki/Cooperative_multitasking" title="Cooperative multitasking">cooperative tasks</a>
,
 <a href="http://en.wikipedia.org/wiki/Iterator" title="Iterator">iterators</a>
,
 <a href="http://en.wikipedia.org/wiki/Lazy_evaluation" title="Lazy 
evaluation">infinite lists</a>
 and <a href="http://en.wikipedia.org/wiki/Pipeline_%28software%29" title="Pipeline (software)">pipes</a>
.。</p>
<p> </p>
<p>References:</p>
<p>coroutine: <a href="http://en.wikipedia.org/wiki/Coroutine">http://en.wikipedia.org/wiki/Coroutine</a>
</p>
<p>用Ruby的fiber实现pipe的例子：<a href="http://pragdave.blogs.pragprog.com/pragdave/2007/12/pipelines-using.html">http://pragdave.blogs.pragprog.com/pragdave/2007/12/pipelines-using.html</a>
</p>
<p> </p>
<p> </p>
<p>----EOF----</p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://andyhu1007.iteye.com/blog/634916#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              ]]>
            </description>
            <pubDate>Tue, 06 Apr 2010 22:13:15 +0800</pubDate>
            <link>http://andyhu1007.iteye.com/blog/634916</link>
            <guid isPermaLink="false">http://andyhu1007.iteye.com/blog/634916</guid>
          </item>
                  <item>
            <title>线程安全的Rails</title>
            <description>
              <![CDATA[
              <div style="margin: 8px;">
<p>Thread Safe Rails？ 关于线程安全的Rails，请大家慎重采用，具体看这篇文章：<a>http://m.onkey.org/thread-safety-for-your-rails</a>。</p>
<p> </p>
<p>上一个项目我们怀着战战兢兢的心情开启了多线程，结果是出乎意料得好。开启多线程后没遇到什么困难，可能会遇到的问题有两点：</p>
<p> </p>
<p>1. 去掉class variable (当然，基本上你不应该使用类变量)</p>
<p> </p>
<p>2. 另外一件事情，就是在multiple threads下面Rails需要eager load libs。请移步：<a href="http://gigix.thoughtworkers.org/2009/7/17/move-to-multi-threaded-rails">http://gigix.thoughtworkers.org/2009/7/17/move-to-multi-threaded-rails</a></p>
<p> </p>
<p> </p>
<p>多线程的Rails对于performance有极大提升，请再次移步：<a href="http://blog.headius.com/2008/08/qa-what-thread-safe-rails-means.html"><span style="font-size: x-small;">http://blog.headius.com/2008/08/qa-what-thread-safe-rails-means.html</span> </a><span style="font-size: x-small;">  （请自行fan qiang）</span></p>
<p> </p>
<p><strong>基本上意思是内存使用量的锐减和数据库连接资源的优化:</strong></p>
<p> </p>
<div class="quote_title">写道</div>
<div class="quote_div">In a typical Mongrel setup, handling 10 concurrent requests means you have to have 10 copies of Rails loaded, 10 copies of your application loaded, 10 in-memory data caches, 10 database connections...everything has to be scaled in lock step for every additional request you want to handle concurrently.</div>
<p> </p>
<div class="quote_title">写道</div>
<div class="quote_div">Instead of a single database connection for a given Rails instance, there will be a pool of connections, allowing N database connections to be used by the M requests executing concurrently. It also means allowing requests to potentially execute without consuming a connection, so the number of live, active connections usually will be lower than the number of requests you can handle concurrently.<br>
</div>
 
<p>References:</p>
<p> </p>
<p>教程： <a href="http://m.onkey.org/2008/10/23/thread-safety-for-your-rails">http://m.onkey.org/2008/10/23/thread-safety-for-your-rails</a></p>
<p>Slides:<span style="font-size: x-small;">  <a href="http://www.slideshare.net/Naoto.Takai/jruby-on-rails-and-thread-safety-presentation">http://www.slideshare.net/Naoto.Takai/jruby-on-rails-and-thread-safety-presentation</a> <br></span>Q &amp; A: <a href="http://stackoverflow.com/questions/647642/ruby-rails-thread-safety">http://stackoverflow.com/questions/647642/ruby-rails-thread-safety</a></p>
<p> </p>
<p>----EOF----</p>
</div>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://andyhu1007.iteye.com/blog/634890#comments" style="color:red;">已有 <strong>2</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              ]]>
            </description>
            <pubDate>Tue, 06 Apr 2010 21:33:35 +0800</pubDate>
            <link>http://andyhu1007.iteye.com/blog/634890</link>
            <guid isPermaLink="false">http://andyhu1007.iteye.com/blog/634890</guid>
          </item>
                  <item>
            <title>To be an Eligible JavaScript Programmer</title>
            <description>
              <![CDATA[
              <p><a href="http://book.douban.com/subject/1775608/"><img style="border: 0;" alt="" src="http://t.douban.com/lpic/s4251169.jpg"></a>
</p>
<p>Along with the popularity of  RIA and the arrival of HTML5 in near 
future, JavaScript is more and more important.</p>
<p>Are you an eligible JavaScript programmer? Please check the 
following list.</p>
<h4>Core JavaScript</h4>
<ol>
<li>Is JavaScript an object-oriented language or object-based language? 
Why?</li>
<li>What does the keyword this refer to in a method? Is there an 
exception for nested function? And how about the keyword this in an 
event handler of client JavaScript?</li>
<li>How does the JavaScript function scoped? Lexically or dynamically? 
What is the scope chain of a function consisted of? What are call object
 and global object?</li>
<li>All JavaScript functions are closures, is it true? Why?</li>
<li>What is constructor?</li>
<li>What is prototype?</li>
<li>How to simulate classes in JavaScript?</li>
<li>How to create modules and namespaces in JavaScript?</li>
</ol>
<h4>Client-Side JavaScript</h4>
<ol>
<li>What is global object and global execution context of client-side 
JavaScript? What happens when there are multiple frames in a window or 
when we need to interact with multiple windows?</li>
<li>What is DOM - document object model? What's the differences between 
legacy DOM and W3C DOM?</li>
<li>What is unobtrusive JavaScript?</li>
<li>How many ways to embed JavaScript in HTML? What are they?</li>
<li>What is the execution order of client-side JavaScript?</li>
<li>Is client-side JavaScript multi-threaded or single-threaded? How 
about the core JavaScript?</li>
<li>What is same-origin policy of client-side JavaScript?</li>
<li>How does client-side JavaScript deal  with event handler? What's the
 differences between event handlers in legacy DOM and DOM level 2?</li>
<li>How can client-side JavaScript communicate with a web server without
 causing web browser to reload current page? Are there any other ways 
besides XMLHttpRequest?</li>
<li>How to create, specify and submit HTTP request through 
XMLHttpRequest? How to synchronously or asynchronously retrieve the 
response?</li>
<li>Do you fully understand the sentence: "Ajax takes control of HTTP 
away from the browser itself"? What kind of user experience problems 
arise because of it? How to improve the user experience of Ajax-based 
web applications？</li>
<li>Do you know that W3C designed the DOM API to be language-neutral and
 focused primary on XML documents? Its use with HTML documents is 
through an optional extension module.</li>
<li> What can JavaScript do with client-side graphics, Java Applets and 
Flash movies?</li>
</ol>
<p> </p>
<p>原文：<a href="http://huzhenbo.name/blog/2010/04/04/to-be-an-eligible-javascript-programmer/">http://huzhenbo.name/blog/2010/04/04/to-be-an-eligible-javascript-programmer/</a></p>
              
              <br/><br/>
              <span style="color:red;">
                <a href="http://andyhu1007.iteye.com/blog/632755#comments" style="color:red;">已有 <strong>0</strong> 人发表留言，猛击-&gt;&gt;<strong>这里</strong>&lt;&lt;-参与讨论</a>
              </span>
              <br/><br/><br/>
<span style="color:#E28822;">ITeye推荐</span>
<br/>
<ul><li><a href='/clicks/433' target='_blank'><span style="color:red;font-weight:bold;">—软件人才免语言低担保 赴美带薪读研！— </span></a></li></ul>
<br/><br/><br/>
              ]]>
            </description>
            <pubDate>Sun, 04 Apr 2010 16:43:40 +0800</pubDate>
            <link>http://andyhu1007.iteye.com/blog/632755</link>
            <guid isPermaLink="false">http://andyhu1007.iteye.com/blog/632755</guid>
          </item>
          </channel>
</rss>
