perf(sub-block): avoid double encoding during signature hashing
Avoid double encoding during signature hashing
@0x00101010 is attempting to deploy a commit to the Tempo Team on Vercel.
A member of the Team first needs to authorize it.
ah good catch, proper fix here would be to define
fn lengthasself.rlp_header().length_with_payload()
Thought about this, personally prefer current solution:
- other parts of the codebase uses the same / similar pattern as current PR
- comparing vec reallocation vs pre-computation even using the
self.rlp_header().length_with_payload()function, still think vec reallocation will be faster?length_with_payloadwill go through all txs and calculate length one by one, which is slower than a few purememcpy? - simpler to read
we should definitely override length fn
self.encode will still compute the length for the header so I guess we could just do something like
let header = self.rlp_header();
let mut buf = Vec::with_capacity(header.length_with_payload() + 1);
buf.put_u8(SUBBLOCK_SIGNATURE_HASH_MAGIC_BYTE);
header.encode(&mut buf);
self.rlp_encode_fields(&mut buf);
we should definitely override
lengthfn
self.encodewill still compute the length for the header so I guess we could just do something likelet header = self.rlp_header(); let mut buf = Vec::with_capacity(header.length_with_payload() + 1); buf.put_u8(SUBBLOCK_SIGNATURE_HASH_MAGIC_BYTE); header.encode(&mut buf); self.rlp_encode_fields(&mut buf);
The only difference between above and the proposed solution is the vec reallocation, which I think is totally negligible. However, with proposed solution we gain better readability and easier future maintainence (directly use the defined self.encode function)
let mut buf = vec![SUBBLOCK_SIGNATURE_HASH_MAGIC_BYTE];
self.encode(&mut buf);
let me know if you agree.
P.S.
Added length() override function for SubBlock.