interruptible iteratorsjed/slides/jmatch-popl2006-slides.pdf · iteration abstractions important to...

50
Interruptible Iterators Jed Liu Aaron Kimball Andrew C. Myers Department of Computer Science Cornell University 33 rd ACM Symposium on Principles of Programming Languages 13 January, 2006 Jed Liu et al. Interruptible Iterators 1

Upload: others

Post on 19-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Interruptible Iterators

Jed Liu Aaron Kimball Andrew C. Myers

Department of Computer ScienceCornell University

33rd ACM Symposium on Principles of Programming Languages13 January, 2006

Jed Liu et al. Interruptible Iterators 1

Page 2: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Iteration Abstractions

Important to support iteration abstractions wellI Clients get on-demand access to elements of a

lazily-evaluated sequenceI Many mainstream languages support IAs

I e.g., C++, Python, RubyI Evolving to support better: C# 2.0, Java 1.5I Libraries too: Java Collections, Microsoft .NET

I Iterators are hard to implementI Especially if they support imperative update

Interruptible iterators make IAs easier to implement

I Implemented as part of JMatch

Jed Liu et al. Interruptible Iterators 1

Page 3: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Iterators: Easy to Use, Hard to Write

Easy to use: Java iterator interfaceinterface Iterator {

boolean hasNext(); // Is there a next element?

Object next(); // Return the next element

void remove(); // Remove last element returned

}

Can be hard to implementI Iteration must continue where it last left off

I Iterator can become awkward state machine

Jed Liu et al. Interruptible Iterators 2

Page 4: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Binary Tree Iterator Example in Java

class TreeIterator implements Iterator {

Iterator subIterator;

boolean hasNext;

Object current;

// 1 = Iterating through left child

// 2 = Just yielded current node value

// 3 = Iterating through right child

int state;

TreeIterator() {

subIterator = Tree.this.left.iterator();

state = 1;

current = preload();

}

public boolean hasNext() {

return hasNext;

}

public Object next() {

if (!hasNext)

throw new NoSuchElementException();

Object result = current;

current = preload();

return result;

}

private Object preload() {

loop: while (true) {

switch (state) {

case 1:

case 3:

hasNext = true;

if (subIterator.hasNext()) {

return subIterator.next();

}

if (state == 1) {

state = 2;

return Tree.this.value;

}

hasNext = false;

return null;

case 2:

subIterator =

Tree.this.right.iterator();

state = 3;

continue loop;

}

}

}

}

Jed Liu et al. Interruptible Iterators 3

Page 5: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Binary Tree Iterator Example in Java

class TreeIterator implements Iterator {

Iterator subIterator;

boolean hasNext;

Object current;

// 1 = Iterating through left child

// 2 = Just yielded current node value

// 3 = Iterating through right child

int state;

TreeIterator() {

subIterator = Tree.this.left.iterator();

state = 1;

current = preload();

}

public boolean hasNext() {

return hasNext;

}

public Object next() {

if (!hasNext)

throw new NoSuchElementException();

Object result = current;

current = preload();

return result;

}

private Object preload() {

loop: while (true) {

switch (state) {

case 1:

case 3:

hasNext = true;

if (subIterator.hasNext()) {

return subIterator.next();

}

if (state == 1) {

state = 2;

return Tree.this.value;

}

hasNext = false;

return null;

case 2:

subIterator =

Tree.this.right.iterator();

state = 3;

continue loop;

}

}

}

}

Even worse when youadd support for updatesEven worse when youadd support for updates

Jed Liu et al. Interruptible Iterators 3

Page 6: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Coroutine Iterators

I Increasingly popular: C# 2.0, Python, Ruby

I Iterator as a coroutine:I Separate stackI Iterator suspends execution by yielding valuesI Client obtains more values by resuming iterator

Example: JMatch binary tree iterator

class Node {

int val; Node left, right;

int elements() iterates(result) {

foreach (int elt = left.elements()) yield elt;

yield val;

foreach (int elt = right.elements()) yield elt;

}

}

Jed Liu et al. Interruptible Iterators 4

Page 7: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Coroutine Iterators

I Increasingly popular: C# 2.0, Python, Ruby

I Iterator as a coroutine:I Separate stackI Iterator suspends execution by yielding valuesI Client obtains more values by resuming iterator

