Skip to content

Commit

Permalink
add try_with_capacity (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
mo271 authored Dec 23, 2024
1 parent 6889e79 commit fcf5d8e
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 38 deletions.
11 changes: 5 additions & 6 deletions jxl/src/entropy_coding/huffman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use std::fmt::Debug;
use crate::bit_reader::BitReader;
use crate::entropy_coding::decode::*;
use crate::error::{Error, Result};
use crate::util::tracing_wrappers::*;
use crate::util::*;
use crate::util::{tracing_wrappers::*, CeilLog2, NewWithCapacity};

pub const HUFFMAN_MAX_BITS: usize = 15;
const TABLE_BITS: usize = 8;
Expand Down Expand Up @@ -104,7 +103,7 @@ impl Table {
TABLE_SIZE
]),
(2, _) => {
let mut ret = Vec::with_capacity(TABLE_SIZE);
let mut ret = Vec::new_with_capacity(TABLE_SIZE)?;
for _ in 0..(TABLE_SIZE >> 1) {
ret.push(TableEntry {
bits: 1,
Expand All @@ -118,7 +117,7 @@ impl Table {
Ok(ret)
}
(3, _) => {
let mut ret = Vec::with_capacity(TABLE_SIZE);
let mut ret = Vec::new_with_capacity(TABLE_SIZE)?;
for _ in 0..(TABLE_SIZE >> 2) {
ret.push(TableEntry {
bits: 1,
Expand All @@ -140,7 +139,7 @@ impl Table {
Ok(ret)
}
(4, false) => {
let mut ret = Vec::with_capacity(TABLE_SIZE);
let mut ret = Vec::new_with_capacity(TABLE_SIZE)?;
for _ in 0..(TABLE_SIZE >> 2) {
ret.push(TableEntry {
bits: 2,
Expand All @@ -162,7 +161,7 @@ impl Table {
Ok(ret)
}
(4, true) => {
let mut ret = Vec::with_capacity(TABLE_SIZE);
let mut ret = Vec::new_with_capacity(TABLE_SIZE)?;
symbols[2..4].sort_unstable();
for _ in 0..(TABLE_SIZE >> 3) {
ret.push(TableEntry {
Expand Down
2 changes: 1 addition & 1 deletion jxl/src/entropy_coding/hybrid_uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::bit_reader::BitReader;
use crate::error::Error;

use crate::util::*;
use crate::util::CeilLog2;

#[derive(Debug)]
pub struct HybridUint {
Expand Down
5 changes: 2 additions & 3 deletions jxl/src/features/patches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
entropy_coding::decode::Histograms,
error::{Error, Result},
frame::DecoderState,
util::tracing_wrappers::*,
util::{tracing_wrappers::*, NewWithCapacity},
};

// Context numbers as specified in Section C.4.5, Listing C.2:
Expand Down Expand Up @@ -153,8 +153,7 @@ impl PatchesDictionary {
let mut next_size = 1;
let mut positions: Vec<PatchPosition> = Vec::new();
let mut blendings = Vec::new();
let mut ref_positions: Vec<PatchReferencePosition> = Vec::new();
ref_positions.try_reserve(num_ref_patch)?;
let mut ref_positions = Vec::new_with_capacity(num_ref_patch)?;
for _ in 0..num_ref_patch {
let reference =
patches_reader.read(br, PatchContext::ReferenceFrame as usize)? as usize;
Expand Down
6 changes: 3 additions & 3 deletions jxl/src/features/spline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
bit_reader::BitReader,
entropy_coding::decode::{unpack_signed, Histograms, Reader},
error::{Error, Result},
util::{tracing_wrappers::*, CeilLog2},
util::{tracing_wrappers::*, CeilLog2, NewWithCapacity},
};
const MAX_NUM_CONTROL_POINTS: u32 = 1 << 20;
const MAX_NUM_CONTROL_POINTS_PER_PIXEL_RATIO: u32 = 2;
Expand Down Expand Up @@ -192,7 +192,7 @@ impl QuantizedSpline {
max_control_points,
));
}
let mut control_points = Vec::with_capacity(num_control_points as usize);
let mut control_points = Vec::new_with_capacity(num_control_points as usize)?;
for _ in 0..num_control_points {
let x = splines_reader.read_signed(br, CONTROL_POINTS_CONTEXT)? as i64;
let y = splines_reader.read_signed(br, CONTROL_POINTS_CONTEXT)? as i64;
Expand Down Expand Up @@ -237,7 +237,7 @@ impl QuantizedSpline {
let area_limit = area_limit(image_size);

let mut result = Spline {
control_points: Vec::with_capacity(self.control_points.len() + 1),
control_points: Vec::new_with_capacity(self.control_points.len() + 1)?,
..Default::default()
};

Expand Down
8 changes: 3 additions & 5 deletions jxl/src/frame/modular/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
bit_reader::BitReader,
entropy_coding::decode::Histograms,
error::{Error, Result},
util::tracing_wrappers::*,
util::{tracing_wrappers::*, NewWithCapacity},
};

#[allow(dead_code)]
Expand Down Expand Up @@ -115,11 +115,9 @@ impl Tree {
tree_reader.check_final_state()?;

let num_properties = max_property as usize + 1;
let mut property_ranges = vec![];
property_ranges.try_reserve(num_properties * tree.len())?;
let mut property_ranges = Vec::new_with_capacity(num_properties * tree.len())?;
property_ranges.resize(num_properties * tree.len(), (i32::MIN, i32::MAX));
let mut height = vec![];
height.try_reserve(tree.len())?;
let mut height = Vec::new_with_capacity(tree.len())?;
height.resize(tree.len(), 0);
for i in 0..tree.len() {
const HEIGHT_LIMIT: usize = 2048;
Expand Down
16 changes: 6 additions & 10 deletions jxl/src/headers/permutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::bit_reader::BitReader;
use crate::entropy_coding::decode::Reader;
use crate::error::{Error, Result};
use crate::util::{tracing_wrappers::instrument, value_of_lowest_1_bit, CeilLog2};
use crate::util::{tracing_wrappers::instrument, value_of_lowest_1_bit, CeilLog2, NewWithCapacity};

#[derive(Debug, PartialEq, Default)]
pub struct Permutation(pub Vec<u32>);
Expand Down Expand Up @@ -41,8 +41,7 @@ impl Permutation {
return Err(Error::InvalidPermutationSize { size, skip, end });
}

let mut lehmer = Vec::new();
lehmer.try_reserve(end as usize)?;
let mut lehmer = Vec::new_with_capacity(end as usize)?;

let mut prev_val = 0u32;
for idx in skip..(skip + end) {
Expand All @@ -59,8 +58,7 @@ impl Permutation {
}

// Initialize the full permutation vector with skipped elements intact
let mut permutation: Vec<u32> = Vec::new();
permutation.try_reserve((size - skip) as usize)?;
let mut permutation = Vec::new_with_capacity((size - skip) as usize)?;
permutation.extend(0..size);

// Decode the Lehmer code into the slice starting at `skip`
Expand Down Expand Up @@ -88,15 +86,13 @@ fn decode_lehmer_code(code: &[u32], permutation_slice: &[u32]) -> Result<Vec<u32
});
}

let mut permuted = vec![];
permuted.try_reserve(n)?;
let mut permuted = Vec::new_with_capacity(n)?;
permuted.extend_from_slice(permutation_slice);

let padded_n = (n as u32).next_power_of_two() as usize;

// Allocate temp array inside the function
let mut temp = vec![];
temp.try_reserve(padded_n)?;
let mut temp = Vec::new_with_capacity(padded_n)?;
temp.extend((0..padded_n as u32).map(|x| value_of_lowest_1_bit(x + 1)));

for (i, permuted_item) in permuted.iter_mut().enumerate() {
Expand Down Expand Up @@ -167,7 +163,7 @@ fn decode_lehmer_code_naive(code: &[u32], permutation_slice: &[u32]) -> Result<V

// Create temp array with values from permutation_slice
let mut temp = permutation_slice.to_vec();
let mut permuted = Vec::with_capacity(n);
let mut permuted = Vec::new_with_capacity(n)?;

// Iterate over the Lehmer code
for (i, &idx) in code.iter().enumerate() {
Expand Down
5 changes: 2 additions & 3 deletions jxl/src/icc/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

use crate::error::Result;
use crate::{error::Result, util::NewWithCapacity};

use super::{IccStream, ICC_HEADER_SIZE};

Expand Down Expand Up @@ -40,8 +40,7 @@ pub(super) fn read_header(data_stream: &mut IccStream, output_size: u64) -> Resu
let header_size = output_size.min(ICC_HEADER_SIZE);
let header_data = data_stream.read_to_vec_exact(header_size as usize)?;

let mut profile = Vec::new();
profile.try_reserve(output_size as usize)?;
let mut profile = Vec::new_with_capacity(output_size as usize)?;

for (idx, &e) in header_data.iter().enumerate() {
let p = predict_header(idx, output_size as u32, &header_data);
Expand Down
4 changes: 2 additions & 2 deletions jxl/src/icc/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::bit_reader::*;
use crate::entropy_coding::decode::{Histograms, Reader};
use crate::error::{Error, Result};
use crate::util::tracing_wrappers::{instrument, warn};
use crate::util::NewWithCapacity;

fn read_varint(mut read_one: impl FnMut() -> Result<u8>) -> Result<u64> {
let mut value = 0u64;
Expand Down Expand Up @@ -129,8 +130,7 @@ impl<'br, 'buf, 'hist> IccStream<'br, 'buf, 'hist> {
return Err(Error::IccEndOfStream);
}

let mut out = Vec::new();
out.try_reserve(len)?;
let mut out = Vec::new_with_capacity(len)?;

for _ in 0..len {
out.push(self.read_one()?);
Expand Down
7 changes: 3 additions & 4 deletions jxl/src/icc/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};

use crate::error::{Error, Result};
use crate::util::tracing_wrappers::warn;
use crate::util::NewWithCapacity;

use super::{read_varint_from_reader, IccStream, ICC_HEADER_SIZE};

Expand Down Expand Up @@ -96,8 +97,7 @@ pub(super) fn read_tag_list(

fn shuffle_w2(bytes: &[u8]) -> Result<Vec<u8>> {
let len = bytes.len();
let mut out = Vec::new();
out.try_reserve(len)?;
let mut out = Vec::new_with_capacity(len)?;

let height = len / 2;
let odd = len % 2;
Expand All @@ -113,8 +113,7 @@ fn shuffle_w2(bytes: &[u8]) -> Result<Vec<u8>> {

fn shuffle_w4(bytes: &[u8]) -> Result<Vec<u8>> {
let len = bytes.len();
let mut out = Vec::new();
out.try_reserve(len)?;
let mut out = Vec::new_with_capacity(len)?;

let step = len / 4;
let wide_count = len % 4;
Expand Down
3 changes: 2 additions & 1 deletion jxl/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
pub mod test;

mod bits;
#[allow(unused)]
mod concat_slice;
mod log2;
mod shift_right_ceil;
pub mod tracing_wrappers;
mod vec_helpers;

pub use bits::*;
#[allow(unused)]
pub use concat_slice::*;
pub use log2::*;
pub use shift_right_ceil::*;
pub use vec_helpers::*;
33 changes: 33 additions & 0 deletions jxl/src/util/vec_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// TODO(firsching): as soon as "Vec::try_with_capacity" is available from the
// standard library use this instead of the functions here.
pub trait NewWithCapacity {
type Output;
type Error;
fn new_with_capacity(capacity: usize) -> Result<Self::Output, Self::Error>;
}

impl<T> NewWithCapacity for Vec<T> {
type Output = Vec<T>;
type Error = std::collections::TryReserveError;

fn new_with_capacity(capacity: usize) -> Result<Self::Output, Self::Error> {
let mut vec = Vec::new();
vec.try_reserve(capacity)?;
Ok(vec)
}
}

impl NewWithCapacity for String {
type Output = String;
type Error = std::collections::TryReserveError;
fn new_with_capacity(capacity: usize) -> Result<Self::Output, Self::Error> {
let mut s = String::new();
s.try_reserve(capacity)?;
Ok(s)
}
}

0 comments on commit fcf5d8e

Please sign in to comment.