interruptible iteratorsjed/slides/jmatch-popl2006-slides.pdf · iteration abstractions important to...
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/34.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/35.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/36.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/37.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/38.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/39.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/40.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/42.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/43.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/44.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/45.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/46.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/47.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/48.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/49.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b2ed17890591e132be67b/html5/thumbnails/50.jpg)
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