Caching: An Optimization Aspect - PowerPoint PPT Presentation

About This Presentation
Title:

Caching: An Optimization Aspect

Description:

Cache the value of the sum for each container and the number of violations. If the same container is checked again and there has been no change, we can ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 15
Provided by: CCS3
Category:

less

Transcript and Presenter's Notes

Title: Caching: An Optimization Aspect


1
Caching An Optimization Aspect
2
Part 1a
  • Background Container/Simple both inherit from
    Item Container contains vector of Item.
  • Cache the value of the sum for each container and
    the number of violations.
  • If the same container is checked again and there
    has been no change, we can reuse the cached values

3
HashTable method
  • Hashtable cachenew Hashtable()
  • pointcut changed(Container c)target(c)
  • call( Container.addItem(..))
  • before(Container c)changed(c)
    if(cache.containsKey(c))
  • Container containc.getContainer()
  • cache.remove(c) //invalidate
  • while(contain!null) cache.remove(contain)
    containcontain.getContainer()

4
  • pointcut getvalue(Item i)target(i)
  • call( .check(..))
  • int around(Item i)getvalue(i)
    if(cache.containsKey(i))
  • return ((Integer)cache.get(i)).intValue() int
    vthisJoinPoint.proceed(i) cache.put(i,new
    Integer(v))
  • return v

5
Introductions
  • private int Container.violations 0
  • private int Container.total 0
  • private boolean
  • Container.cacheIsValid false

6
  • pointcut getTotal(Container c)
  • call (int check(..)) target(c)
  • int around (Container c) getTotal(c)
  • if(c.cacheIsValid)
  • System.out.println(c.name " using cached
    value for total " c.total)
  • if(c.violations gt 0)
  • System.out.println(c.name " had "
    c.violations " violations")
  • return c.total
  • else return proceed(c)

7
  • after(Container c) returning (int tot)
    getTotal(c)
  • c.total tot
  • c.violations c.total - c.capacity
  • c.cacheIsValid true

8
Adding a new item
  • pointcut newItem(Container c, Item i)
    call(void setContainer(..))
  • target(i) args(c)
  • after(Container c, Item i) newItem(c, i)
  • c.setValid(false)
  • public void Container.setValid(boolean
    isValid) cacheIsValid isValid
  • if(getContainer() ! null)
  • getContainer().setValid(isValid)

9
What was learned?
  • The Hashtable allows us to better encapsulate the
    Caching aspect, leaving us with more elegant code
    that doesnt pollute the name-space of Container
  • It seems cleaner for each Container to keep track
    of its total weight. This will also probably
    shorten the run-time.

10
Part 1b/d Improving modularity and reusability
  • If we dont cache, we dont need the back
    pointers in our containers. So make it an aspect!
  • Improve reusability through use of abstract
    aspects.

11
Back pointer based on introductions
  • private Container Item.container
  • public void Item.setContainer(Container c)
  • container c
  • public Container Item.getContainer()
  • return container

12
Setting the BP
  • pointcut addingItem(Container c, Item i)
    call (void addItem(..))
  • target(c) args(i)
  • after(Container c, Item i) addingItem(c, i)
  • i.setContainer(c)

13
Abstract Caching Aspect
  • There is a Parent/Child relationship which can be
    useful in creating abstract aspects.
  • interface Parent extends Child
  • void addItem(Child c)
  • abstract aspect Cashing
  • abstract pointcut invalidate(Parent p)
  • abstract pointcut cashing(Parent p)

14
  • Such that we can implement interesting
    functionality based
  • only on knowledge that a Parent/Child
    relationship exists.
  • after(Parent p) invalidate(p)
  • while(p ! null)
  • beforeInvalidate(p)
  • p.cashedValue -1
  • p ((Child)p).getParent(
    )
Write a Comment
User Comments (0)
About PowerShow.com