High-performance sheet-defined functions in Excel - Peter Sestoft at Sems 2014
1. www.itu.dk 1
High-performance
sheet-defined functions
in spreadsheets
Peter Sestoft
IT University of Copenhagen
SEMS 2014-07-02
With thanks to Thomas S Iversen, Daniel Cortes, Morten Hansen,
Poul Serek, Morten Poulsen, Hui Xu, Mainul Liton, Poul Brønnum,
Tim Garbos, Kasper Videbæk, Jens Hamann, Jonas Druedahl Rask,
Simon Eikeland Timmermann
2. www.itu.dk 2
The trouble with functions
• One cannot define functions in a spreadsheet
• To define new functions, ”experts” use VBA
• Often very poorly, witness newsgroup
microsoft.public.excel.programming
• Many (Excel) built-in functions are bad:
– Week numbers: two kinds, but not ISO standard
• Possible answers to this mess:
– ”People should not use spreadsheets”
– ”Only computer scientists should define functions”
– ”All necessary functions should be built in”
– Or: Functions within the spreadsheet metaphor
(Nuñez 2000, Peyton-Jones et al 2003)
3. www.itu.dk
Problem example: Area of triangles
• Area of triangle with sides a, b, c is
SQRT(s(s-a)(s-b)(s-c)) where s = (a+b+c)/2
Either (1) compute s in column D:
or (2) try to inline s in the area formula:
Annoying intermediate result
Horrible and error-prone
5. www.itu.dk 5
How use sheet-defined functions
• Assumptions
– End-users understand spreadsheet models
– End-users do not understand VBA, C#, VB.NET, …
• Sheet-defined functions in the organization
– Models are developed in ordinary spreadsheets
– After a while functions are factored out of models
– Functions can be further developed interactively
– An organization can develop and share libraries
– Without preventing further evolution by users
• Works only if
– Sheet-defined functions are fast enough
7. Runtime code generation
=SQRT(a1*a1+a2*a2)
ldloc 2
ldloc 2
mul
ldloc 3
ldloc 3
mul
add
call Math.Sqrt
fldl 0xfffffff0(%ebp)
fldl 0xfffffff0(%ebp)
fmulp %st,%st(1)
fldl 0xffffffe8(%ebp)
fldl 0xffffffe8(%ebp)
fmulp %st,%st(1)
faddp %st,%st(1)
fsqrt
Spreadsheet
formula
.NET
bytecode
x86 machine
code
My
compiler
JIT
compiler
Result: A very
fast, portable
spreadsheet
implementation
8. www.itu.dk
New book (next month)
• Spreadsheet Implementation Technology,
MIT Press, August 2014
8
Peter Sestoft
Spreadsheet
Implementation
Technology
Basics and Extensions
Version 0.99.5 of 2014-05-10
The MIT Press
Cambridge, Massachusetts
London, England
• A standard spreadsheet
implementation
• Sheet-defined functions
• Examples
• Design choices
• Scalability and speed
• Implementation details
• Funcalc user manual
9. www.itu.dk 9
Example function: NORMDISTCDF
• Normal distribution N(0,1) cumulative distribution function
• As accurate as Excel’s built-in NORMSDIST(z), and faster
Input cell Output cell
11. Examples: Calendrical functions
• Excel’s calendar functions are poor
– Wrong before 1900, no ISO week numbers,
cannot easily find first Monday of month, Easter, …
• Easy to implement as sheet-defined functions
• Example: Easter in a given year (1400 ns/call):
By MSc students Xu and
Liton, following
Dershowitz & Reingold
(3rd ed, Cambridge UP)
Input: year
Output:
Easter
fixdate
• Some other functions:
– Fixdate to/from day-month-year
– Fixdate to/from ISO week and ISO year
– Last/nth Monday (etc) before given date
– First/nth Monday (etc) after given date
12. Higher-order functions:
Sheet-defined functions as values
• New built-ins to manipulate functions
– CLOSURE(“name”, a1, …) evaluates to a closure:
a partially applied sheet-defined function
– APPLY(f, b1, …) applies a function value
• Example function “ndie”, a general n-side die
• Defining & rolling 6-sided and 20-sided dice
Input cell
Output cell
13. www.itu.dk
Funsheet: Linking Excel and Funcalc
• Sheet-defined functions in Excel!
• Eikeland and Timmermann MSc, June 2014
• Via Excel DNA, an Excel-.NET bridge
• Generated code is as fast as Funcalc
• Call speed Excel -> Funcalc suffers from
general Excel slowness, 11 us/call or so
• Complete Funcalc functionality: DEFINE,
CLOSURE, APPLY, SPECIALIZE, BENCHMARK
• Prototype, so still a number of defects
13
14. www.itu.dk
TO DO: Validation
• Improve the Excel <-> Funcalc link
• Demonstrate one application area
• Fix obvious problems
• Perform development experiments
• Perform maintenance experiments
• ...
• But experiments is not my area of expertise
14