mana-ethereum / ex_rlp

Elixir implementation of Ethereum's RLP (Recursive Length Prefix) encoding
MIT License
31 stars 9 forks source link

Improvement - using guard on decode item #34

Closed prihandi closed 2 years ago

prihandi commented 2 years ago

Recently, I want to use ExRLP for my projects. Then I see through the source code on your repository.

I found that DecodeItem module have room for improvements, and also I see your previous PR to update test cases for invalid RLP from the ethereum submodule.

This PR is my attempts to refactor the module and update the test cases. I also update Erlang and Elixir versions to the latest and remove Poison, since we already have Jason on our dependency list mix.lock.

Kindly check and looking forward for feedbacks

Regards, Dwi

prihandi commented 2 years ago

Hi @ayrat555 , Will you looking into this PR ? I try to use this library on my projects. If your're not have any interest with this, I'll use my own repo then.

Thanks, Dwi

ayrat555 commented 2 years ago

@prihandi hey.

in the first version of ex_rlp, we had exactly the same guards.

I think the current code works faster, can you please benchmark your changes and the current changes to verify this?

prihandi commented 2 years ago

Yup. nice reason. I was intrigued by test running time which has the guard version a bit faster, turns out it need more time to compile longer source code for the current version.

Anyway this is the benchmark result, I took the inputs from the json test file.

Operating System: Linux
CPU Information: AMD Ryzen 7 5700U with Radeon Graphics
Number of Available Cores: 16
Available memory: 14.98 GB
Elixir 1.13.1
Erlang 24.2

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
parallel: 1
inputs: big_int, dict, long_list, long_string
Estimated total run time: 56 s

Benchmarking decode item with guard with input big_int...
Benchmarking decode item with guard with input dict...
Benchmarking decode item with guard with input long_list...
Benchmarking decode item with guard with input long_string...
Benchmarking hard-coded decode item with input big_int...
Benchmarking hard-coded decode item with input dict...
Benchmarking hard-coded decode item with input long_list...
Benchmarking hard-coded decode item with input long_string...

##### With input big_int #####
Name                             ips        average  deviation         median         99th %
hard-coded decode item        5.64 M      177.38 ns ±17946.84%         100 ns         391 ns
decode item with guard        5.39 M      185.51 ns ±13846.72%         130 ns         461 ns

Comparison: 
hard-coded decode item        5.64 M
decode item with guard        5.39 M - 1.05x slower +8.13 ns

##### With input dict #####
Name                             ips        average  deviation         median         99th %
hard-coded decode item        1.84 M      544.19 ns  ±5460.12%         391 ns        1182 ns
decode item with guard        1.10 M      905.87 ns  ±3045.66%         702 ns        2325 ns

Comparison: 
hard-coded decode item        1.84 M
decode item with guard        1.10 M - 1.66x slower +361.68 ns

##### With input long_list #####
Name                             ips        average  deviation         median         99th %
hard-coded decode item      279.07 K        3.58 μs   ±434.83%        2.83 μs        8.14 μs
decode item with guard      159.78 K        6.26 μs   ±266.86%        5.32 μs       20.89 μs

Comparison: 
hard-coded decode item      279.07 K
decode item with guard      159.78 K - 1.75x slower +2.68 μs

##### With input long_string #####
Name                             ips        average  deviation         median         99th %
hard-coded decode item        5.94 M      168.25 ns ±15308.26%         130 ns         492 ns
decode item with guard        5.86 M      170.74 ns ±15764.26%         130 ns         491 ns

Comparison: 
hard-coded decode item        5.94 M
decode item with guard        5.86 M - 1.01x slower +2.50 ns

I'll close the PR

Thanks @ayrat555