packjpg / packJPG

A compression program for further compressing JPEG image files
http://packjpg.encode.ru/
GNU Lesser General Public License v3.0
165 stars 26 forks source link

Bitops and Aricoder Performance Enhancements, Visual Studio Compilation Fix #6

Closed TarVanimelde closed 7 years ago

TarVanimelde commented 7 years ago

Total program runtime is reduced between 10--30% through performance-oriented changes to bitops/aricoder and the dependent code in packjpg. In addition, verifiable correctness is improved where it is possible to do so in a performance-neutral way.


Bitops Highlights:

  1. Memory reallocation is changed from additive to multiplicative (e.g., when bytewriter exceeds its current internal capacity, instead of adding a flat amount of memory, the capacity is now doubled).
  2. Implementation and use of read_byte/write_byte for single-byte IO.
  3. Use of setvbuf for a larger file io buffer (the default seems to be 512 bytes, at least for MSVC++).
  4. Use of memset/memcopy (through std::fill and std::copy) where possible instead of simple for loops.

Some public fields in the bitops classes were made private and given simple getter methods to improve the encapsulation of bitops. Some basic bounds checks are also added to a few of the bitops classes to improve correctness on invalid inputs (e.g., requesting a negative number of bytes from a reader).


Aricoder Highlights:

  1. Removed dead code paths (e.g., non-above symbol paths in exclude_symbols and table.lesser).
  2. Better memory allocation (ref bitops).
  3. Cache locality exploited in decode/encode (local copies of fields are used to improve performance).
  4. Use of memset/memcopy (through std::fill and std::copy) where possible.
  5. Use of template functions (e.g., he various shift_model functions are rolled into a single function using a variadic template).

Some functions in model_s and model_b were moved to table_s and table, respectively. Since tables are always rescaled by rightshifting by 1 in packjpg (and all pack* in general, I believe), I changed the rescale_table signature to () from (int scale_factor) to reflect this.


Visual Studio Compilation Fix

iostream::open_stream is changed to accomodate Visual Studio 2015; in particular, the functions for selecting the binary mode for streams are fixed.