Example: JMatch binary tree iterator

class Node {

int val; Node left, right;

int elements() iterates(result) {

foreach (int elt = left.elements()) yield elt;

yield val;

foreach (int elt = right.elements()) yield elt;

}

}

elements is an iteratorelements is an iterator

Jed Liu et al. Interruptible Iterators 4

Page 8: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Coroutine Iterators

I Increasingly popular: C# 2.0, Python, Ruby

I Iterator as a coroutine:I Separate stackI Iterator suspends execution by yielding valuesI Client obtains more values by resuming iterator

Example: JMatch binary tree iterator

class Node {

int val; Node left, right;

int elements() iterates(result) {

foreach (int elt = left.elements()) yield elt;

yield val;

foreach (int elt = right.elements()) yield elt;

}

}

Jed Liu et al. Interruptible Iterators 4

Page 9: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Coroutine Iterators

I Increasingly popular: C# 2.0, Python, Ruby

I Iterator as a coroutine:I Separate stackI Iterator suspends execution by yielding valuesI Client obtains more values by resuming iterator

Example: JMatch binary tree iterator

class Node {

int val; Node left, right;

int elements() iterates(result) {

foreach (int elt = left.elements()) yield elt;

yield val;

foreach (int elt = right.elements()) yield elt;

}

}

Jed Liu et al. Interruptible Iterators 4

Page 10: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Coroutine Iterators

I Increasingly popular: C# 2.0, Python, Ruby

I Iterator as a coroutine:I Separate stackI Iterator suspends execution by yielding valuesI Client obtains more values by resuming iterator

Example: JMatch binary tree iterator

class Node {

int val; Node left, right;

int elements() iterates(result) {

foreach (int elt = left.elements()) yield elt;

yield val;

foreach (int elt = right.elements()) yield elt;

}

}

Jed Liu et al. Interruptible Iterators 4

Page 11: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Coroutine Iterators

class Node {

int val; Node left, right;

int elements() iterates(result) {

foreach (int elt = left.elements()) yield elt;

yield val;

foreach (int elt = right.elements()) yield elt;

}

}

Client

elements

elements

elementsyield

yield

yield

Jed Liu et al. Interruptible Iterators 4

Page 12: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Coroutine Iterators

class Node {

int val; Node left, right;

int elements() iterates(result) {

foreach (int elt = left.elements()) yield elt;

yield val;

foreach (int elt = right.elements()) yield elt;

}

}

Client

elements

elements

elementsyield

yield

yield

Only a partial solution: no imperative updatesOnly a partial solution: no imperative updates

Jed Liu et al. Interruptible Iterators 4

Page 13: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Imperative Updates

I Unsafe to changeunderlying datastructure directlyduring iteration

I All updates must gothrough the iterator

I Java: remove()

I Previous coroutineiterators don’t haveupdate

Example: Tree iterator

iteratoriterator

Jed Liu et al. Interruptible Iterators 5

Page 14: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Imperative Updates

I Unsafe to changeunderlying datastructure directlyduring iteration

I All updates must gothrough the iterator

I Java: remove()

I Previous coroutineiterators don’t haveupdate

Example: Tree iterator

iteratoriterator

Iterator no longer pointsto part of the tree

Jed Liu et al. Interruptible Iterators 5

Page 15: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Interruptible Iterators

JMatch extends coroutine iterators to handleupdates via interrupts:

1. Client raises interrupt

2. Iterator handles interrupt

3. Control returns to client after raise

Example

Collection c = ...;

foreach (Object o = c.elements()) {

if (o == null) raise new Remove();

System.out.println(o);

}

Jed Liu et al. Interruptible Iterators 6

Page 16: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Interruptible Iterators

JMatch extends coroutine iterators to handleupdates via interrupts:

1. Client raises interrupt

2. Iterator handles interrupt

3. Control returns to client after raise

Example

Collection c = ...;

foreach (Object o = c.elements()) {

if (o == null) raise new Remove();

System.out.println(o);

}

Generates a Remove interruptGenerates a Remove interrupt

Jed Liu et al. Interruptible Iterators 6

Page 17: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Interruptible Iterators

JMatch extends coroutine iterators to handleupdates via interrupts:

1. Client raises interrupt

2. Iterator handles interrupt

3. Control returns to client after raise

Example

