<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erlang非业余研究 &#187; binary</title>
	<atom:link href="http://blog.yufeng.info/archives/tag/binary/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.yufeng.info</link>
	<description>Erlang系统深度探索和应用</description>
	<lastBuildDate>Tue, 17 Jan 2012 06:05:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>R14A实现了EEP31，添加了binary模块</title>
		<link>http://blog.yufeng.info/archives/574</link>
		<comments>http://blog.yufeng.info/archives/574#comments</comments>
		<pubDate>Fri, 21 May 2010 03:24:55 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA[AC]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[BM]]></category>
		<category><![CDATA[EEP31]]></category>
		<category><![CDATA[match]]></category>
		<category><![CDATA[R14A]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=574</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: R14A实现了EEP31，添加了binary模块 Erlang的binary数据结构非常强大，而且偏向底层，在作网络程序的时候，很方便的能够和二进制协议对应起来。但是由于这个数据结构加入erlang语言的时间不是很长，相关的配套模块不是很多。 在binary的匹配，替换，修改就显的非常麻烦。 于是有了EEP31 。 R14A昨天已经实现了这个功能， 在stdlib下添加了个binary模块。 这个模块大部分功能是由BIF实现的， 同时充分考虑了CPU使用的公平性，源码大部分在erl_bif_binary.c下。 还添加了个gurad函数： binary_part进一步方便我们写匹配条件。 我们在源码里面发现了以下注释： /* * The native implementation functions for the module binary. * Searching is implemented using aither Boyer-More or Aho-Corasick * depending on number of searchstrings (BM if one, AC if more than one). * Native implementation is mostly for efficiency, [...]]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/574">R14A实现了EEP31，添加了binary模块</a></p>
</div>
<p>Erlang的binary数据结构非常强大，而且偏向底层，在作网络程序的时候，很方便的能够和二进制协议对应起来。但是由于这个数据结构加入erlang语言的时间不是很长，相关的配套模块不是很多。 在binary的匹配，替换，修改就显的非常麻烦。 于是有了<a href="http://www.erlang.org/eeps/eep-0031.html">EEP31</a> 。 R14A昨天已经实现了这个功能， 在stdlib下添加了个binary模块。 这个模块大部分功能是由BIF实现的， 同时充分考虑了CPU使用的公平性，源码大部分在erl_bif_binary.c下。 还添加了个gurad函数： binary_part进一步方便我们写匹配条件。</p>
<p>我们在源码里面发现了以下注释：<br />
/*<br />
 * The native implementation functions for the module binary.<br />
 * Searching is implemented using aither Boyer-More or Aho-Corasick<br />
 * depending on number of searchstrings (BM if one, AC if more than one).<br />
 * Native implementation is mostly for efficiency, nothing<br />
 * (except binary:referenced_byte_size) really *needs* to be implemented<br />
 * in native code.<br />
 */</p>
<p>这个模块兼顾了效率和方便性，使用起来就大大简化了代码的复杂度，有福气了。</p>
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/574/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>R13B04在细化Binary heap</title>
		<link>http://blog.yufeng.info/archives/186</link>
		<comments>http://blog.yufeng.info/archives/186#comments</comments>
		<pubDate>Thu, 14 Jan 2010 07:11:40 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[fragment]]></category>
		<category><![CDATA[R13B04]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=186</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: R13B04在细化Binary heap 从github otp的更新日志可以清楚的看到otp R13B04在binary heap的细化上做了很多工作: 1. 提供参数 限制binary的最小limit. 2. binary_to_term 加多参数来保证安全, 对于外来binary解码的安全性检查 比如说 避免atom的滥用. 3. 更高效率的处理binary fragment. 4. 加强GC对binary碎片的回收操作. 这个对于服务器性能很大帮助. Post Footer automatically generated by wp-posturl plugin for wordpress.]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/186">R13B04在细化Binary heap</a></p>
