SlideShare a Scribd company logo
1 of 17
Download to read offline
Threaded-Execution and CPS Provide Smooth Switching Between Execution
Modes
Dave Mason
Toronto Metropolitan University
©2023 Dave Mason
Execution Models
Source Interpretation
Bytecode Interpretation
Threaded Execution
Hardware Interpretation
Timing of Fibonacci
Thread CPS Object NativePharo JIT
169
278
347
1,094
559
186
304
391
1,918
591
136
332
564
2,010
695
Apple M1
Apple M2 Pro
Intel i7 2.8GHz
Threaded is 2.3-4.7 times faster than bytecode.
Zag Smalltalk
supports 2 models: threaded and native CPS
seamless transition
threaded is smaller and fully supports step-debugging
native is 3-5 times faster and can fallback to full debugging after a send
Threaded Execution
sequence of addresses of native code
like an extensible bytecode
each word passes control to the next
associated with Forth, originally in PDP-11 FORTRAN compiler, was used in BrouHaHa
fibonacci
1 fibonacci
2 self <= 2 ifTrue: [ ↑ 1 ].
3 ↑ (self - 1) fibonacci + (self - 2) fibonacci
Threaded fibonacci
1 verifySelector,
2 ":recurse",
3 dup, // self
4 pushLiteral, Object.from(2),
5 p5, // <=
6 ifFalse,"label3",
7 drop, // self
8 pushLiteral1,
9 returnNoContext,
10 ":label3",
11 pushContext,"^",
12 pushLocal0, // self
13 pushLiteral1,
14 p2, // -
15 callRecursive, "recurse",
16 pushLocal0, //self
17 pushLiteral2,
18 p2, // -
19 callRecursive, "recurse",
20 p1, // +
21 returnTop,
Some control words
1 pub fn drop(pc:PC, sp:Stack, process:*Process, context:ContextPtr, selector:Object) Stack {
2 tailcall pc[0].prim(pc+1, sp+1, process, context, selector, cache);
3 }
4 pub fn dup(pc:PC, sp:Stack, process:*Process, context:ContextPtr, selector:Object) Stack {
5 const newSp = sp-1;
6 newSp[0] = newSp[1];
7 tailcall pc[0].prim(pc+1, newSp, process, context, selector, cache);
8 }
9 pub fn ifFalse(pc:PC, sp:Stack, process:*Process, context:ContextPtr, selector:Object) Stack {
10 const v = sp[0];
11 if (False.equals(v)) tailcall branch(pc, sp+1, process, context, selector, cache );
12 if (True.equals(v)) tailcall pc[1].prim(pc+2, sp+1, process, context, selector, cache );
13 @panic("non boolean");
14 }
15 pub fn p1(pc:PC, sp:Stack, process:*Process, context:ContextPtr, selector:Object) Stack {
16 if (!Sym.@"+".selectorEquals(selector)) tailcall dnu(pc, sp, process, context, selector);
17 sp[1] = inlines.p1(sp[1], sp[0])
18 catch tailcall pc[0].prim(pc+1, sp, process, context, selector, cache);
19 tailcall context.npc(context.tpc, sp+1, process, context, selector, cache);
20 }
Continuation Passing Style
continuation is the rest of the program
comes from optimization of functional languages (continuation was a closure)
no implicit stack frames - passed explicitly
like the original Smalltalk passing Context (maybe not obvious that Context is a special kind of closure)
Normal Style
1 pub fn fibNative(self: i64) i64 {
2 if (self <= 2) return 1;
3 return fibNative(self - 1) + fibNative(self - 2);
4 }
5 const one = Object.from(1);
6 const two = Object.from(2);
7 pub fn fibObject(self: Object) Object {
8 if (i.p5N(self,two)) return one;
9 const m1 = i.p2L(self, 1) catch @panic("int subtract failed in fibObject");
10 const fm1 = fibObject(m1);
11 const m2 = i.p2L(self, 2) catch @panic("int subtract failed in fibObject");
12 const fm2 = fibObject(m2);
13 return i.p1(fm1, fm2) catch @panic("int add failed in fibObject");
14 }
Continuation Passing Style
1 pub fn fibCPS(pc:PC, sp:Stack, process:*Process, context:ContextPtr, selector:Object) Stack {
2 if (!fibSym.equals(selector)) tailcall dnu(pc,sp,process,context,selector);
3 if (inlined.p5N(sp[0],Object.from(2))) {
4 sp[0] = Object.from(1);
5 tailcall context.npc(context.tpc,sp,process,context,selector);
6 }
7 const newContext = context.push(sp,process,fibThread.asCompiledMethodPtr(),0,2,0);
8 const newSp = newContext.sp();
9 newSp[0]=inlined.p2L(sp[0],1)
10 catch tailcall pc[10].prim(pc+11,newSp+1,process,context,fibSym);
11 newContext.setReturnBoth(fibCPS1, pc+13); // after first callRecursive (line 15 above)
12 tailcall fibCPS(fibCPST+1,newSp,process,newContext,fibSym);
13 }
Continuation Passing Style
1 fn fibCPS1(pc:PC, sp:Stack, process:*Process, context:ContextPtr, _:Object) Stack {
2 const newSp = sp.push();
3 newSp[0] = inlined.p2L(context.getTemp(0),2)
4 catch tailcall pc[0].prim(pc+1,newSp,process,context,fibSym));
5 context.setReturnBoth(fibCPS2, pc+3); // after 2nd callRecursive (line 19 above)
6 tailcall fibCPS(fibCPST+1,newSp,process,context,fibSym);
7 }
Continuation Passing Style
1 fn fibCPS2(pc:PC, sp:Stack, process:*Process, context:ContextPtr, selector:Object) Stack {
2 const sum = inlined.p1(sp[1],sp[0])
3 catch tailcall pc[0].prim(pc+1,sp,process,context,fibSym);
4 const result = context.pop(process);
5 const newSp = result.sp;
6 newSp.put0(sum);
7 const callerContext = result.ctxt;
8 tailcall callerContext.npc(callerContext.tpc,newSp,process,callerContext,selector);
9 }
Implementation Decisions
Context must contain not only native return points, but also threaded return points;
CompiledMethods must facilitate seamless switching between execution modes;
the stack cannot reasonably be woven into the hardware stack with function calls, so no native stack;
as well as parameters, locals, and working space, stack is used to allocate Context and BlockClosure as
usually released in LIFO pattern
Conclusions
with proper structures, can easily switch between threaded and native code
threaded code is “good enough” for many purposes
this is preliminary work, so some open questions
many experiments to run to validate my intuitions
many more details in the paper
Questions?
@DrDaveMason dmason@torontomu.ca
https://github.com/dvmason/Zag-Smalltalk
Timing of Fibonacci
Thread CPS P-JIT P-Stack Z-Byte
347
1,094
559
5,033
4,730
391
1,918
591
3,527
4,820
564
2,010
695
3,568
4,609
Apple M1
Apple M2 Pro
Intel i7 2.8GHz
M2 P-Stack is presumed to be mis-configured

