Skip to content

Commit

Permalink
Work around rayon-rs/rayon#1064
Browse files Browse the repository at this point in the history
  • Loading branch information
Pr0methean committed Jun 25, 2023
1 parent 9a0fb4c commit 1b82d85
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion main/Cargo.toml
Expand Up @@ -21,7 +21,7 @@ include_dir = "0.7.3"
tikv-jemallocator = "0.5.0"
zip_next = {version = "0.10.3", default-features = false, features = ["deflate-zlib-ng", "deflate-zopfli"]}
zopfli = {git = "https://github.com/Pr0methean/zopfli.git", branch = "pr21+pr22", default-features = false, features = ["std", "zlib"] }
oxipng = {git = "https://github.com/Pr0methean/oxipng.git", branch = "pr522+pr524", default-features = false, features = ["zopfli", "parallel"]}
oxipng = {git = "https://github.com/Pr0methean/oxipng.git", branch = "test-zopfli-pr21+pr22", default-features = false, features = ["zopfli", "parallel"]}
bitstream-io = "1.6.0"
num_cpus = "1.15.0"
palette = "0.7.2"
Expand Down
17 changes: 12 additions & 5 deletions main/src/image_tasks/png_output.rs
Expand Up @@ -4,7 +4,7 @@ use include_dir::File;
use std::io::{Cursor, Write};
use std::mem::transmute;
use std::num::NonZeroU64;
use std::ops::DerefMut;
use std::ops::{DerefMut};
use std::sync::{Mutex};
use bitstream_io::{BigEndian, BitWrite, BitWriter};
use bytemuck::cast;
Expand All @@ -21,14 +21,17 @@ use zip_next::ZipWriter;

use crate::image_tasks::MaybeFromPool;
use crate::image_tasks::task_spec::channel_to_bit_depth;
use crate::{GRID_SIZE, TILE_SIZE, ZOPFLI_ITERS};
use crate::{anyhoo, GRID_SIZE, TILE_SIZE, ZOPFLI_ITERS};
use crate::image_tasks::cloneable::CloneableError;
use crate::image_tasks::color::ComparableColor;

pub type ZipBufferRaw = Cursor<Vec<u8>>;

const PNG_BUFFER_SIZE: usize = 1024 * 1024;

thread_local! {
static OXIPNG_MUTEX: Mutex<()> = Default::default();
}
static ZOPFLI_OPTIONS: Lazy<zopfli::Options> = Lazy::new(|| zopfli::Options {
iteration_count: None,
iterations_without_improvement: NonZeroU64::new(*ZOPFLI_ITERS),
Expand Down Expand Up @@ -177,9 +180,13 @@ pub fn png_output(image: MaybeFromPool<Pixmap>, color_type: ColorType,
bit_writer.into_writer().into_inner()
}
};
info!("Starting PNG optimization for {}", file_path);
let png = RawImage::new(width, height, color_type, bit_depth, raw_bytes)?
.create_optimized_png(&OXIPNG_OPTIONS, &*ZOPFLI_DEFLATER)?;
let raw_image = RawImage::new(width, height, color_type, bit_depth, raw_bytes)?;
// Work around https://github.com/rayon-rs/rayon/issues/1064
let png = OXIPNG_MUTEX.with(|mutex| -> Result<_, CloneableError> {
let _lock = mutex.lock().map_err(|e| anyhoo!(e.to_string()))?;
info!("Starting PNG optimization for {}", file_path);
Ok(raw_image.create_optimized_png(&OXIPNG_OPTIONS, &*ZOPFLI_DEFLATER)?)
})?;
info!("Finished PNG optimization for {}", file_path);
let zip = &*ZIP;
let mut writer = zip.lock()?;
Expand Down
4 changes: 3 additions & 1 deletion main/src/main.rs
Expand Up @@ -24,14 +24,16 @@ use std::fs::create_dir_all;
use std::hint::unreachable_unchecked;
use std::ops::DerefMut;
use include_dir::{Dir, DirEntry};
use once_cell::sync::Lazy;
use rayon::{scope_fifo, ThreadPoolBuilder};
use tikv_jemallocator::Jemalloc;
use image_tasks::cloneable::{CloneableError};
use crate::image_tasks::png_output::{copy_in_to_out, ZIP};
use crate::image_tasks::prewarm_pixmap_pool;
use crate::image_tasks::repaint::prewarm_mask_pool;

#[cfg(not(any(test,clippy)))]
use once_cell::sync::Lazy;

const GRID_SIZE: u32 = 32;

#[cfg(not(any(test,clippy)))]
Expand Down

0 comments on commit 1b82d85

Please sign in to comment.