</div>
<p>从github otp的更新日志可以清楚的看到otp R13B04在binary heap的细化上做了很多工作:</p>
<p>1. 提供参数 限制binary的最小limit.</p>
<p>2. binary_to_term 加多参数来保证安全, 对于外来binary解码的安全性检查 比如说 避免atom的滥用.</p>
<p>3. 更高效率的处理binary fragment.</p>
<p>4. 加强GC对binary碎片的回收操作.</p>
<p>这个对于服务器性能很大帮助.
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/186/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>R13B03 binary vheap有助减少binary内存压力</title>
		<link>http://blog.yufeng.info/archives/172</link>
		<comments>http://blog.yufeng.info/archives/172#comments</comments>
		<pubDate>Sun, 29 Nov 2009 08:09:22 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[r13b03]]></category>
		<category><![CDATA[vheap]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=172</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: R13B03 binary vheap有助减少binary内存压力 R13B03 binary vheap有助减少binary内存压力. 参看：http://www.erlang.org/download/otp_src_R13B03.readme OTP-8202 A new garbage collecting strategy for binaries which is more aggressive than the previous implementation. Binaries now has a virtual binary heap tied to each process. When binaries are created or received to a process it will check if the heap limit has been [...]]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/172">R13B03 binary vheap有助减少binary内存压力</a></p>
</div>
<p>R13B03 binary vheap有助减少binary内存压力.</p>
<p>参看：<a href="http://www.erlang.org/download/otp_src_R13B03.readme">http://www.erlang.org/download/otp_src_R13B03.readme</a></p>
<blockquote><p>OTP-8202  A new garbage collecting strategy for binaries which is more<br />
aggressive than the previous implementation. Binaries now has<br />
a virtual binary heap tied to each process. When binaries are<br />
created or received to a process it will check if the heap<br />
limit has been reached and if a reclaim should be done. This<br />
imitates the behavior of ordinary Erlang terms. The virtual<br />
heaps are grown and shrunk like ordinary heaps. This will<br />
lessen the memory footprint of binaries in a system.</p></blockquote>
<p>原来的binary和其他类型的eterm是混在一起的，当进程的heap用完的时候，才进行GC，腾出空间。现在是把binary单独抽取出来，单独计数， 当binary用量过大的时候，马上就GC。</p>
<p>这个策略对于我们的服务器程序很有帮助，因为我们的服务器程序基本上包都是binary, 很容易出现binary无法及时回收，导致内存耗光的情况。</p>
<p>具体的改进效果待进一步的观察。
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/172/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>binary的常量优化</title>
		<link>http://blog.yufeng.info/archives/46</link>
		<comments>http://blog.yufeng.info/archives/46#comments</comments>
		<pubDate>Sun, 11 Oct 2009 05:59:54 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[const fold]]></category>
		<category><![CDATA[opt]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=46</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: binary的常量优化 erlang的binary在这个网络程序里面占着非常重要的地位，所以otp团队采用了非常多的优化手段包括： 1. binary操作对应着 opcode 2. 根据生命期和作用，有4种类型的binary 3. hipe优化，把bs_操作直接翻译成asm指令 4. 编译器层面消除无必须的操作。 下面的例子就是演示4的特性： 所以我们在使用binary的时候， 尽可能的利用这个特性。 Post Footer automatically generated by wp-posturl plugin for wordpress.]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/46">binary的常量优化</a></p>
</div>
<p>erlang的binary在这个网络程序里面占着非常重要的地位，所以otp团队采用了非常多的优化手段包括：</p>
<p>1.  binary操作对应着 opcode<br />
2. 根据生命期和作用，有4种类型的binary<br />
3. hipe优化，把bs_操作直接翻译成asm指令<br />
4. 编译器层面消除无必须的操作。</p>
<p>下面的例子就是演示4的特性：</p>
<pre class="brush: bash; title: ; notranslate">
yu-fengdemacbook-2:~ yufeng$ cat bin.erl
</pre>
<pre class="brush: erlang; title: ; notranslate">
-module(bin).
-export([start/1]).