More Related Content

Similar to Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes

Exploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelExploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelVitaly Nikolenko
 
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoCapturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoScyllaDB
 
What's new with Apache Spark's Structured Streaming?
What's new with Apache Spark's Structured Streaming?What's new with Apache Spark's Structured Streaming?
What's new with Apache Spark's Structured Streaming?Miklos Christine
 
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...South Tyrol Free Software Conference
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Adrian Huang
 
To Infinity & Beyond: Protocols & sequences in Node - Part 2
To Infinity & Beyond: Protocols & sequences in Node - Part 2To Infinity & Beyond: Protocols & sequences in Node - Part 2
To Infinity & Beyond: Protocols & sequences in Node - Part 2Bahul Neel Upadhyaya
 
Linux Process & CF scheduling
Linux Process & CF schedulingLinux Process & CF scheduling
Linux Process & CF schedulingSangJung Woo
 
Cpu高效编程技术
Cpu高效编程技术Cpu高效编程技术
Cpu高效编程技术Feng Yu
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойSigma Software
 
Asynchronous programming with java script and node.js
Asynchronous programming with java script and node.jsAsynchronous programming with java script and node.js
Asynchronous programming with java script and node.jsTimur Shemsedinov
 
CorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. ProgrammingCorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. ProgrammingSlide_N
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the wayOleg Podsechin
 
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsAsuka Nakajima
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptMuhammad Sikandar Mustafa
 
