Posts Tagged ‘MatchSpec’

Erlang match_spec引擎介绍和应用

October 7th, 2011 2 comments

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: Erlang match_spec引擎介绍和应用


A “match specification” (match_spec) is an Erlang term describing a small “program” that will try to match something (either the parameters to a function as used in the erlang:trace_pattern/2 BIF, or the objects in an ETS table.). The match_spec in many ways works like a small function in Erlang, but is interpreted/compiled by the Erlang runtime system to something much more efficient than calling an Erlang function. The match_spec is also very limited compared to the expressiveness of real Erlang functions.

说白了它就是个erlang term得过滤器,可以让用户来自己选择需要匹配什么,需要从term里面抽取什么数据。那同学可能就有疑问了,Erlang的函数不是很强大吗,它能做的函数也能做,那为什么要重新费劲做一个呢?
Erlang实现这个match_spec得原因有2个:1. 运行效率 2. 小巧可以在运行期使用。

它的实现思路是: match_spec是个引擎,有自己的语法,先把语句编译成专用的opcode, 然后在在匹配的时候运行opcode,获取结果,可以理解为erlang的DSL。

Read more…

Post Footer automatically generated by wp-posturl plugin for wordpress.