Pas2js optimizations

From Free Pascal wiki
Jump to navigationJump to search


This page describes existing optimizations for the pas2js transpiler as well as feature requests.

Generated JavaScript

Existing optimizations

  • -O-: Disable all optimizations
  • -O1 : Level 1 optimizations (quick and debugger friendly)
  • -OoEnumNumbers[-]: write enum value as number instead of name. Default in -O1.
  • -OoRemoveNotUsedPrivates[-]: Default is enabled
  • -OoRemoveNotUsedDeclarations[-]: Default enabled for programs with -Jc
  • omit unneeded brackets in associative operations (a||b)||(c||d), (a&&b)&&(c&&d), (a|b)|c, (a&b)&c, (a^b)^c, (a+b)+c, (a-b)-c, (a*b)*c
  • Use {$modeswitch OmitRTTI} to treat published sections as public and thus remove them by WPO. Published section provide RTTI, so the compiler cannot know at compile time if a published identifier is needed, so it keeps it.

Feature requests

  • -O1 : Level 1 otpimizations, fast and debugger friendly
  • -O2 : Level 2 optimizations, slow and not debugger friendly
  • -O3 : Level 3 optimizations, slow and might break special code
  • -O4 : Level 4 optimizations, very slow and/or only for rare cases
  • move rtl.js functions to system.pp and let the WPO decide if they are needed
  • add $mod only if needed
  • add Self only if needed
  • use a number for small sets instead of JS objects
  • shortcut for test if a set is empty a=[] a<>[]
  • SetLength(scope.a,l) -> read scope only once, same for Include, Exclude, Inc, Dec, +=, -=, *=, /=
  • ECMAScript6: use const for local const
  • -O1: shortcut: a in [b] -> a=b
  • -O1: shortcut: [a]*b=[] -> not (a in b)
  • -O1: shortcut: [a]*b<>[] -> a in b
  • -O1: combine multiple var a=0,b=0
  • -O1: init a local var with the first assignment
  • -O1: skip clone array for new array and arraysetlength
  • -O1/-O2: autoinline
  • -O1: replace constant with literal only if reference is longer, e.g. use pas.mymath.tau instead of 6.2831853071795862.
  • -O1: replace constant expression with result, e.g. b:=1+2 -> b:=3
  • -O1: create constants for set literals in expressions [lit,lit]
  • -O1: no function Result var when assigned only once, e.g. Result = 3; return Result; -> return 3;
  • -O1: by reference: create getter/setter object only once
  • -O1: pass array element by reference: when index is constant, use that directly (i.e. no temporary variable)
  • -O1: var/out argument: create accessor as var outside loops and combine multiple to one
  • -O2: inline -Si
  • -O2: case-of with 6+ elements as binary tree
  • -O2: insert local/unit vars for global type references:
    • at start of intf var $r1=null;
    • at end of impl: $r1=path;
  • -O2 removeemptyprocs
  • -O2 skip dead code If(false){...}
  • -O2 CSE
  • -O3 DFA

Compiler itself

The compiler was deliberately written

  • in a modular fashion. Each part has its own test suite and is used by other projects.
  • can be used in a library, i.e. no restart required to compile another program.
  • can be used without a filesystem, i.e. in a browser
  • threadsafe, i.e. the compilation can run in a thread. Compiling a program with multiple threads is planned.
  • easy maintenance has higher priority than high speed and low memory