Docsity

Fighting Finalizers - Program and Data Representation | CS 2150, Study notes of Computer Science

Toggle sidebar
bg1
1
CS216: Program and Data Represe ntation
University of Virginia Computer Science
Spring 2006 David Evans
Lecture 15:
Compression
http://www.cs.virginia.edu/cs216 2
UVa CS216 Spring 2006 -Le cture 15: Numbers
Menu
• Garbage Collection Puzzle
• Encoding: Huffman, LZW, GIF
• Representing Numbers
3
UVa CS216 Spring 2006 -Le cture 15: Numbers
Fighting Finalizers
• finalize() – method in java.lang.Object
– Class can override
– It is called when GC determines the
object is garbage (before collecting it)
Problem due to Paul Tyma, Google
ab
x
x = NULL;
a.finalize ();
b.finalize ();
GC
4
UVa CS216 Spring 2006 -Le cture 15: Numbers
Finality?
ab
x
class A {
B p;
}
class B {
A r;
void finalize() {
r.p = this;
}
a = new A();
a.p = new B();
a.p.r = a;
x = a;
a.p = NULL;
GC – b.finalize ()
a.p.toString () Problem due to Paul Tyma, Google
5
UVa CS216 Spring 2006 -Le cture 15: Numbers
Encoding
• Huffman Encoding
– We proved there is no better encoding
that is:
• Prefix encoding (can divide coded message
into symbols without looking ahead)
• One-to-one mapping (Symbol→Bits)
• Fixed mapping
• Can we do better without these
constraints?
6
UVa CS216 Spring 2006 -Le cture 15: Numbers
Lempel-Ziv-Wench (LZW)
• Terry Wench refined the L-Z scheme
• Fixed-length (typically 12-bits)
codewords
• Dictionary maps each codeword to
text
• Greedy scheme for building
dictionary
pf3

Partial preview of the text

Download Fighting Finalizers - Program and Data Representation | CS 2150 and more Study notes Computer Science in PDF only on Docsity!

CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans

Lecture 15:

Compression

http://www.cs.virginia.edu/cs216 UVa CS216 Spring 2006 - Lecture 15: Numbers 2

Menu

  • Garbage Collection Puzzle
  • Encoding: Huffman, LZW, GIF
  • Representing Numbers

UVa CS216 Spring 2006 - Lecture 15: Numbers 3

Fighting Finalizers

  • finalize() – method in java.lang.Object
    • Class can override
    • It is called when GC determines the object is garbage (before collecting it)

Problem due to Paul Tyma, Google

x a (^) b x = NULL; a.finalize (); b.finalize ();

GC

UVa CS216 Spring 2006 - Lecture 15: Numbers 4

Finality?

x a (^) b

class A { B p; }

class B { A r; void finalize() { r.p = this; }

a = new A(); a.p = new B(); a.p.r = a; x = a; a.p = NULL; GC – b.finalize () a.p.toString () Problem due to Paul Tyma, Google

UVa CS216 Spring 2006 - Lecture 15: Numbers 5

Encoding

  • Huffman Encoding
    • We proved there is no better encoding that is: - Prefix encoding (can divide coded message into symbols without looking ahead) - One-to-one mapping (Symbol→Bits) - Fixed mapping
  • Can we do better without these constraints?

UVa CS216 Spring 2006 - Lecture 15: Numbers 6

Lempel-Ziv-Wench (LZW)

  • Terry Wench refined the L-Z scheme
  • Fixed-length (typically 12-bits) codewords
  • Dictionary maps each codeword to text
  • Greedy scheme for building dictionary

UVa CS216 Spring 2006 - Lecture 15: Numbers 7

LZW Encoding Algorithm def LZWEncode (s): w = “”; res = “”; dictionary.initialize() # code for each alphabet symbol foreach k in s: if dictionary.contains (w + k): w = w + k; else

need to do something if dictionary is full

dictionary.append (w + k) res = res + dictionary.find (w) w = k;

w is already in dictionary: no need to send dictionary to decode! UVa CS216 Spring 2006 - Lecture 15: Numbers 8

Compression Bake-off Declaration of Independence Original 8586 Huffman (PS4) 5123 (60%) Compress (LZW) 4441 (52%) Gzip (not LZW) 3752 (44%) Random Characters Original 10000 Huffman (PS4) 9517 Compress (LZW) 10000 (“file unchanged”) Gzip (not LZW) 8800 This is quite surprising!

UVa CS216 Spring 2006 - Lecture 15: Numbers 9

GIF

  • Graphics Interchange Format developed by Compuserve (1987)
  • Algorithm:
    • Divide image into 8x8 blocks
    • Find optimal Huffman encoding for those blocks
    • Encode result using LZW

How is GIF different from JPEG? UVa CS216 Spring 2006 - Lecture 15: Numbers 10

Lossy/Lossless Compression

  • Lossless Compression:
    • uncompress (compress (S)) = S
  • Lossy Compression:
    • uncompress (compress (S)) similar to S
  • For images, sound, video: lossy compression is usually okay
  • For computer programs, declarations of independence, email: only lossless compression will do!

UVa CS216 Spring 2006 - Lecture 15: Numbers 11

What’s wrong with GIF?

  • 1978: LZ patented by Sperry
  • 1984: (June) Welch’s article on LZW
  • 1984: (July) Unix compress implemented using LZW
  • 1987: Compuserve develops GIF (Graphics Interchange Format) image format, used LZW but didn’t know it was patented
  • GIF becomes popular
  • 1994: Unisys/Compuserve decide that developers who implement LZW (including in GIF) will have to pay a licensing fee
  • 2003: LZW patent expired
    • Divide image into 8x8 blocks
    • Find optimal Huffman encoding for those blocks
    • Encode result using LZW

PNG (“PNG’s Not GIF”) UVa CS216 Spring 2006 - Lecture 15: Numbers 12

Representing Numbers