start(A)-&gt;
    B1= &lt;&lt;12&gt;&gt;,
    B2 = &lt;&lt;B1/binary, 5.0/float&gt;&gt;,
    B3= &lt;&lt;B2/binary,  &quot;yes&quot;&gt;&gt;,
% B3的值是预先可以知道的， 无需一步步的构造

    &lt;&lt;&quot;abcd&quot;, 3:32,B3:128/binary,_/binary&gt;&gt; = &lt;&lt;&quot;abcd1234&quot;,A/binary,2:32, 8773:64, &quot;a&quot;, 5.0/float&gt;&gt;.

％ 2:32, 8773:64, &quot;a&quot;, 5.0/float 这些都是预先知道的 直接翻译成二进制流。
</pre>
<pre class="brush: bash; title: ; notranslate">
yu-fengdemacbook-2:~ yufeng$ erlc +&quot;'S'&quot; bin.erl
yu-fengdemacbook-2:~ yufeng$ cat bin.S
</pre>
<pre class="brush: erlang; title: ; notranslate">
{module, bin}.  %% version = 0

{exports, [{module_info,0},{module_info,1},{start,1}]}.

{attributes, []}.

{labels, 8}.

{function, start, 1, 2}.
  {label,1}.
    {func_info,{atom,bin},{atom,start},1}.
  {label,2}.
    {move,{integer,0},{x,1}}.
    {gc_bif,byte_size,{f,0},2,[{x,0}],{x,2}}.
    {bs_add,{f,0},[{x,1},{x,2},1],{x,1}}.
    {bs_add,{f,0},[{x,1},{integer,29},1],{x,1}}.
    {bs_init2,{f,0},{x,1},0,1,{field_flags,[]},{x,1}}.
    {bs_put_string,8,{string,&quot;abcd1234&quot;}}.
    {bs_put_binary,{f,0},{atom,all},8,{field_flags,[unsigned,big]},{x,0}}.

％％ 一步到位
    {bs_put_string,21,
                   {string,[0,0,0,2,0,0,0,0,0,0,34,69,97,64,20,0,0,0,0,0,0]}}.

    {test,bs_start_match2,{f,3},[{x,1},2,0,{x,0}]}.
    {test,bs_match_string,{f,3},[{x,0},64,{string,[97,98,99,100,0,0,0,3]}]}.
％％  一步到位

    {test,bs_get_binary2,
          {f,3},
          [{x,0},
           2,
           {integer,128},
           8,
           {field_flags,[{anno,[8,{file,&quot;./bin.erl&quot;}]},unsigned,big]},
           {x,2}]}.
    {test,bs_skip_bits2,
          {f,3},
          [{x,0},
           {atom,all},
           8,
           {field_flags,[{anno,[8,{file,&quot;./bin.erl&quot;}]},unsigned,big]}]}.
    {test,is_eq_exact,
          {f,3},
          [{x,2},{literal,&lt;&lt;12,64,20,0,0,0,0,0,0,121,101,115&gt;&gt;}]}.
％％  一步到位

    {move,{x,1},{x,0}}.
    return.
  {label,3}.
    {badmatch,{x,1}}.

{function, module_info, 0, 5}.
  {label,4}.
    {func_info,{atom,bin},{atom,module_info},0}.
  {label,5}.
    {move,{atom,bin},{x,0}}.
    {call_ext_only,1,{extfunc,erlang,get_module_info,1}}.

{function, module_info, 1, 7}.
  {label,6}.
    {func_info,{atom,bin},{atom,module_info},1}.
  {label,7}.
    {move,{x,0},{x,1}}.
    {move,{atom,bin},{x,0}}.
    {call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
</pre>
<p>所以我们在使用binary的时候， 尽可能的利用这个特性。
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/46/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