Using R on Netezza
Using R on NetezzaUsing R on Netezza
Using R on NetezzaAjay Ohri
 
Handling Large State on BEAM
Handling Large State on BEAMHandling Large State on BEAM
Handling Large State on BEAMYoshihiro TANAKA
 
Direct Code Execution @ CoNEXT 2013
Direct Code Execution @ CoNEXT 2013Direct Code Execution @ CoNEXT 2013
Direct Code Execution @ CoNEXT 2013Hajime Tazaki
 
0.5mln packets per second with Erlang
0.5mln packets per second with Erlang0.5mln packets per second with Erlang
0.5mln packets per second with ErlangMaxim Kharchenko
 

Similar to Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes (20)

MPI
MPIMPI
MPI
 
Exploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernelExploitation of counter overflows in the Linux kernel
Exploitation of counter overflows in the Linux kernel
 
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoCapturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
 
What's new with Apache Spark's Structured Streaming?
What's new with Apache Spark's Structured Streaming?What's new with Apache Spark's Structured Streaming?
What's new with Apache Spark's Structured Streaming?
 
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel  write Python code, get Fortran ...
SFSCON23 - Emily Bourne Yaman Güçlü - Pyccel write Python code, get Fortran ...
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...
 
To Infinity & Beyond: Protocols & sequences in Node - Part 2
To Infinity & Beyond: Protocols & sequences in Node - Part 2To Infinity & Beyond: Protocols & sequences in Node - Part 2
To Infinity & Beyond: Protocols & sequences in Node - Part 2
 
Linux Process & CF scheduling
Linux Process & CF schedulingLinux Process & CF scheduling
Linux Process & CF scheduling
 
Cpu高效编程技术
Cpu高效编程技术Cpu高效编程技术
Cpu高效编程技术
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай МозговойСтолпы функционального программирования для адептов ООП, Николай Мозговой
Столпы функционального программирования для адептов ООП, Николай Мозговой
 
Asynchronous programming with java script and node.js
Asynchronous programming with java script and node.jsAsynchronous programming with java script and node.js
Asynchronous programming with java script and node.js
 
CorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. ProgrammingCorePy High-Productivity CellB.E. Programming
CorePy High-Productivity CellB.E. Programming
 
Process management
Process managementProcess management
Process management
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter ppt
 
Using R on Netezza
Using R on NetezzaUsing R on Netezza
Using R on Netezza
 
Handling Large State on BEAM
Handling Large State on BEAMHandling Large State on BEAM
Handling Large State on BEAM
 
Direct Code Execution @ CoNEXT 2013
Direct Code Execution @ CoNEXT 2013Direct Code Execution @ CoNEXT 2013
Direct Code Execution @ CoNEXT 2013
 
0.5mln packets per second with Erlang
0.5mln packets per second with Erlang0.5mln packets per second with Erlang
0.5mln packets per second with Erlang
 

More from ESUG

Workshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programmingWorkshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programmingESUG
 
Technical documentation support in Pharo
Technical documentation support in PharoTechnical documentation support in Pharo
Technical documentation support in PharoESUG
 
The Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and RoadmapThe Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and RoadmapESUG
 
Sequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in PharoSequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in PharoESUG
 
Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...ESUG
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsESUG
 
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6ESUG
 
A Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationA Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationESUG
 
Creating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingCreating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingESUG
 
Exploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportExploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportESUG
 
Pharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsPharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsESUG
 
Garbage Collector Tuning
Garbage Collector TuningGarbage Collector Tuning
Garbage Collector TuningESUG
 
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseImproving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseESUG
 
Pharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FuturePharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FutureESUG
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the DebuggerESUG
 
Websockets for Fencing Score
Websockets for Fencing ScoreWebsockets for Fencing Score
Websockets for Fencing ScoreESUG
 
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScriptShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScriptESUG
 
Advanced Object- Oriented Design Mooc
Advanced Object- Oriented Design MoocAdvanced Object- Oriented Design Mooc
Advanced Object- Oriented Design MoocESUG
 
A New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsA New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsESUG
 
