From b1a92932c38c0bb695382bc85423da54d4640485 Mon Sep 17 00:00:00 2001 From: lijie <1713699517@qq.com> Date: Thu, 27 Apr 2023 20:04:46 +0800 Subject: [PATCH] =?UTF-8?q?ft:=20ets=20=E5=B9=B6=E5=8F=91=E5=86=99?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=20=E9=81=BF=E5=85=8D=E5=86=99=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=E7=9A=84=E4=BC=98=E5=8C=96=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/docs/erlang/etsmatch.hrl.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/docs/erlang/etsmatch.hrl.md b/src/docs/erlang/etsmatch.hrl.md index 57c0e34..1ef15c3 100644 --- a/src/docs/erlang/etsmatch.hrl.md +++ b/src/docs/erlang/etsmatch.hrl.md @@ -18,4 +18,16 @@ -type nonCompositeTerm() :: term(). %% (not list or tuple or map) -type constant() :: {const, term()}. -type guardFunction() :: boolFunction() | abs | element | hd | length | map_get | map_size | node | round | size | bit_size | tl | trunc | '+' | '-' | '*' | 'div' | 'rem' | 'band' | 'bor' | 'bxor' | 'bnot' | 'bsl' | 'bsr' | '>' | '>=' | '<' | '=<' | '=:=' | '==' | '=/=' | '/=' | self. --type matchBody() :: [ conditionExpression(), ... ]. \ No newline at end of file +-type matchBody() :: [ conditionExpression(), ... ]. + +ets表并发写优化 + 1.核心思想还是避免数据写操作是并发操作 这种可以将ets 设置为bag 相当于有主key 是并发的 然后次key的拥有者只能自己写自己的数据 + 2. 使用ets:select_replace 实现无锁替换操作 + update_with_fun(Table, Key, Fun) -> + [Old] = ets:lookup(Table, Key), + New = Fun(Old), + Success = (1 =:= ets:select_replace(Table, [{Old, [], [{const, New}]}])), + case Success of + true -> New; + false -> update_with_fun(Table, Key, Fun) + end. \ No newline at end of file