use types;
YAPC::US 2011
rurban - Reini Urban <br> vienna.pm
, houston.pm
Outline
- Status Quo: CORE types
-
my TYPE $i; my My::Class $o;
<br>Declaration only. package TYPE must exist
- Artur Bergman's types module
-
Declare and check type safety. int, double, number, string. params and return values.
- Optimizations planned for 5.16
-
smaller and faster, ensured at compile-time. typed arrays + hashes, perfect hashes, fast method calls, type declarations via class and attributes, fix types.
Who am I
rurban active in p5p since 1995.
maintains cygwin perl since 5.8.8 and 3-4 modules: illguts, B::* => 5.10, mainly the "compiler".
In the future a lot more time for the compiler and CORE.
I'm known as the BAD guy on p5p who started fighting with Nick. Sorry about that. Sometimes I'm just arrogant and stupid, but unfortunately not as funny as Matt.
Perl's Type System
Yes, the language already has one.
Perl's Type System
Yes, the language already has one. CORE:
Only a few out-of-CORE implementations: <br>
Moose, fields, <strike>types, typesafety</strike>, Lexical::Types
Params: Devel::Declare
, Params::Validate
, Params::Classify
<br> Objects: Class::Meta
, Moose
Perl's Type System
At first a look CORE types, not the language:
SCALAR (non-strict, VB-like): IV, NV, UV, PV, ...
ARRAY (non-strict): AV
HASH (non-strict): HV
Objects ("stashes", @ISA, declaration)
Perl's Type System - SCALAR
SCALAR (non-strict, VB-like)
multiple types (IV,NV,PV) per variable, context dependent.
internally upgraded: IV => PVIV => PVNV
Perl's Type System - SCALAR
SCALAR (non-strict, VB-like)
multiple types (IV,NV,PV) per variable, context dependent.
IV: integer
<img src=pix/sviv-14.png height=204px width=278px>
Perl's Type System - SCALAR
SCALAR (non-strict, VB-like)
multiple types (IV,NV,PV) per variable, context dependent.
IV - integer unblessed, untied
<img src=pix/sviv-14.png height=204px width=278px> => <img src=pix/sviv-new.png height=204px width=278px>
Perl's Type System - SCALAR
SCALAR (non-strict, VB-like)
multiple types (IV,NV,PV) per variable, context dependent.
internally upgraded: IV => NV
<img src=pix/svnv-14.png height=252px width=288px>
Perl's Type System - SCALAR
SCALAR (non-strict, VB-like)
multiple types (IV,NV,PV) per variable, context dependent.
internally upgraded: IV => NV => PVNV
<img src=pix/svpvnv-14.png height=264px width=552px>
Perl's Type System - SCALAR
SCALAR (non-strict, VB-like)
internally upgraded: IV => PVIV => PVNV => "Objects" => Tie
<img src=pix/svpvmg-14.png>
Perl's Type System - SCALAR
"STASH" - Hierarchical symbol table,<br> used as package name for Objects. i.e. "Class pointer"
<img src=pix/stash.png>
Perl's Type System - ARRAY
ARRAY (non-strict)
Keys are integers, Values any SCALAR
Perl's Type System - ARRAY
ARRAY (non-strict)
Keys are integers, Values any SCALAR
Perl's Type System - ARRAY
ARRAY (non-strict)
Keys are integers, Values any SCALAR - flexible, untyped, big
<img src=pix/av-sv.png>
Typed ARRAY
Typed ARRAYs (optional, faster, less space)
Keys are integers, Values of one type only (native)
Untyped:
Typed ARRAY
<img src=pix/av-typed.png>
AvTYPED && (IOK | NOK | POK)
Typed ARRAY
<p>
Declaration already possible now!
int must be a package definition.
<small>Note: @a
is declared as int
, but not blessed.</small>
Perl's Type System - HASH
HASH untyped: flexible, but big
Keys any SCALAR, Values any SCALAR
Internally: Keys are stringified.
Perl's Type System - HASH
HASH untyped: flexible<br> Keys any SCALAR, Values any SCALAR
<img src=pix/hv-sv.png>
Typed HASH
HASH typed: fast, small
Keys STRING only, Values typed
Typed HASH
HASH typed: directer access, small <br> Keys STRING only, Values typed
<img src=pix/hv-typed.png>
Typed HASH
HASH typed: fast, small
Keys STRING only, Values are typed.
Perfect HASH
Perfect hashes - guaranteed O(1) lookup, dynamic hash function generation
my %h :const;
=> untyped perfect hash of unknown size<br>my %h :perfect;
=> writable perfect hashstudy %h
; => optimize lookup function to perfect hash.
study untyped hash => copies the old perl hash (HV) to new perfect hash (PH).
:const
hashes are always :perfect
Perfect HASH
Perfect hash. Should be typed to optimize implementation.
No need to study with :const and init on declaration.
Perfect HASH Idioms
- Avoid copy from perl hash to perfect hash.
Perfect HASH Idioms
- Declare size in advance.
Perfect HASH Idioms
- :const hash with computed key => values, without study
Idea 1
Init until length is filled
Perfect HASH Idioms
- :const hash with computed key => values, without study
Idea 2
Initialization on next expression, usually a block.
Perl's Type System - OBJECTS
OBJECTS: typed, but dynamic
run-time changable, mostly no compile-time optimizations possible.
Features: STASH ("class hash"), @ISA (mro), DESTROY
Perl's Type System - OBJECTS
OBJECTS: typed, but dynamic.
Class by STASH, Inheritance by @ISA (mro), magic DESTROY methods.
Four method calls possible:
Compilation of a function
pushmark <br>
ARGS... <br>
gv => GV *Class::sub <br>
entersub
Class->method() <br> $object->method() <br> Class->$method() <br> $object->$method()
Compilation of a static method call
Class::sub() <br>
pushmark <br>
const => PV "Class" <br>
ARGS... <br>
method_named => PV "method" <br>
entersub
$object->method() <br> Class->$method() <br> $object->$method()
Compilation of a method call
pushmark <br>
padsv => GV *object <br>
ARGS... <br>
method_named => PV "method" <br>
entersub
Compilation of a method call
pushmark <br>
const => PV "Class" <br>
ARGS... <br>
method => GV *method <br>
entersub
Compilation of a method call
pushmark <br>
padsv => GV *object <br>
ARGS... <br>
method => GV *method <br>
entersub
Optimization of a static method call
pushmark <br>
const => PV "Class" <br>
ARGS... <br>
method_named => PV "method" <br>
entersub <br>
=> <br>
pushmark <br>
const => PV "Class" <br>
ARGS... <br>
gv => GV *Class::method <br>
entersub
Optimization of a static method call
or package Class
:locked
i.e. not changed at run-time.
Note: @Class::ISA
:const = qw(bla);
does not help.
Optimization of other method calls
Dynamic method calls are possible to optimize in a similar way, if the object is declared - known class at compile-time.
Inherited methods are optimizable if all classes in path to the finalclass are :locked, resp. immutable.
Summary so far
All this is possible now, without changing the language.
Just optimized implementations are missing.
I heard that in July 2011 Moose methods of immutable classes are going to be inlined, but what I saw so far it's not using optree changes like these to speed it up.
More optimizations
More compile-time optimizations.
:const for variables
:locked for packages: const @ISA
, no run-time created methods.
use types;
types is Artur Bergman's compile-time checking attempt from 2002, after the compiler, B::Generate and optimize.
And before optimizer, which uses types to improve the optree.
use types;
types does compile-time type-checking only.
compile-time type-optimizations in optimizer.
Problem: slower, not faster.
use types;
The idea is to make programs with use types;
faster, not slower.
And define basic scalar types from CORE
int
, double
and string
B::CC
The same types and declarations are used in B::CC also to optimize types even further.
B::CC - optimizing perl compiler
B::CC also needs a syntax to optionally declare simple types:
int and double (strict)
So far it was done by magic variable name suffices: $a_i
, $n_d
;
faster, much faster
B::CC - User Type declarations
Strict types as class, and optional type hints as attributes.
With use types
you get type-declarations, <br> partial type-safety, and <br> type optimizations at compile-time.