BioSmalltalk
BioSmalltalkBioSmalltalk
BioSmalltalkESUG
 

More from ESUG (20)

Workshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programmingWorkshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programming
 
Technical documentation support in Pharo
Technical documentation support in PharoTechnical documentation support in Pharo
Technical documentation support in Pharo
 
The Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and RoadmapThe Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and Roadmap
 
Sequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in PharoSequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in Pharo
 
Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early results
 
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
 
A Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationA Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test Generation
 
Creating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingCreating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic Programming
 
Exploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportExploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience Report
 
Pharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsPharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIs
 
Garbage Collector Tuning
Garbage Collector TuningGarbage Collector Tuning
Garbage Collector Tuning
 
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseImproving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
 
Pharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FuturePharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and Future
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the Debugger
 
Websockets for Fencing Score
Websockets for Fencing ScoreWebsockets for Fencing Score
Websockets for Fencing Score
 
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScriptShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
 
Advanced Object- Oriented Design Mooc
Advanced Object- Oriented Design MoocAdvanced Object- Oriented Design Mooc
Advanced Object- Oriented Design Mooc
 
A New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsA New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and Transformations
 
BioSmalltalk
BioSmalltalkBioSmalltalk
BioSmalltalk
 

Recently uploaded

Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfIdiosysTechnologies1
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 

