SlideShare a Scribd company logo
Scratching the Itch 
How to attack slow applications, with particular reference 
to Scratch on a Raspberry Pi 
tim Rowledge 
tim@rowledge.org 
for the Raspberry Pi Foundation 
ESUG ’14 Cambridge UK 
Scratch lives at scratch.mit.edu - though be warned, the latest version is not Smalltalk based at all. 
Raspberry Pi lives at www.raspberrypi.org 
tim lives at www.rowledge.org/tim
“How many of you like the Ocean?” 
For the full musical experience of the following five slides, see the first 35 seconds of https://www.youtube.com/watch?v=hQeeQjdU2Bk And then enjoy 
the rest of Steam Powered Giraffe.
“It’s pretty wet, huh?”
“And how many of you like Fishing?”
“That’s right, nobody. 
Because it’s Boring”
“But how many of you like fighting 
Skeleton Pirates on the back of Killer Whales?” 
–Steam Powered Giraffe
Making code better is that exciting! 
• Scratch is an important application, perhaps the 
most used Smalltalk code out there 
• Raspberry Pi is wildly popular as an educational 
tool, a toy, a personal dynamic medium 
• Scratch is slow on a Pi :-( 
• We need to fix that. 
• Saddle up my Orca! 
Scratch on Pi has probably over a million regular users. That’s a scary large number of customers.
• Squeak… isn’t that some sort of old fashioned 
interpreted language? 
• Scratch is highly graphical, so it must be slow 
because of poor graphics in Squeak 
• Pi is ARM cpu based, so can’t possibly be fast 
• Pi is cheap… yada-yada
We all know what to do 
• Dream up some benchmarks 
• Run some tests 
• Do a few profiles 
• Fix problems 
• Profit!!!! 
This is what we normally do, or at least what we get told to do. Like most of what we are taught, there is a lot more to it.
Except… 
• Benchmarks are hard to devise 
• Tests don’t, often 
• Profiles only show where time is spent, not if it is 
spent wisely 
• Fix which problems? Code quality, algorithm 
choice, system design… 
Benchmarks are a morass of bad code, stupid ideas, inadequate thinking, simplistic approaches and general wrongness. And then we add all the numbers 
and pretend it means something. 
Tests can be worse.Even well thought out tests go out of date. 
Profiling is perhaps the most misunderstood issue of all.
Specifically… 
• Scratch/Pi was claimed to spend most time doing 
bitblt. Not actually true even for a totally graphical 
test. 
• Code to do a simple swap between two backgrounds 
involved many bitblts, mostly of ‘paint’ type. 
• Surrounding graphic ‘frame’ used several complex 
translucency bitblts for a virtually invisible effect 
• Multiple (ab)uses of #isKindOf:. Sigh.
• Actual bitblt time was 22% with a 6fps display rate 
• Pre-composite graphics to avoid multiple paint 
• Dump invisible prettiness 
• Avoid #isKindOf: 
• 10fps 
Here are some example changes made in the early part of the project.
BitBlt actually is slow on a Pi 
• The Pi SoC was originally intended for phones - essentially an iPhone 1 
logic board 
• Memory bandwidth to the ARM is not huge 
• We improved bitblt performance by a large factor with carefully written 
ARM assembler to replace some important routines; thanks to Ben Avison 
• Use ARM PLD instruction to preload scanlines 
• Make better use of ARM magic than C compiler can typically manage plus 
some SIMD trickery. 
• A lot of 1@1 sized blts; turned out to be a way of testing for A touching B. 
Replace with a very simple single-pixel test prim. Good case of a better 
algorithm rather than makee-faster 
The ARM bitblt improvements are in the trunk VM code, as is the pixel testing prim.
They did what? 
• Scratch uses an interesting and clever execution simulator to run the scripts; 
kind of a virtual machine running on the virtual machine. 
• Some really, really bad code in some important places. 
• #isKindOf: all over the place plus lots of state based behaviour instead of 
class based, thus wasting the benefits of the message sending VM. 
• Specialise some key parts of the Scratch engine to know what they do and 
some important sections run 8x faster. Suddenly Asteroids is playable. 
• Still a lot that could be done in this area 
• number/string conversion/polymorphism within Scratch costs a lot of time 
• still at least 50 extraneous #isKindOf: at last count 
Scratch’s execution mechanism is an excellent example of a very clever idea that didn't get looked after as well as it deserved. We’re trying to fix that.
Script Editing 
• Users drag script elements from palette to editor and around editor to build 
and change a script 
• Dragging a large stack of script blocks involves creating a large bitmap, 
rendering all the blocks into it and then animating dragging a mostly 
transparent bitmap. 
• Added caching of the fiddly bits, rounded or <> ends of argument morphs to 
speed up this rendering 
• In many cases it’s almost as fast to (re)draw each script block individually at 
each step to save so much transparency work. 
• Perhaps we will change to dragging a symbolic script chunk 
• Changes in place improved the system from ‘unusable by my students’ to 
‘wow, amazing’
Moving to a modern image 
• Scratch was originally written in a 2.4 era image and much 
hacking was done 
• Lots has changed 
• importantly the old image could not run on latest VMs 
• StackVM as a first step; major work 
• Required extracting code from old system, porting 
forward to 4.5 image 
• Oh my - look at the list -> 
This is what happens when code gets left to mould. It becomes a major effort to bring it back to a modern world.
• Huge changes to Morph 
• Changes to event handling - #mouseDown: etc 
• Scratch relies upon recorded keyboard/mouse state as well as event changes 
• Rewrote (with much help from Nic Cellier) the core text scanning to remove 
pointless duplication and speed up quite a bit 
• #copy-ing chaos 
• Custom i18n solution 
• Message name clashes as facilities added to general Morph that were Scratch 
specific 
• Morphic layout changes 
• Morphic drag/drop infrastructure 
• Dictionary>>do: changed semantics! 
And here is a list of at least some of the problem areas that have been tackled.
Going Faster 
• NuScratch beta is now near final and runs on the StackVM 
• Next step is CogVM/ARMv6 
• Eliot is freakin’ amazing 
• Tantalisingly close to running in simulator 
• Huge amount of subtle tricky stuff 
• Sends, SICs, PICs and most returns done 
• One day, ARMv8
Where can I get this wonderful stuff? 
• Beta release for ARM is on 
• http://github.com/raspberrypi/scratch 
• Beta8 tgz is latest 
• extract the image and run on OS X, Win-whatever, 
other linuxen. 
• Feedback!

More Related Content

PPTX
Your Goat Anti-Fragiled My Snowflake! Demystifying DevOps Jargon (30 minute v...
PDF
PDF
Perl-Critic
PDF
Shortening the feedback loop: faster and better code development
PDF
Sensepost assessment automation
PDF
Optimizing thread performance for a genomics variant caller
PDF
Crafting interactive troubleshooting guides and team documentation for your K...
PPTX
EhTrace -- RoP Hooks
Your Goat Anti-Fragiled My Snowflake! Demystifying DevOps Jargon (30 minute v...
Perl-Critic
Shortening the feedback loop: faster and better code development
Sensepost assessment automation
Optimizing thread performance for a genomics variant caller
Crafting interactive troubleshooting guides and team documentation for your K...
EhTrace -- RoP Hooks

What's hot (11)

PDF
Porque Odeio Branches
PDF
Puppet Camp Atlanta 2014: DEV Toolsets for Ops (Beginner) -
PPTX
Amsterdam Meetup Presentation - Marts 2014
PDF
Opening up Open Source
PPTX
.NET, Alexa and me
PDF
React, London JS Meetup, 11 Aug 2015
PDF
Merb Camp Keynote
PDF
Multimedia Buzzword Bingo: Translating to English
PDF
Stackato v6
PPTX
ZN-2015
PPTX
Trunk Based Development in the Enterprise - Its Relevance and Economics
Porque Odeio Branches
Puppet Camp Atlanta 2014: DEV Toolsets for Ops (Beginner) -
Amsterdam Meetup Presentation - Marts 2014
Opening up Open Source
.NET, Alexa and me
React, London JS Meetup, 11 Aug 2015
Merb Camp Keynote
Multimedia Buzzword Bingo: Translating to English
Stackato v6
ZN-2015
Trunk Based Development in the Enterprise - Its Relevance and Economics
Ad

Similar to Scratching the itch, making Scratch for the Raspberry Pie (20)

PPT
Ites Scratch Software Review
PPTX
Scratch - Intuitive and Accessible Programming
PDF
LibraryLinkNJ - Scratch Slides Part 1
PDF
Guides To Analyzing WebKit Performance
PDF
My summary for cs001x computer science for beginners
PDF
Scratch middle school gifted
PPTX
Lecture 1.pptx
PDF
Learn scratch-programming-e book
PPTX
PDF
Scratch
PDF
Scratch
PDF
Raspberry pi history, tips and use case (coscup19)
PDF
Raspberry pi history, tips and use case
PPT
Introduction
PPTX
Presentation unit 2 programming
PPTX
STUDENTS COPY-COMPUTER 6-WK2-1STQTR-24-25.pptx WEEK 2 OF THE FIRST QUARTER
PDF
[1C5]Lessons from developing a web browser for raspberry pi
PDF
Getting-Started-With-Scratch-3.0 for kids programming
PPT
Scratch
PDF
scratch-3-tutorial.pdf
Ites Scratch Software Review
Scratch - Intuitive and Accessible Programming
LibraryLinkNJ - Scratch Slides Part 1
Guides To Analyzing WebKit Performance
My summary for cs001x computer science for beginners
Scratch middle school gifted
Lecture 1.pptx
Learn scratch-programming-e book
Scratch
Scratch
Raspberry pi history, tips and use case (coscup19)
Raspberry pi history, tips and use case
Introduction
Presentation unit 2 programming
STUDENTS COPY-COMPUTER 6-WK2-1STQTR-24-25.pptx WEEK 2 OF THE FIRST QUARTER
[1C5]Lessons from developing a web browser for raspberry pi
Getting-Started-With-Scratch-3.0 for kids programming
Scratch
scratch-3-tutorial.pdf
Ad

More from ESUG (20)

PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
PDF
Directing Generative AI for Pharo Documentation
PDF
Even Lighter Than Lightweiht: Augmenting Type Inference with Primitive Heuris...
PDF
Composing and Performing Electronic Music on-the-Fly with Pharo and Coypu
PDF
Gamifying Agent-Based Models in Cormas: Towards the Playable Architecture for...
PDF
Analysing Python Machine Learning Notebooks with Moose
PDF
FASTTypeScript metamodel generation using FAST traits and TreeSitter project
PDF
Migrating Katalon Studio Tests to Playwright with Model Driven Engineering
PDF
Package-Aware Approach for Repository-Level Code Completion in Pharo
PDF
Evaluating Benchmark Quality: a Mutation-Testing- Based Methodology
PDF
An Analysis of Inline Method Refactoring
PDF
Identification of unnecessary object allocations using static escape analysis
PDF
Control flow-sensitive optimizations In the Druid Meta-Compiler
PDF
Clean Blocks (IWST 2025, Gdansk, Poland)
PDF
Encoding for Objects Matters (IWST 2025)
PDF
Challenges of Transpiling Smalltalk to JavaScript
PDF
Immersive experiences: what Pharo users do!
PDF
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
PDF
Cavrois - an Organic Window Management (ESUG 2025)
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
Micromaid: A simple Mermaid-like chart generator for Pharo
Directing Generative AI for Pharo Documentation
Even Lighter Than Lightweiht: Augmenting Type Inference with Primitive Heuris...
Composing and Performing Electronic Music on-the-Fly with Pharo and Coypu
Gamifying Agent-Based Models in Cormas: Towards the Playable Architecture for...
Analysing Python Machine Learning Notebooks with Moose
FASTTypeScript metamodel generation using FAST traits and TreeSitter project
Migrating Katalon Studio Tests to Playwright with Model Driven Engineering
Package-Aware Approach for Repository-Level Code Completion in Pharo
Evaluating Benchmark Quality: a Mutation-Testing- Based Methodology
An Analysis of Inline Method Refactoring
Identification of unnecessary object allocations using static escape analysis
Control flow-sensitive optimizations In the Druid Meta-Compiler
Clean Blocks (IWST 2025, Gdansk, Poland)
Encoding for Objects Matters (IWST 2025)
Challenges of Transpiling Smalltalk to JavaScript
Immersive experiences: what Pharo users do!
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
Cavrois - an Organic Window Management (ESUG 2025)

Recently uploaded (20)

PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Nekopoi APK 2025 free lastest update
PPTX
ai tools demonstartion for schools and inter college
PDF
System and Network Administration Chapter 2
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
System and Network Administraation Chapter 3
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
Upgrade and Innovation Strategies for SAP ERP Customers
Which alternative to Crystal Reports is best for small or large businesses.pdf
ManageIQ - Sprint 268 Review - Slide Deck
ISO 45001 Occupational Health and Safety Management System
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Softaken Excel to vCard Converter Software.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Nekopoi APK 2025 free lastest update
ai tools demonstartion for schools and inter college
System and Network Administration Chapter 2
Odoo Companies in India – Driving Business Transformation.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How Creative Agencies Leverage Project Management Software.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
System and Network Administraation Chapter 3
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
L1 - Introduction to python Backend.pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development

Scratching the itch, making Scratch for the Raspberry Pie

  • 1. Scratching the Itch How to attack slow applications, with particular reference to Scratch on a Raspberry Pi tim Rowledge tim@rowledge.org for the Raspberry Pi Foundation ESUG ’14 Cambridge UK Scratch lives at scratch.mit.edu - though be warned, the latest version is not Smalltalk based at all. Raspberry Pi lives at www.raspberrypi.org tim lives at www.rowledge.org/tim
  • 2. “How many of you like the Ocean?” For the full musical experience of the following five slides, see the first 35 seconds of https://www.youtube.com/watch?v=hQeeQjdU2Bk And then enjoy the rest of Steam Powered Giraffe.
  • 4. “And how many of you like Fishing?”
  • 5. “That’s right, nobody. Because it’s Boring”
  • 6. “But how many of you like fighting Skeleton Pirates on the back of Killer Whales?” –Steam Powered Giraffe
  • 7. Making code better is that exciting! • Scratch is an important application, perhaps the most used Smalltalk code out there • Raspberry Pi is wildly popular as an educational tool, a toy, a personal dynamic medium • Scratch is slow on a Pi :-( • We need to fix that. • Saddle up my Orca! Scratch on Pi has probably over a million regular users. That’s a scary large number of customers.
  • 8. • Squeak… isn’t that some sort of old fashioned interpreted language? • Scratch is highly graphical, so it must be slow because of poor graphics in Squeak • Pi is ARM cpu based, so can’t possibly be fast • Pi is cheap… yada-yada
  • 9. We all know what to do • Dream up some benchmarks • Run some tests • Do a few profiles • Fix problems • Profit!!!! This is what we normally do, or at least what we get told to do. Like most of what we are taught, there is a lot more to it.
  • 10. Except… • Benchmarks are hard to devise • Tests don’t, often • Profiles only show where time is spent, not if it is spent wisely • Fix which problems? Code quality, algorithm choice, system design… Benchmarks are a morass of bad code, stupid ideas, inadequate thinking, simplistic approaches and general wrongness. And then we add all the numbers and pretend it means something. Tests can be worse.Even well thought out tests go out of date. Profiling is perhaps the most misunderstood issue of all.
  • 11. Specifically… • Scratch/Pi was claimed to spend most time doing bitblt. Not actually true even for a totally graphical test. • Code to do a simple swap between two backgrounds involved many bitblts, mostly of ‘paint’ type. • Surrounding graphic ‘frame’ used several complex translucency bitblts for a virtually invisible effect • Multiple (ab)uses of #isKindOf:. Sigh.
  • 12. • Actual bitblt time was 22% with a 6fps display rate • Pre-composite graphics to avoid multiple paint • Dump invisible prettiness • Avoid #isKindOf: • 10fps Here are some example changes made in the early part of the project.
  • 13. BitBlt actually is slow on a Pi • The Pi SoC was originally intended for phones - essentially an iPhone 1 logic board • Memory bandwidth to the ARM is not huge • We improved bitblt performance by a large factor with carefully written ARM assembler to replace some important routines; thanks to Ben Avison • Use ARM PLD instruction to preload scanlines • Make better use of ARM magic than C compiler can typically manage plus some SIMD trickery. • A lot of 1@1 sized blts; turned out to be a way of testing for A touching B. Replace with a very simple single-pixel test prim. Good case of a better algorithm rather than makee-faster The ARM bitblt improvements are in the trunk VM code, as is the pixel testing prim.
  • 14. They did what? • Scratch uses an interesting and clever execution simulator to run the scripts; kind of a virtual machine running on the virtual machine. • Some really, really bad code in some important places. • #isKindOf: all over the place plus lots of state based behaviour instead of class based, thus wasting the benefits of the message sending VM. • Specialise some key parts of the Scratch engine to know what they do and some important sections run 8x faster. Suddenly Asteroids is playable. • Still a lot that could be done in this area • number/string conversion/polymorphism within Scratch costs a lot of time • still at least 50 extraneous #isKindOf: at last count Scratch’s execution mechanism is an excellent example of a very clever idea that didn't get looked after as well as it deserved. We’re trying to fix that.
  • 15. Script Editing • Users drag script elements from palette to editor and around editor to build and change a script • Dragging a large stack of script blocks involves creating a large bitmap, rendering all the blocks into it and then animating dragging a mostly transparent bitmap. • Added caching of the fiddly bits, rounded or <> ends of argument morphs to speed up this rendering • In many cases it’s almost as fast to (re)draw each script block individually at each step to save so much transparency work. • Perhaps we will change to dragging a symbolic script chunk • Changes in place improved the system from ‘unusable by my students’ to ‘wow, amazing’
  • 16. Moving to a modern image • Scratch was originally written in a 2.4 era image and much hacking was done • Lots has changed • importantly the old image could not run on latest VMs • StackVM as a first step; major work • Required extracting code from old system, porting forward to 4.5 image • Oh my - look at the list -> This is what happens when code gets left to mould. It becomes a major effort to bring it back to a modern world.
  • 17. • Huge changes to Morph • Changes to event handling - #mouseDown: etc • Scratch relies upon recorded keyboard/mouse state as well as event changes • Rewrote (with much help from Nic Cellier) the core text scanning to remove pointless duplication and speed up quite a bit • #copy-ing chaos • Custom i18n solution • Message name clashes as facilities added to general Morph that were Scratch specific • Morphic layout changes • Morphic drag/drop infrastructure • Dictionary>>do: changed semantics! And here is a list of at least some of the problem areas that have been tackled.
  • 18. Going Faster • NuScratch beta is now near final and runs on the StackVM • Next step is CogVM/ARMv6 • Eliot is freakin’ amazing • Tantalisingly close to running in simulator • Huge amount of subtle tricky stuff • Sends, SICs, PICs and most returns done • One day, ARMv8
  • 19. Where can I get this wonderful stuff? • Beta release for ARM is on • http://github.com/raspberrypi/scratch • Beta8 tgz is latest • extract the image and run on OS X, Win-whatever, other linuxen. • Feedback!