Collection c = ...;

foreach (Object o = c.elements()) {

if (o == null) raise new Remove();

System.out.println(o);

}

Receives and handles the interruptReceives and handles the interrupt

Jed Liu et al. Interruptible Iterators 6

Page 18: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Interruptible Iterators

JMatch extends coroutine iterators to handleupdates via interrupts:

1. Client raises interrupt

2. Iterator handles interrupt

3. Control returns to client after raise

Example

Collection c = ...;

foreach (Object o = c.elements()) {

if (o == null) raise new Remove();

System.out.println(o);

}

Execution continues immediatelyafter raise statementExecution continues immediatelyafter raise statement

Jed Liu et al. Interruptible Iterators 6

Page 19: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Interruptible Iterators

JMatch extends coroutine iterators to handleupdates via interrupts:

1. Client raises interrupt

2. Iterator handles interrupt

3. Control returns to client after raise

Example

Collection c = ...;

foreach (Object o = c.elements()) {

if (o == null) raise new Remove();

System.out.println(o);

}

Jed Liu et al. Interruptible Iterators 6

Page 20: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Declaring Interrupt Handlers

I JMatch iterators declare handled interrupts

I Compiler checks all interrupts are handled

Example

interface Collection {

...

Object elements() traps Remove

iterates(result);

}

Jed Liu et al. Interruptible Iterators 7

Page 21: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Declaring Interrupt Handlers

I JMatch iterators declare handled interrupts

I Compiler checks all interrupts are handled

Example

interface Collection {

...

Object elements() traps Remove

iterates(result);

}

elements is an iterator thathandles Remove interruptselements is an iterator thathandles Remove interrupts

Jed Liu et al. Interruptible Iterators 7

Page 22: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Writing an Interruptible Iterator

Example: Linked list iterator

// Object head; List tail;

Object elements() traps SetValue iterates(result) {

yield head;

foreach (Object elt = tail.elements())

yield elt;

}Jed Liu et al. Interruptible Iterators 8

Page 23: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Writing an Interruptible Iterator

Example: Linked list iterator

// Object head; List tail;

Object elements() traps SetValue iterates(result) {

yield head;

foreach (Object elt = tail.elements())

yield elt;

}

Handling SetValue overwritesprevious element returned(a la Java: ListIterator.set())

Handling SetValue overwritesprevious element returned(a la Java: ListIterator.set())

Jed Liu et al. Interruptible Iterators 8

Page 24: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Writing an Interruptible Iterator

Example: Linked list iterator

// Object head; List tail;

Object elements() traps SetValue iterates(result) {

yield head;

foreach (Object elt = tail.elements())

yield elt;

}

Interrupt appears tobe raised by yield;propagates outwardlike an exception

Interrupt appears tobe raised by yield;propagates outwardlike an exception

Jed Liu et al. Interruptible Iterators 8

Page 25: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Writing an Interruptible Iterator

Example: Linked list iterator

// Object head; List tail;

Object elements() traps SetValue iterates(result) {

try {

yield head;

} trap (SetValue s) {

head = s.value; // resume in caller

}

foreach (Object elt = tail.elements())

yield elt;

}Jed Liu et al. Interruptible Iterators 8

Page 26: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Writing an Interruptible Iterator

Example: Linked list iterator

// Object head; List tail;

Object elements() traps SetValue iterates(result) {

try {

yield head;

} trap (SetValue s) {

head = s.value; // resume in caller

}

foreach (Object elt = tail.elements())

yield elt;

}Interrupt appears here tooInterrupt appears here too

Jed Liu et al. Interruptible Iterators 8

Page 27: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Writing an Interruptible Iterator

Example: Linked list iterator

// Object head; List tail;

Object elements() traps SetValue iterates(result) {

try {

yield head;

} trap (SetValue s) {

head = s.value; // resume in caller

}

foreach (Object elt = tail.elements())

yield elt;

}

Receives interruptReceives interrupt

Jed Liu et al. Interruptible Iterators 8

Page 28: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Writing an Interruptible Iterator

Example: Linked list iterator

// Object head; List tail;

Object elements() traps SetValue iterates(result) {

try {

yield head;

} trap (SetValue s) {

head = s.value; // resume in caller

}

foreach (Object elt = tail.elements())

yield elt;

}

Client

elements

elements