Recently uploaded (20)

Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Best Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdfBest Web Development Agency- Idiosys USA.pdf
Best Web Development Agency- Idiosys USA.pdf
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 

Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes

  • 1. Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes Dave Mason Toronto Metropolitan University ©2023 Dave Mason
  • 2. Execution Models Source Interpretation Bytecode Interpretation Threaded Execution Hardware Interpretation
  • 3. Timing of Fibonacci Thread CPS Object NativePharo JIT 169 278 347 1,094 559 186 304 391 1,918 591 136 332 564 2,010 695 Apple M1 Apple M2 Pro Intel i7 2.8GHz Threaded is 2.3-4.7 times faster than bytecode.
  • 4. Zag Smalltalk supports 2 models: threaded and native CPS seamless transition threaded is smaller and fully supports step-debugging native is 3-5 times faster and can fallback to full debugging after a send
  • 5. Threaded Execution sequence of addresses of native code like an extensible bytecode each word passes control to the next associated with Forth, originally in PDP-11 FORTRAN compiler, was used in BrouHaHa
  • 6. fibonacci 1 fibonacci 2 self <= 2 ifTrue: [ ↑ 1 ]. 3 ↑ (self - 1) fibonacci + (self - 2) fibonacci
  • 7. Threaded fibonacci 1 verifySelector, 2 ":recurse", 3 dup, // self 4 pushLiteral, Object.from(2), 5 p5, // <= 6 ifFalse,"label3", 7 drop, // self 8 pushLiteral1, 9 returnNoContext, 10 ":label3", 11 pushContext,"^", 12 pushLocal0, // self 13 pushLiteral1, 14 p2, // - 15 callRecursive, "recurse", 16 pushLocal0, //self 17 pushLiteral2, 18 p2, // - 19 callRecursive, "recurse", 20 p1, // + 21 returnTop,
  • 8. Some control words 1 pub fn drop(pc:PC, sp:Stack, process:*Process, context:ContextPtr, selector:Object) Stack { 2 tailcall pc[0].prim(pc+1, sp+1, process, context, selector, cache); 3 } 4 pub fn dup(pc:PC, sp:Stack, process:*Process, context:ContextPtr, selector:Object) Stack { 5 const newSp = sp-1; 6 newSp[0] = newSp[1]; 7 tailcall pc[0].prim(pc+1, newSp, process, context, selector, cache); 8 } 9 pub fn ifFalse(pc:PC, sp:Stack, process:*Process, context:ContextPtr, selector:Object) Stack { 10 const v = sp[0]; 11 if (False.equals(v)) tailcall branch(pc, sp+1, process, context, selector, cache ); 12 if (True.equals(v)) tailcall pc[1].prim(pc+2, sp+1, process, context, selector, cache ); 13 @panic("non boolean"); 14 } 15 pub fn p1(pc:PC, sp:Stack, process:*Process, context:ContextPtr, selector:Object) Stack { 16 if (!Sym.@"+".selectorEquals(selector)) tailcall dnu(pc, sp, process, context, selector); 17 sp[1] = inlines.p1(sp[1], sp[0]) 18 catch tailcall pc[0].prim(pc+1, sp, process, context, selector, cache); 19 tailcall context.npc(context.tpc, sp+1, process, context, selector, cache); 20 }
  • 9. Continuation Passing Style continuation is the rest of the program comes from optimization of functional languages (continuation was a closure) no implicit stack frames - passed explicitly like the original Smalltalk passing Context (maybe not obvious that Context is a special kind of closure)
  • 10. Normal Style 1 pub fn fibNative(self: i64) i64 { 2 if (self <= 2) return 1; 3 return fibNative(self - 1) + fibNative(self - 2); 4 } 5 const one = Object.from(1); 6 const two = Object.from(2); 7 pub fn fibObject(self: Object) Object { 8 if (i.p5N(self,two)) return one; 9 const m1 = i.p2L(self, 1) catch @panic("int subtract failed in fibObject"); 10 const fm1 = fibObject(m1); 11 const m2 = i.p2L(self, 2) catch @panic("int subtract failed in fibObject"); 12 const fm2 = fibObject(m2); 13 return i.p1(fm1, fm2) catch @panic("int add failed in fibObject"); 14 }
  • 11. Continuation Passing Style 1 pub fn fibCPS(pc:PC, sp:Stack, process:*Process, context:ContextPtr, selector:Object) Stack { 2 if (!fibSym.equals(selector)) tailcall dnu(pc,sp,process,context,selector); 3 if (inlined.p5N(sp[0],Object.from(2))) { 4 sp[0] = Object.from(1); 5 tailcall context.npc(context.tpc,sp,process,context,selector); 6 } 7 const newContext = context.push(sp,process,fibThread.asCompiledMethodPtr(),0,2,0); 8 const newSp = newContext.sp(); 9 newSp[0]=inlined.p2L(sp[0],1) 10 catch tailcall pc[10].prim(pc+11,newSp+1,process,context,fibSym); 11 newContext.setReturnBoth(fibCPS1, pc+13); // after first callRecursive (line 15 above) 12 tailcall fibCPS(fibCPST+1,newSp,process,newContext,fibSym); 13 }
  • 12. Continuation Passing Style 1 fn fibCPS1(pc:PC, sp:Stack, process:*Process, context:ContextPtr, _:Object) Stack { 2 const newSp = sp.push(); 3 newSp[0] = inlined.p2L(context.getTemp(0),2) 4 catch tailcall pc[0].prim(pc+1,newSp,process,context,fibSym)); 5 context.setReturnBoth(fibCPS2, pc+3); // after 2nd callRecursive (line 19 above) 6 tailcall fibCPS(fibCPST+1,newSp,process,context,fibSym); 7 }
  • 13. Continuation Passing Style 1 fn fibCPS2(pc:PC, sp:Stack, process:*Process, context:ContextPtr, selector:Object) Stack { 2 const sum = inlined.p1(sp[1],sp[0]) 3 catch tailcall pc[0].prim(pc+1,sp,process,context,fibSym); 4 const result = context.pop(process); 5 const newSp = result.sp; 6 newSp.put0(sum); 7 const callerContext = result.ctxt; 8 tailcall callerContext.npc(callerContext.tpc,newSp,process,callerContext,selector); 9 }
  • 14. Implementation Decisions Context must contain not only native return points, but also threaded return points; CompiledMethods must facilitate seamless switching between execution modes; the stack cannot reasonably be woven into the hardware stack with function calls, so no native stack; as well as parameters, locals, and working space, stack is used to allocate Context and BlockClosure as usually released in LIFO pattern
  • 15. Conclusions with proper structures, can easily switch between threaded and native code threaded code is “good enough” for many purposes this is preliminary work, so some open questions many experiments to run to validate my intuitions many more details in the paper
  • 17. Timing of Fibonacci Thread CPS P-JIT P-Stack Z-Byte 347 1,094 559 5,033 4,730 391 1,918 591 3,527 4,820 564 2,010 695 3,568 4,609 Apple M1 Apple M2 Pro Intel i7 2.8GHz M2 P-Stack is presumed to be mis-configured