Skip to content

Commit

Permalink
fn decode_sb: Slightly optimize var pc (#383)
Browse files Browse the repository at this point in the history
More than just a slight optimization, this also more closely aligns to
how the C was written while also being safe and zero overhead. In C,
`pc` is left uninitialized and unused when `t.frame_thread.pass == 2`.
The easiest way to make this safe in Rust was to default/zero initialize
in this case and use `pc` normally in the rest of the function, but that
has a slight overhead of initializing a `[0u16; 16]`. This changes it to
use an `Option`, more similar to the C, but to avoid `.unwrap()`s, it
uses `if let Some(pc) = pc` instead of `if t.frame_thread.pass != 2` in
the rest of the function.
  • Loading branch information
kkysen authored Aug 24, 2023
2 parents f9f7adb + e2a3e9c commit 7591504
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4195,12 +4195,13 @@ unsafe fn decode_sb(
return decode_sb(t, bl + 1, (*(node as *const EdgeBranch)).split[0]);
}

let mut pc = &mut Default::default();
let mut bp;
let mut ctx = 0;
let mut bx8 = 0;
let mut by8 = 0;
if t.frame_thread.pass != 2 {
let pc = if t.frame_thread.pass == 2 {
None
} else {
if bl == BL_64X64 {
println!(
"poc={},y={},x={},bl={},r={}",
Expand All @@ -4214,18 +4215,11 @@ unsafe fn decode_sb(
bx8 = (t.bx & 31) >> 1;
by8 = (t.by & 31) >> 1;
ctx = get_partition_ctx(&*t.a, &t.l, bl, by8, bx8);
pc = &mut ts.cdf.m.partition[bl as usize][ctx as usize];
}
Some(&mut ts.cdf.m.partition[bl as usize][ctx as usize])
};

if have_h_split && have_v_split {
if t.frame_thread.pass == 2 {
let b = &mut *(f.frame_thread.b).offset(t.by as isize * f.b4_stride + t.bx as isize);
bp = if b.bl as BlockLevel == bl {
b.bp as BlockPartition
} else {
PARTITION_SPLIT
};
} else {
if let Some(pc) = pc {
bp = dav1d_msac_decode_symbol_adapt16(
&mut ts.msac,
pc,
Expand All @@ -4251,6 +4245,13 @@ unsafe fn decode_sb(
ts.msac.rng,
);
}
} else {
let b = &mut *(f.frame_thread.b).offset(t.by as isize * f.b4_stride + t.bx as isize);
bp = if b.bl as BlockLevel == bl {
b.bp as BlockPartition
} else {
PARTITION_SPLIT
};
}
let b = &dav1d_block_sizes[bl as usize][bp as usize];

Expand Down Expand Up @@ -4531,10 +4532,7 @@ unsafe fn decode_sb(
}
} else if have_h_split {
let mut is_split;
if t.frame_thread.pass == 2 {
let b = &mut *(f.frame_thread.b).offset(t.by as isize * f.b4_stride + t.bx as isize);
is_split = b.bl as BlockLevel != bl;
} else {
if let Some(pc) = pc {
is_split = dav1d_msac_decode_bool(&mut ts.msac, gather_top_partition_prob(pc, bl));
if DEBUG_BLOCK_INFO(f, t) {
println!(
Expand All @@ -4552,6 +4550,9 @@ unsafe fn decode_sb(
ts.msac.rng,
);
}
} else {
let b = &mut *(f.frame_thread.b).offset(t.by as isize * f.b4_stride + t.bx as isize);
is_split = b.bl as BlockLevel != bl;
}

assert!(bl < BL_8X8);
Expand Down Expand Up @@ -4582,10 +4583,7 @@ unsafe fn decode_sb(
} else {
assert!(have_v_split);
let mut is_split;
if t.frame_thread.pass == 2 {
let b = &mut *(f.frame_thread.b).offset(t.by as isize * f.b4_stride + t.bx as isize);
is_split = b.bl as BlockLevel != bl;
} else {
if let Some(pc) = pc {
is_split = dav1d_msac_decode_bool(&mut ts.msac, gather_left_partition_prob(pc, bl));
if f.cur.p.layout == DAV1D_PIXEL_LAYOUT_I422 && !is_split {
return 1;
Expand All @@ -4606,6 +4604,9 @@ unsafe fn decode_sb(
ts.msac.rng,
);
}
} else {
let b = &mut *(f.frame_thread.b).offset(t.by as isize * f.b4_stride + t.bx as isize);
is_split = b.bl as BlockLevel != bl;
}

assert!(bl < BL_8X8);
Expand Down

0 comments on commit 7591504

Please sign in to comment.