Skip to content

Commit

Permalink
chore: compact type increment counts mutations into a single one (#1453)
Browse files Browse the repository at this point in the history
  • Loading branch information
sborrazas committed Jul 10, 2023
1 parent d2ba17a commit 1b5f77e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
32 changes: 0 additions & 32 deletions lib/ae_mdw/db/mutations/increment_type_count_mutation.ex

This file was deleted.

37 changes: 37 additions & 0 deletions lib/ae_mdw/db/mutations/type_counters_mutation.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
defmodule AeMdw.Db.TypeCountersMutation do
@moduledoc """
Increments the transaction type counts.
"""

alias AeMdw.Db.State
alias AeMdw.Db.Model
alias AeMdw.Node

require Model

@derive AeMdw.Db.Mutation
defstruct [:type_counts]

@typep type_counts() :: %{Node.tx_type() => pos_integer()}
@opaque t() :: %__MODULE__{
type_counts: type_counts()
}

@spec new(type_counts()) :: t()
def new(type_counts), do: %__MODULE__{type_counts: type_counts}

@spec execute(t(), State.t()) :: State.t()
def execute(%__MODULE__{type_counts: type_counts}, state) do
Enum.reduce(type_counts, state, fn {tx_type, type_increment}, state ->
State.update(
state,
Model.TypeCount,
tx_type,
fn Model.type_count(count: count) = type_count ->
Model.type_count(type_count, count: count + type_increment)
end,
Model.type_count(index: tx_type, count: 0)
)
end)
end
end
12 changes: 9 additions & 3 deletions lib/ae_mdw/db/sync/block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ defmodule AeMdw.Db.Sync.Block do
alias AeMdw.Db.Sync.Transaction
alias AeMdw.Db.WriteMutation
alias AeMdw.Db.Mutation
alias AeMdw.Db.TypeCountersMutation
alias AeMdw.Sync.MutationsCache
alias AeMdw.Log
alias AeMdw.Node, as: AE
Expand Down Expand Up @@ -130,11 +131,11 @@ defmodule AeMdw.Db.Sync.Block do
mb_model = Model.block(index: {height, mbi}, tx_index: first_txi, hash: mb_hash)
block_mutation = WriteMutation.new(Model.Block, mb_model)

{ts, mutations} =
{ts, {txs_mutations, type_counters}} =
:timer.tc(fn ->
mb_txs
|> Enum.with_index(first_txi)
|> Enum.reduce([block_mutation], fn {signed_tx, txi}, mutations ->
|> Enum.map_reduce(%{}, fn {signed_tx, txi}, type_counters ->
transaction_mutations =
Transaction.transaction_mutations(
signed_tx,
Expand All @@ -145,12 +146,17 @@ defmodule AeMdw.Db.Sync.Block do
events
)

mutations ++ transaction_mutations
{type, _tx} = :aetx.specialize_type(:aetx_sign.tx(signed_tx))

{transaction_mutations, Map.update(type_counters, type, 1, &(&1 + 1))}
end)
end)

type_counters_mutation = TypeCountersMutation.new(type_counters)
_sum = :ets.update_counter(:sync_profiling, {:txs, height}, ts, {{:txs, height}, 0})

mutations = [block_mutation, type_counters_mutation | txs_mutations]

{mutations, first_txi + length(mb_txs)}
end

Expand Down
2 changes: 0 additions & 2 deletions lib/ae_mdw/db/sync/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ defmodule AeMdw.Db.Sync.Transaction do
alias AeMdw.Db.ContractCallMutation
alias AeMdw.Db.ContractCreateMutation
alias AeMdw.Db.ContractCreateCacheMutation
alias AeMdw.Db.IncrementTypeCountMutation
alias AeMdw.Db.Model
alias AeMdw.Db.NameRevokeMutation
alias AeMdw.Db.NameTransferMutation
Expand Down Expand Up @@ -99,7 +98,6 @@ defmodule AeMdw.Db.Sync.Transaction do
[
WriteMutation.new(Model.Tx, m_tx),
WriteMutation.new(Model.Type, Model.type(index: {type, txi})),
IncrementTypeCountMutation.new(type),
WriteMutation.new(Model.Time, Model.time(index: {mb_time, txi})),
WriteFieldsMutation.new(type, tx, block_index, txi)
| tx_mutations(tx_context)
Expand Down

0 comments on commit 1b5f77e

Please sign in to comment.