Skip to content

Commit

Permalink
Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicletz committed Sep 23, 2024
1 parent fbf3754 commit 064431a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/block_process.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule BlockProcess do
@impl true
def init(state) do
EtsLru.new(__MODULE__, 300)
EtsLru.new(__MODULE__.State, 15)
EtsLru.new(__MODULE__.State, 20)
{:ok, state}
end

Expand Down
3 changes: 2 additions & 1 deletion lib/chain/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ defmodule Chain.State do

@spec set_account(Chain.State.t(), binary(), Chain.Account.t()) :: Chain.State.t()
def set_account(state = %Chain.State{accounts: accounts}, id = <<_::160>>, account) do
%{state | accounts: MutableMap.put(accounts, id, account), hash: nil, store: nil}
tree = CMerkleTree.insert(tree(state), id, Account.hash(account))
%{state | accounts: MutableMap.put(accounts, id, account), hash: nil, store: tree}
end

@spec delete_account(Chain.State.t(), binary()) :: Chain.State.t()
Expand Down
14 changes: 11 additions & 3 deletions lib/cmerkletree.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ defmodule CMerkleTree do

Enum.reduce(b, a_diffmap, fn {key, bvalue}, acc ->
case Map.get(acc, key) do
nil -> Map.put(acc, key, {nil, bvalue})
{^bvalue, nil} -> Map.delete(acc, key)
{avalue, nil} -> Map.put(acc, key, {avalue, bvalue})
nil ->
Map.put(acc, key, {nil, bvalue})

{avalue, nil} ->
if avalue.nonce == bvalue.nonce && avalue.balance == bvalue.balance &&
avalue.code == bvalue.code &&
Chain.Account.root_hash(avalue) == Chain.Account.root_hash(bvalue) do
Map.delete(acc, key)
else
Map.put(acc, key, {avalue, bvalue})
end
end
end)
end
Expand Down
29 changes: 23 additions & 6 deletions lib/model/chainsql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,21 @@ defmodule Model.ChainSql do
def state(block_hash) do
Writer.wait_for_flush(block_hash, "state")

{time, {type, result}} =
:timer.tc(fn ->
do_state(block_hash)
end)

time_ms = div(time, 1000)

if time_ms > 2000 do
Logger.warning("state(#{type}:#{Base16.encode(block_hash)}) took #{time_ms}ms")
end

result
end

defp do_state(block_hash) do
case fetch!("SELECT state FROM blocks WHERE hash = ?1", block_hash) do
%Chain.State{} = state ->
# IO.inspect(state, label: "preuncompact")
Expand All @@ -463,15 +478,17 @@ defmodule Model.ChainSql do
# IO.inspect(Profiler.stacktrace())
# end

IO.puts("uncompact(#{Base16.encode(block_hash)})")
Chain.State.uncompact(state)
{:uncompact, Chain.State.uncompact(state)}

{prev_hash, delta} ->
# For non-jump blocks we assume the jump block is cached for performance
EtsLru.fetch(__MODULE__.JumpState, {:state, prev_hash}, fn -> state(prev_hash) end)
|> Chain.State.clone()
|> Chain.State.apply_difference(delta)
|> Chain.State.normalize()
result =
EtsLru.fetch(__MODULE__.JumpState, {:state, prev_hash}, fn -> state(prev_hash) end)
|> Chain.State.clone()
|> Chain.State.apply_difference(delta)
|> Chain.State.normalize()

{:delta, result}
end
end

Expand Down
44 changes: 44 additions & 0 deletions notes.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
# 23rd Sept 2024
DetsPlus.delete_all_objects(:remoterpc_cache)

block_hash = Chain.blockhash(7919610)
{prev_hash, delta} = Model.ChainSql.fetch!("SELECT state FROM blocks WHERE hash = ?1", [block_hash])

a = Model.ChainSql.state(prev_hash)
b = Model.ChainSql.state(block_hash)


block_hash2 = Chain.blockhash(7919871)
{_prev_hash2, delta2} = Model.ChainSql.fetch!("SELECT state FROM blocks WHERE hash = ?1", [block_hash2])


for _ <- 1..10 do
:timer.tc(fn ->
Model.ChainSql.state(block_hash2)
end) |> elem(0) |> div(1000)
end



CMerkleTree.list_difference(a.accounts, b.accounts)

Profiler.fprof(fn ->
Model.ChainSql.state(block_hash)
end)

for _ <- 1..10 do
:timer.tc(fn ->
Model.ChainSql.state(block_hash)
end) |> elem(0) |> div(1000)
end

Profiler.fprof(fn ->
Network.Rpc.handle_jsonrpc(%{"id" => 0, "method" => "dio_edgev2", "params" => ["0xe48a6765746163636f756e748378d80a94107bd776052f1fc9a1e28e7d4bb999440b0bab11"]})
end)

for _ <- 1..10 do
:timer.tc(fn ->
Network.Rpc.handle_jsonrpc(%{"id" => 0, "method" => "dio_edgev2", "params" => ["0xe48a6765746163636f756e748378d76d94cd1a719c177d80f06bcdf0c617c20e48afc7b25f"]})
end) |> elem(0) |> div(1000)
end

# 16th Sept 2024

:timer.tc(fn ->
Expand Down

0 comments on commit 064431a

Please sign in to comment.