elements

yieldyield

yield

Jed Liu et al. Interruptible Iterators 8

Page 29: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Writing an Interruptible Iterator

Example: Linked list iterator

// Object head; List tail;

Object elements() traps SetValue iterates(result) {

try {

yield head;

} trap (SetValue s) {

head = s.value; // resume in caller

}

foreach (Object elt = tail.elements())

yield elt;

}

Client

elements

elements

elements

raiseraiseraise

Jed Liu et al. Interruptible Iterators 8

Page 30: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Writing an Interruptible Iterator

Example: Linked list iterator

// Object head; List tail;

Object elements() traps SetValue iterates(result) {

try {

yield head;

} trap (SetValue s) {

head = s.value; // resume in caller

}

foreach (Object elt = tail.elements())

yield elt;

}

Client

elements

elements

elements

raiseraiseraise

resumeresume

resume

Jed Liu et al. Interruptible Iterators 8

Page 31: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Declarative Iterators

State MachineIterators

CoroutineIterators

DeclarativeIterators

+ Interrupts

Jed Liu et al. Interruptible Iterators 9

Page 32: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Declarative Iterators

Example: JMatch hash map [PADL 2003]

boolean contains(Object key, Object value)

iterates(key,value) iterates(key) returns(value)

(

int n = hash(key) &&

Bucket b = table[n] &&

b.contains(key, value)

)

Example uses

Jed Liu et al. Interruptible Iterators 10

Page 33: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Declarative Iterators

Example: JMatch hash map [PADL 2003]

boolean contains(Object key, Object value)

iterates(key,value) iterates(key) returns(value)

(

int n = hash(key) &&

Bucket b = table[n] &&

b.contains(key, value)

)

Example uses

Logical formulainterpreted inmultiple ways

Logical formulainterpreted inmultiple ways

Jed Liu et al. Interruptible Iterators 10

Page 34: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Declarative Iterators

Example: JMatch hash map [PADL 2003]

boolean contains(Object key, Object value)

iterates(key,value) iterates(key) returns(value)

(

int n = hash(key) &&

Bucket b = table[n] &&

b.contains(key, value)

)

Example uses

Mode declarationsMode declarations

Jed Liu et al. Interruptible Iterators 10

Page 35: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Declarative Iterators

Example: JMatch hash map [PADL 2003]

boolean contains(Object key, Object value)

iterates(key,value) iterates(key) returns(value)

(

int n = hash(key) &&

Bucket b = table[n] &&

b.contains(key, value)

)

Example uses

foreach (map.contains(Object key, Object value)) ...

Iterates all key-value pairsIterates all key-value pairs

Jed Liu et al. Interruptible Iterators 10

Page 36: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Declarative Iterators

Example: JMatch hash map [PADL 2003]

boolean contains(Object key, Object value)

iterates(key,value) iterates(key) returns(value)

(

int n = hash(key) &&

Bucket b = table[n] &&

b.contains(key, value)

)

Example uses

foreach (map.contains(Object key, Object value)) ...

foreach (map.contains(Object key, "foo")) ...

Iterates all keys thatmap to a given valueIterates all keys thatmap to a given value

Jed Liu et al. Interruptible Iterators 10

Page 37: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Declarative Iterators

Example: JMatch hash map [PADL 2003]

boolean contains(Object key, Object value)

iterates(key,value) iterates(key) returns(value)

(

int n = hash(key) &&

Bucket b = table[n] &&

b.contains(key, value)

)

Example uses

foreach (map.contains(Object key, Object value)) ...

foreach (map.contains(Object key, "foo")) ...

let map.contains("foo", Object value);

Returns value mappedby a given keyReturns value mappedby a given key

Jed Liu et al. Interruptible Iterators 10

Page 38: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Declarative Iterators

Example: JMatch hash map [PADL 2003]

boolean contains(Object key, Object value)

iterates(key,value) iterates(key) returns(value)

(

int n = hash(key) &&

Bucket b = table[n] &&

b.contains(key, value)

)

Example uses

foreach (map.contains(Object key, Object value)) ...

foreach (map.contains(Object key, "foo")) ...

let map.contains("foo", Object value);

if (map.contains("foo", "bar")) ...

Implicit modeImplicit mode

Jed Liu et al. Interruptible Iterators 10

