From a43718e19183a1c629f1fcea90a3b6870b09d6fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 21 May 2024 09:51:29 +0200 Subject: [PATCH] use git VLQ varint encoding --- analysis/code_analysis.py | 44 ++++++++++++++++++++++------- analysis/top_bytecodes_analysis.csv | 4 +-- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/analysis/code_analysis.py b/analysis/code_analysis.py index 7791702..2448758 100644 --- a/analysis/code_analysis.py +++ b/analysis/code_analysis.py @@ -1,8 +1,6 @@ import csv -import io import json import sys -import leb128 from collections import defaultdict from dataclasses import dataclass, field from pathlib import Path @@ -52,6 +50,36 @@ def test_decode_varint(): assert decode_varint(bytes([0xFF, 0xFF, 0x7f])) == (2113663, 3) +# int encode_varint(uintmax_t value, unsigned char *buf) +# { +# unsigned char varint[16]; +# unsigned pos = sizeof(varint) - 1; +# varint[pos] = value & 127; +# while (value >>= 7) +# varint[--pos] = 128 | (--value & 127); +# if (buf) +# memcpy(buf, varint + pos, sizeof(varint) - pos); +# return sizeof(varint) - pos; +# } + +def encode_varint(value) -> bytes: + varint = bytearray() + varint.append(value & 127) + while value := value >> 7: + value -= 1 + varint.append(128 | (value & 127)) + ret = bytes(reversed(varint)) + return ret + + +def test_encode_varint(): + assert encode_varint(0) == bytes([0]) + assert encode_varint(1) == bytes([1]) + assert encode_varint(127) == bytes([127]) + assert encode_varint(128) == bytes([0x80, 0]) + assert encode_varint(2113663) == bytes([0xFF, 0xFF, 0x7f]) + + @dataclass class Chunk: first_instruction_offset: int @@ -257,20 +285,16 @@ def encode(self, chunks: dict[int, int]) -> tuple[bytes, int]: assert 0 <= value < self.VALUE_MOD delta = i - last_chunk_index e = delta * self.VALUE_MOD + value - ops += leb128.u.encode(e) + ops += encode_varint(e) last_chunk_index = i + 1 return ops, 8 * len(ops) def decode(self, ops: bytes) -> dict[int, int]: - stream = io.BytesIO(ops) - stream.seek(0, 2) - end = stream.tell() - stream.seek(0, 0) - m = {} index = 0 - while stream.tell() != end: - e, _ = leb128.u.decode_reader(stream) + while ops: + e, move = decode_varint(ops) + ops = ops[move:] delta = e // self.VALUE_MOD value = e % self.VALUE_MOD index += delta diff --git a/analysis/top_bytecodes_analysis.csv b/analysis/top_bytecodes_analysis.csv index 2373246..bf428d5 100644 --- a/analysis/top_bytecodes_analysis.csv +++ b/analysis/top_bytecodes_analysis.csv @@ -1,5 +1,5 @@ example address,earliest block,latest block,gas used,code length,code chunks,push bytes,PUSH1 zeros,jumpdests,invalid jumpdests,scheme f11,scheme f10,scheme f9,scheme f8,VLQM33 -total,52029,19759709,179861783901613,10635333,332355,40.42%,1.82%,3.11%,0.07%,12820,12663,12477,11526,11303 +total,52029,19759709,179861783901613,10635333,332355,40.42%,1.82%,3.11%,0.07%,12820,12663,12477,11526,11302 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2,4753408,19759709,14946073640548,3124,98,49.52%,2.21%,2.05%,0.16%,9,8,8,7,7 0x00001bea43608c5ee487f82b773af8bd7cb20a6f,10008355,19759709,13147711796412,11293,353,52.43%,1.30%,2.33%,0.03%,5,4,4,3,3 0x7a250d5630b4cf539739df2c5dacb4c659f2488d,10208463,19759709,11636690698027,21943,686,49.76%,1.59%,2.13%,0.00%,2,2,3,2,2 @@ -669,7 +669,7 @@ total,52029,19759709,179861783901613,10635333,332355,40.42%,1.82%,3.11%,0.07%,12 0x9ebfb53fa8526906738856848a27cb11b0285c3f,15223734,18295281,21043686342,14538,455,39.31%,2.27%,3.52%,0.01%,5,4,4,4,4 0xcca06cd29c61123d9d65b904b18174382380ca64,12297849,19433031,20981375879,6803,213,28.03%,1.93%,3.07%,0.04%,7,7,6,6,5 0xd8de6af55f618a7bc69835d55ddc6582220c36c0,13181841,19755829,20979207171,22142,692,33.21%,1.43%,2.96%,0.04%,17,15,16,14,14 -0x79a8c46dea5ada233abaffd40f3a0a2b1e5a4f27,9567303,19744897,20871789593,21590,675,36.84%,3.15%,3.37%,0.01%,9,8,7,7,7 +0x79a8c46dea5ada233abaffd40f3a0a2b1e5a4f27,9567303,19744897,20871789593,21590,675,36.84%,3.15%,3.37%,0.01%,9,8,7,7,6 0x41e5560054824ea6b0732e656e3ad64e20e94e45,4009734,19759699,20848852723,4160,130,31.01%,1.13%,3.51%,0.10%,7,7,6,5,5 0x84a0856b038eaad1cc7e297cf34a7e72685a8693,12216864,19758870,20755603594,5340,167,36.93%,1.33%,3.45%,0.07%,9,8,8,8,8 0xf8a95b2409c27678a6d18d950c5d913d5c38ab03,12379095,19271682,20721735531,22142,692,33.21%,1.43%,2.96%,0.05%,18,18,18,16,16