Why are you interested in Pony? We bet at least a bit of your answer involves its promise of high-performance code. When it comes to writing code that runs fast, Pony sets you up for success in a way that few languages do. That said, you can still write slow code.
The patterns in this chapter aim to help teach you a variety of tricks to help you write fast code. Most of them will focus on one or both of the following:
- Limit memory allocations
- Limit the number of objects your create
Neither of these techniques is unique to Pony. They are both fairly standard means of getting more performance from programs in any language. How you go about that is often specific to individual languages. Here’s your entree getting the most out of Pony.
Problem Your code is performance sensitive and needs to make your String concatenation code as fast as possible.
Solution Replace any usage of String.add with String.append. Going from code like:
let output = file_name + ":" + file_linenum + ":" + file_linepos + ": " + msg to String.append where we pre-allocate the memory needed to hold our final string.
let output = recover String(file_name.size() + file_linenum.size() + file_linepos.
Problem Your code is performance sensitive and suffers from poor performance due to arrays being resized while in use.
Solution It’s common for new Pony programmers to create arrays that use the default allocation:
// allocate space for no entries let small: Array[U8] = Array[U8] Instead, preallocate more space than you expect to use:
// preallocate space for at least 2048 entries let not_so_small: Array[U8] = Array[U8](2048) Discussion Arrays are an incredibly flexible data structure.