Page 39: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Declarative Iterators + Imperative Update

Example: JMatch hash map [PADL 2003]

boolean contains(Object key, Object value)

traps Remove

iterates(key,value) iterates(key) returns(value)

(

int n = hash(key) &&

Bucket b = table[n] &&

b.contains(key, value)

)

Example use

foreach (map.contains(Object key, Object value))

if (value == null) raise new Remove();

Updates can be supportedvia interruptsUpdates can be supportedvia interrupts

Jed Liu et al. Interruptible Iterators 10

Page 40: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Implementation

I JMatch implemented using Polyglot extensiblecompiler framework [CC 2003]

I Java back-end available for downloadI Designed C++ back-end for performance evaluation

I Better memory management for coroutine stack

I Tail-yield optimisation: send values back toclient in constant time

Client

elements

elements

elements

yield

Jed Liu et al. Interruptible Iterators 11

Page 41: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Evaluation

Expressiveness (LOC)

Java JMatch SavingsArrayList 204 112 45%

LinkedList 249 155 38%HashMap 434 158 64%TreeMap 805 472 41%

Total 1692 897 47%

Performance vs. C++ STL

Average 3% difference iterating 250k elements:LinkedList, HashMap, TreeMap vs. STL equivalent

I More results in paper, including vs. Java

Jed Liu et al. Interruptible Iterators 12

Page 42: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Related Work

I Coroutine iteratorsI CLU, ICON, Python, Ruby, C#I Sather: Limited support for imperative updates

through “hot” arguments

I CoroutinesI Simula, Modula-2, BETA

I Resumption-style exceptionsI Cedar

I First-class continuationsI SML/NJ, Scheme, Ruby

Jed Liu et al. Interruptible Iterators 13

Page 43: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Summary

I Interrupts make it easier to write iteration abstractionswith imperative update

I Supports coroutine and declarative iteratorsI Implemented for Java in JMatchI LOC savings without performance penalty

Also in the paper...

I Non-compositionality of Java iteratorsI Interaction of interrupts & exceptionsI Static checking of interrupts

I Checks all raised interrupts have unique handlerI Support for first-class iterator objects

I Implement Java iterator interface

http://www.cs.cornell.edu/projects/jmatch/Jed Liu et al. Interruptible Iterators 14

Page 44: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Java Iterators are Non-Compositional

interface Iterator {

boolean hasNext();

Object next();

void remove();

}

Client

1 2 3 4 5

Jed Liu et al. Interruptible Iterators 15

Page 45: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Java Iterators are Non-Compositional

interface Iterator {

boolean hasNext();

Object next();

void remove();

}

Client

List iterator

1 2 3 4 5

Jed Liu et al. Interruptible Iterators 15

Page 46: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Java Iterators are Non-Compositional

interface Iterator {

boolean hasNext();

Object next();

void remove();

}

Client

Filtering iterator

List iterator

1 2 3 4 5

Jed Liu et al. Interruptible Iterators 15

Page 47: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Java Iterators are Non-Compositional

interface Iterator {

boolean hasNext();

Object next();

void remove();

}

Client next()next()

Filtering iterator

List iterator

1 2 3 4 5

Jed Liu et al. Interruptible Iterators 15

Page 48: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Java Iterators are Non-Compositional

interface Iterator {

boolean hasNext();

Object next();

void remove();

}

Client hasNext()hasNext()

Filtering iterator

List iterator

1 2 3 4 5

Jed Liu et al. Interruptible Iterators 15

Page 49: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Java Iterators are Non-Compositional

interface Iterator {

boolean hasNext();

Object next();

void remove();

}

Client remove()remove()

!

Filtering iterator

List iterator

1 2 3 4 5

Jed Liu et al. Interruptible Iterators 15

Page 50: Interruptible Iteratorsjed/slides/jmatch-popl2006-slides.pdf · Iteration Abstractions Important to support iteration abstractions well I Clients get on-demand access to elements

Performance Results

ArrayList LinkedList HashMap TreeMapJMatch 135.0 56.1 3.7 3.1

C++ STL 215.0 57.7 3.1 3.9Java 6.3 10.3 4.2 3.5

Millions of elements iterated per second, iterating over collectionsof 250k elements. Average of 8 measurements, σ ≤ 5%.

Jed Liu et al. Interruptible Iterators 16