Dropped the q-bit (previous-perturb) from cksum tags

Now that we perturb commit cksums with the odd-parity zero, the q-bit no
longer serves a purpose other than extra debug info. But this is a
double-edged sword, because redundant info just means another thing that
can go wrong.

For example, should we assert? If the q-bit doesn't reflect the
previous-perturb state it's a bug, but the only thing that would break
would be the q-bit itself. And if we don't assert what's the point of
keeping the q-bit around?

Dropping the q-bit avoids answering this question and saves a bit of
code:

           code          stack          ctx
  before: 37772           2608          620
  after:  37768 (-0.0%)   2608 (+0.0%)  620 (+0.0%)
This commit is contained in:
Christopher Haster
2025-01-10 15:55:48 -06:00
parent d08d254cd2
commit b6ab323eb1
7 changed files with 22 additions and 45 deletions
+1 -11
View File
@@ -1157,7 +1157,6 @@ enum lfsr_tag {
// checksum tags
LFSR_TAG_CKSUM = 0x3000,
LFSR_TAG_P = 0x0001,
LFSR_TAG_Q = 0x0002,
LFSR_TAG_NOTE = 0x3100,
LFSR_TAG_ECKSUM = 0x3200,
@@ -1229,10 +1228,6 @@ static inline bool lfsr_tag_p(lfsr_tag_t tag) {
return tag & LFSR_TAG_P;
}
static inline bool lfsr_tag_q(lfsr_tag_t tag) {
return tag & LFSR_TAG_Q;
}
static inline bool lfsr_tag_isinternal(lfsr_tag_t tag) {
return tag & LFSR_TAG_INTERNAL;
}
@@ -2821,9 +2816,6 @@ static int lfsr_rbyd_fetch(lfs_t *lfs, lfsr_rbyd_t *rbyd,
break;
}
// if checksums match, perturb bits should also match
LFS_ASSERT(lfsr_tag_q(tag) == lfsr_rbyd_isperturb(rbyd));
// save what we've found so far
rbyd->eoff
= ((lfs_size_t)lfsr_tag_p(tag)
@@ -4046,8 +4038,6 @@ static int lfsr_rbyd_appendcksum(lfs_t *lfs, lfsr_rbyd_t *rbyd) {
// set the valid bit to the cksum parity
| ((uint8_t)v << 7);
cksum_buf[1] = (uint8_t)(LFSR_TAG_CKSUM >> 0)
// include the current perturb bit
| ((uint8_t)lfsr_rbyd_isperturb(rbyd) << 1)
// set the perturb bit so next commit is invalid
| ((uint8_t)perturb << 0);
cksum_buf[2] = 0;
@@ -4063,7 +4053,7 @@ static int lfsr_rbyd_appendcksum(lfs_t *lfs, lfsr_rbyd_t *rbyd) {
// calculate the commit checksum
cksum_ = lfs_crc32c(cksum_, cksum_buf, 2+1+4);
// and perturb, perturbing the commit checksum avoids a perturb hole
// after the last valid bit without needing to manually validate q
// after the last valid bit
//
// note the odd-parity zero preserves our position in the crc32c
// ring while only changing the parity
+1 -2
View File
@@ -50,9 +50,8 @@ TAG_B = 0x0000
TAG_R = 0x2000
TAG_LE = 0x0000
TAG_GT = 0x1000
TAG_CKSUM = 0x3000 ## 0x3c0p v-11 cccc ---- --qp
TAG_CKSUM = 0x3000 ## 0x3c0p v-11 cccc ---- ---p
TAG_P = 0x0001
TAG_Q = 0x0002
TAG_NOTE = 0x3100 # 0x3100 v-11 ---1 ---- ----
TAG_ECKSUM = 0x3200 # 0x3200 v-11 --1- ---- ----
+4 -6
View File
@@ -48,9 +48,8 @@ TAG_B = 0x0000
TAG_R = 0x2000
TAG_LE = 0x0000
TAG_GT = 0x1000
TAG_CKSUM = 0x3000 ## 0x3c0p v-11 cccc ---- --qp
TAG_CKSUM = 0x3000 ## 0x3c0p v-11 cccc ---- ---p
TAG_P = 0x0001
TAG_Q = 0x0002
TAG_NOTE = 0x3100 # 0x3100 v-11 ---1 ---- ----
TAG_ECKSUM = 0x3200 # 0x3200 v-11 --1- ---- ----
@@ -239,10 +238,9 @@ def tagrepr(tag, w=None, size=None, off=None):
else ' -%d' % size if size
else '')
elif (tag & 0x7f00) == TAG_CKSUM:
return 'cksum%s%s%s%s%s' % (
'q' if not tag & 0xfc and tag & TAG_Q else '',
'p' if not tag & 0xfc and tag & TAG_P else '',
' 0x%02x' % (tag & 0xff) if tag & 0xfc else '',
return 'cksum%s%s%s%s' % (
'p' if not tag & 0xfe and tag & TAG_P else '',
' 0x%02x' % (tag & 0xff) if tag & 0xfe else '',
' w%d' % w if w else '',
' %s' % size if size is not None else '')
elif (tag & 0x7f00) == TAG_NOTE:
+4 -6
View File
@@ -49,9 +49,8 @@ TAG_B = 0x0000
TAG_R = 0x2000
TAG_LE = 0x0000
TAG_GT = 0x1000
TAG_CKSUM = 0x3000 ## 0x3c0p v-11 cccc ---- --qp
TAG_CKSUM = 0x3000 ## 0x3c0p v-11 cccc ---- ---p
TAG_P = 0x0001
TAG_Q = 0x0002
TAG_NOTE = 0x3100 # 0x3100 v-11 ---1 ---- ----
TAG_ECKSUM = 0x3200 # 0x3200 v-11 --1- ---- ----
@@ -270,10 +269,9 @@ def tagrepr(tag, w=None, size=None, off=None):
else ' -%d' % size if size
else '')
elif (tag & 0x7f00) == TAG_CKSUM:
return 'cksum%s%s%s%s%s' % (
'q' if not tag & 0xfc and tag & TAG_Q else '',
'p' if not tag & 0xfc and tag & TAG_P else '',
' 0x%02x' % (tag & 0xff) if tag & 0xfc else '',
return 'cksum%s%s%s%s' % (
'p' if not tag & 0xfe and tag & TAG_P else '',
' 0x%02x' % (tag & 0xff) if tag & 0xfe else '',
' w%d' % w if w else '',
' %s' % size if size is not None else '')
elif (tag & 0x7f00) == TAG_NOTE:
+4 -6
View File
@@ -48,9 +48,8 @@ TAG_B = 0x0000
TAG_R = 0x2000
TAG_LE = 0x0000
TAG_GT = 0x1000
TAG_CKSUM = 0x3000 ## 0x3c0p v-11 cccc ---- --qp
TAG_CKSUM = 0x3000 ## 0x3c0p v-11 cccc ---- ---p
TAG_P = 0x0001
TAG_Q = 0x0002
TAG_NOTE = 0x3100 # 0x3100 v-11 ---1 ---- ----
TAG_ECKSUM = 0x3200 # 0x3200 v-11 --1- ---- ----
@@ -254,10 +253,9 @@ def tagrepr(tag, w=None, size=None, off=None):
else ' -%d' % size if size
else '')
elif (tag & 0x7f00) == TAG_CKSUM:
return 'cksum%s%s%s%s%s' % (
'q' if not tag & 0xfc and tag & TAG_Q else '',
'p' if not tag & 0xfc and tag & TAG_P else '',
' 0x%02x' % (tag & 0xff) if tag & 0xfc else '',
return 'cksum%s%s%s%s' % (
'p' if not tag & 0xfe and tag & TAG_P else '',
' 0x%02x' % (tag & 0xff) if tag & 0xfe else '',
' w%d' % w if w else '',
' %s' % size if size is not None else '')
elif (tag & 0x7f00) == TAG_NOTE:
+4 -8
View File
@@ -58,9 +58,8 @@ TAG_B = 0x0000
TAG_R = 0x2000
TAG_LE = 0x0000
TAG_GT = 0x1000
TAG_CKSUM = 0x3000 ## 0x3c0p v-11 cccc ---- --qp
TAG_CKSUM = 0x3000 ## 0x3c0p v-11 cccc ---- ---p
TAG_P = 0x0001
TAG_Q = 0x0002
TAG_NOTE = 0x3100 # 0x3100 v-11 ---1 ---- ----
TAG_ECKSUM = 0x3200 # 0x3200 v-11 --1- ---- ----
@@ -242,10 +241,9 @@ def tagrepr(tag, w=None, size=None, off=None):
else ' -%d' % size if size
else '')
elif (tag & 0x7f00) == TAG_CKSUM:
return 'cksum%s%s%s%s%s' % (
'q' if not tag & 0xfc and tag & TAG_Q else '',
'p' if not tag & 0xfc and tag & TAG_P else '',
' 0x%02x' % (tag & 0xff) if tag & 0xfc else '',
return 'cksum%s%s%s%s' % (
'p' if not tag & 0xfe and tag & TAG_P else '',
' 0x%02x' % (tag & 0xff) if tag & 0xfe else '',
' w%d' % w if w else '',
' %s' % size if size is not None else '')
elif (tag & 0x7f00) == TAG_NOTE:
@@ -557,8 +555,6 @@ def dbg_log(data, block_size, rev, eoff, weight, *,
cksum__ = fromle32(data[j_:j_+4])
if cksum_ != cksum__:
notes.append('cksum!=%08x' % cksum__)
if perturb != bool(tag & TAG_Q):
notes.append('q!=%x' % perturb)
# update perturb bit
perturb = tag & TAG_P
# revert to data cksum and perturb
+4 -6
View File
@@ -46,9 +46,8 @@ TAG_B = 0x0000
TAG_R = 0x2000
TAG_LE = 0x0000
TAG_GT = 0x1000
TAG_CKSUM = 0x3000 ## 0x3c0p v-11 cccc ---- --qp
TAG_CKSUM = 0x3000 ## 0x3c0p v-11 cccc ---- ---p
TAG_P = 0x0001
TAG_Q = 0x0002
TAG_NOTE = 0x3100 # 0x3100 v-11 ---1 ---- ----
TAG_ECKSUM = 0x3200 # 0x3200 v-11 --1- ---- ----
@@ -196,10 +195,9 @@ def tagrepr(tag, w=None, size=None, off=None):
else ' -%d' % size if size
else '')
elif (tag & 0x7f00) == TAG_CKSUM:
return 'cksum%s%s%s%s%s' % (
'q' if not tag & 0xfc and tag & TAG_Q else '',
'p' if not tag & 0xfc and tag & TAG_P else '',
' 0x%02x' % (tag & 0xff) if tag & 0xfc else '',
return 'cksum%s%s%s%s' % (
'p' if not tag & 0xfe and tag & TAG_P else '',
' 0x%02x' % (tag & 0xff) if tag & 0xfe else '',
' w%d' % w if w else '',
' %s' % size if size is not None else '')
elif (tag & 0x7f00) == TAG_NOTE: