Efficient Proxies in Smalltalk

829 views

Published on

Presentation of the research paper entitled "Efficient Proxies in Smalltalk" in ESUG International Workshop on Smalltalk Technologies (IWST 2011), Edinburgh, Scotland.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
829
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Efficient Proxies in Smalltalk

  1. 1. RMod 1 Mariano Martinez Peck marianopeck@gmail.com http://marianopeck.wordpress.com/ Monday, August 22, 2011
  2. 2. What is a proxy? 2 A proxy object is a surrogate or placeholder that controls access to another target object. Monday, August 22, 2011
  3. 3. Glossary Target: object to proxify. Client: user of the proxy. Interceptor: object that intercepts message sending. Handler: object that performs a desired action as a consequence of an interception. 3 Monday, August 22, 2011
  4. 4. Forwarder and Logging example 4 Monday, August 22, 2011
  5. 5. With or without Object replacement? In proxies with object replacement (#become:), the target object is replaced by a proxy. Proxies without object replacement are a kind of factory. 5 Monday, August 22, 2011
  6. 6. With or without Object replacement? In proxies with object replacement (#become:), the target object is replaced by a proxy. Proxies without object replacement are a kind of factory. 5 Monday, August 22, 2011
  7. 7. Traditional proxy implementations 6 Usage of a minimal object together with an implementation of a custom #doesNotUnderstand doesNotUnderstand: executeBeforeHandling executeAfterHandling target Proxy identityHash pointersTo nextObject ... ProtoObject Monday, August 22, 2011
  8. 8. 7 We are going to play a little game... Monday, August 22, 2011
  9. 9. 8 Are both prints in Transcript the same or not? Monday, August 22, 2011
  10. 10. 8 Are both prints in Transcript the same or not? Conclusion: methods understood are NOT intercepted. Is that bad? Monday, August 22, 2011
  11. 11. 8 Are both prints in Transcript the same or not? Conclusion: methods understood are NOT intercepted. Is that bad? Different execution paths Errors difficult to find Monday, August 22, 2011
  12. 12. 9 Do we want the regular #doesNotUnderstand or to intercept the message? Monday, August 22, 2011
  13. 13. 10 Do we want the regular #doesNotUnderstand or to intercept the message? Monday, August 22, 2011
  14. 14. 11 I wanted the normal #doesNotUnderstand!!! Monday, August 22, 2011
  15. 15. 12 I wanted the normal #doesNotUnderstand!!! Monday, August 22, 2011
  16. 16. Problems #doesNotUnderstand: cannot be trapped like a regular message. Mix of handling procedure and proxy interception. Only methods that are not understood are intercepted. No separation between proxies and handlers 13 This approach is not stratified Monday, August 22, 2011
  17. 17. 14 Subclassing from nil does not solve the problem. Monday, August 22, 2011
  18. 18. 15 VM CRASH This solution is not uniform Monday, August 22, 2011
  19. 19. 16 A Uniform, Light-weight and Stratified Proxy Model and Implementation. Monday, August 22, 2011
  20. 20. Used hooks Object replacement (#become:) Change an object’s class (#adoptInstance:) Objects as methods (#run:with:in:) Classes with no method dictionary (#cannotInterpret:) 17 Monday, August 22, 2011
  21. 21. Object replacement 18 A B C D A B C D c become: d Monday, August 22, 2011
  22. 22. Objects as methods 19 The VM sends #run: aSelector with: anArray in: aReceiver Monday, August 22, 2011
  23. 23. Objects as methods 19 The VM sends #run: aSelector with: anArray in: aReceiver So.....We can implement in Proxy: Monday, August 22, 2011
  24. 24. CLasses with no method dictionary 20 aProxy aProxy username 1: #username send methodDict := nil ProxyTrap cannotInterpret: aMessage Proxy Object 2: #aProxy lookup 3: Since the method dictionary was nil, the VM sends #cannotInterpret to the receiver but starting the lookup in the superclass 4: #cannotInterpret: lookup References instance of message send lookup subclass Monday, August 22, 2011
  25. 25. Ghost model 21 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object handleInterception: anInterception ProxyHandler handleInterception: anInterception SimpleForwarderHandler message proxy proxyState Interception Monday, August 22, 2011
  26. 26. How it works? 22 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object Monday, August 22, 2011
  27. 27. How it works? 22 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object Proxy class >> Monday, August 22, 2011
  28. 28. How it works? 22 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object aProxy ‘Mariano’aHandler instanceOf Proxy class >> Monday, August 22, 2011
  29. 29. How it works? 22 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object aProxy ‘Mariano’aHandler instanceOf Proxy class >> ProxyTrap class >> Monday, August 22, 2011
  30. 30. How it works? 22 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object aProxy ‘Mariano’aHandler instanceOf Proxy class >> ProxyTrap class >> Monday, August 22, 2011
  31. 31. How it works? 22 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object aProxy ‘Mariano’aHandler instanceOf Proxy class >> Monday, August 22, 2011
  32. 32. aProxy aProxy username 1: #username send methodDict := nil ProxyTrap cannotInterpret: aMessage Proxy Object 2: #aProxy lookup 3: Since the method dictionary was nil, the VM sends #cannotInterpret to the receiver but starting the lookup in the superclass 4: #cannotInterpret: lookup References instance of message send lookup subclass How it works? 23 Monday, August 22, 2011
  33. 33. aProxy aProxy username 1: #username send methodDict := nil ProxyTrap cannotInterpret: aMessage Proxy Object 2: #aProxy lookup 3: Since the method dictionary was nil, the VM sends #cannotInterpret to the receiver but starting the lookup in the superclass 4: #cannotInterpret: lookup References instance of message send lookup subclass How it works? 23 Monday, August 22, 2011
  34. 34. aProxy aProxy username 1: #username send methodDict := nil ProxyTrap cannotInterpret: aMessage Proxy Object 2: #aProxy lookup 3: Since the method dictionary was nil, the VM sends #cannotInterpret to the receiver but starting the lookup in the superclass 4: #cannotInterpret: lookup References instance of message send lookup subclass How it works? 23 Monday, August 22, 2011
  35. 35. aProxy aProxy username 1: #username send methodDict := nil ProxyTrap cannotInterpret: aMessage Proxy Object 2: #aProxy lookup 3: Since the method dictionary was nil, the VM sends #cannotInterpret to the receiver but starting the lookup in the superclass 4: #cannotInterpret: lookup References instance of message send lookup subclass How it works? 23 Monday, August 22, 2011
  36. 36. aProxy aProxy username 1: #username send methodDict := nil ProxyTrap cannotInterpret: aMessage Proxy Object 2: #aProxy lookup 3: Since the method dictionary was nil, the VM sends #cannotInterpret to the receiver but starting the lookup in the superclass 4: #cannotInterpret: lookup References instance of message send lookup subclass How it works? 23 Monday, August 22, 2011
  37. 37. aProxy aProxy username 1: #username send methodDict := nil ProxyTrap cannotInterpret: aMessage Proxy Object 2: #aProxy lookup 3: Since the method dictionary was nil, the VM sends #cannotInterpret to the receiver but starting the lookup in the superclass 4: #cannotInterpret: lookup References instance of message send lookup subclass How it works? 23 Proxy >> Monday, August 22, 2011
  38. 38. aProxy aProxy username 1: #username send methodDict := nil ProxyTrap cannotInterpret: aMessage Proxy Object 2: #aProxy lookup 3: Since the method dictionary was nil, the VM sends #cannotInterpret to the receiver but starting the lookup in the superclass 4: #cannotInterpret: lookup References instance of message send lookup subclass How it works? 23 Proxy >> SimpleForwarderHandler >> Monday, August 22, 2011
  39. 39. 24 Traditional Ghost #doesNotUnderstand: cannot be trapped like a regular message. #cannotInterpret: is trapped like a regular message. Mix of handling procedure and proxy interception. No mix of handling procedure and proxy interception. Only methods that are not understood are intercepted. “All” methods are intercepted. No separation between proxies and handlers. Clear separation between proxies and handlers. Monday, August 22, 2011
  40. 40. 25 is stratified Monday, August 22, 2011
  41. 41. Methods not intercepted 26 1) Optimizations done by the Compiler 2) Special shortcut bytecodes between Compiler and VM 2.1) Methods NEVER sent: #== and #class 2.2) Methods that may or may not be executed depending on the receiver and arguments: e.g. in ‘1+1’ #+ is not executed. But with ‘1+$C’ #+ is executed. 2.3)Always executed, they are just little optimizations. Examples #new, #next, #nextPut:, #size, etc. Monday, August 22, 2011
  42. 42. Methods not intercepted 26 1) Optimizations done by the Compiler 2) Special shortcut bytecodes between Compiler and VM 2.1) Methods NEVER sent: #== and #class 2.2) Methods that may or may not be executed depending on the receiver and arguments: e.g. in ‘1+1’ #+ is not executed. But with ‘1+$C’ #+ is executed. 2.3)Always executed, they are just little optimizations. Examples #new, #next, #nextPut:, #size, etc. Monday, August 22, 2011
  43. 43. Methods not intercepted 26 1) Optimizations done by the Compiler 2) Special shortcut bytecodes between Compiler and VM 2.1) Methods NEVER sent: #== and #class 2.2) Methods that may or may not be executed depending on the receiver and arguments: e.g. in ‘1+1’ #+ is not executed. But with ‘1+$C’ #+ is executed. 2.3)Always executed, they are just little optimizations. Examples #new, #next, #nextPut:, #size, etc. Monday, August 22, 2011
  44. 44. Proxy for classes 27 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object cannotInterpret: proxyFor: superclass methodDict format handler target ClassProxy initialize nilMethodDict ClassProxyTrap logIn: validate: username age User Monday, August 22, 2011
  45. 45. Proxy for classes 27 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object cannotInterpret: proxyFor: superclass methodDict format handler target ClassProxy initialize nilMethodDict ClassProxyTrap logIn: validate: username age User Monday, August 22, 2011
  46. 46. Proxy for classes 27 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object cannotInterpret: proxyFor: superclass methodDict format handler target ClassProxy initialize nilMethodDict ClassProxyTrap logIn: validate: username age User Monday, August 22, 2011
  47. 47. Proxy for classes 27 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object cannotInterpret: proxyFor: superclass methodDict format handler target ClassProxy initialize nilMethodDict ClassProxyTrap logIn: validate: username age User Monday, August 22, 2011
  48. 48. Proxy for classes 27 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object cannotInterpret: proxyFor: superclass methodDict format handler target ClassProxy initialize nilMethodDict ClassProxyTrap aUser aClassProxy methodDict = nil superclass = ClassProxy logIn: validate: username age User instance of Monday, August 22, 2011
  49. 49. Proxy for classes 27 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object cannotInterpret: proxyFor: superclass methodDict format handler target ClassProxy initialize nilMethodDict ClassProxyTrap aUser aClassProxy methodDict = nil superclass = ClassProxy logIn: validate: username age User instance of User name Monday, August 22, 2011
  50. 50. Proxy for classes 27 cannotInterpret: proxyFor: handler target Proxy initialize nilMethodDict ProxyTrap Object cannotInterpret: proxyFor: superclass methodDict format handler target ClassProxy initialize nilMethodDict ClassProxyTrap aUser aClassProxy methodDict = nil superclass = ClassProxy logIn: validate: username age User instance of aUser username User name Monday, August 22, 2011
  51. 51. Proxy for methods 28 Monday, August 22, 2011
  52. 52. Proxy for methods 28 Regular message Monday, August 22, 2011
  53. 53. Proxy for methods 28 Regular message Method execution Monday, August 22, 2011
  54. 54. Proxy for methods 28 Just handling #run:with:in correctly is enough to also intercept method execution. Regular message Method execution Monday, August 22, 2011
  55. 55. is 29 Uniform Monday, August 22, 2011
  56. 56. is 29 moreUniform Monday, August 22, 2011
  57. 57. More features Low memory footprint. Compact classes. Store the minimal needed state. Easy debugging. Custom list of messages. 30 Monday, August 22, 2011
  58. 58. Conclusion 31 With a little bit of special support from the VM (#cannotInterpret hook), we can have an image-side proxy solution much better than the classic #doesNotUnderstand: Monday, August 22, 2011
  59. 59. Future work Experiment with immediate proxies (memory address tag) in VM side. Think how to correctly intercept non- executed methods. 32 Monday, August 22, 2011
  60. 60. Links http://rmod.lille.inria.fr/web/pier/software/ Marea/GhostProxies http://www.squeaksource.com/Marea.html 33 Monday, August 22, 2011
  61. 61. Mariano Martinez Peck marianopeck@gmail.com http://marianopeck.wordpress.com/ RMod A Uniform, Light-weight and Stratified Proxy Model and Implementation Monday, August 22, 2011

×