zaach/jison — pull request #234 by RubenVerborgh (2014-08-17 15:33:43+00:00)
Title: Significantly decrease parser file size by compacting parser table

### Summary

**This pull request decreases the gzipped size of generated parsers with over 40%.**

### Problem

The largest part of a Jison-generated parser is its `table`: a large array containing objects with numeric keys and (arrays of) numeric values.

Two kinds of patterns occur frequently in such a table:

1. **repeated long numerical arrays**
_example:_ `tables = [{ 5: [1,3,4,6,7,8,15], 6: 7}, { 20: [1,3,4,6,7,8,15], 9: 8 }]`
2. **objects where all keys have the same value**
_example:_ `tables = [{ 5: [200,204], 17: [200,204], 20: [200,204], 21: [200,204] }]`

### Solution

I tackled the first case by storing frequently occurring arrays into **temporary variables**:
```
var a = [1,3,4,6,7,8,15],
tables = [{ 5: a, 6: 7 }, { 20: a, 9: 8 }];
```

I tackled the second case by creating such objects with an **auxiliary function `o`**:
```
var tables = [o([200,204], 5, 17, 20, 21)];
```

Not only does this lead to a significantly **decreased filesize** of the parser, it also leads to **less memory usage**, as it avoids having multiple copies of the same array in memory.

To support such chunks of reusable code, the `generateModule` function has been updated to return an object with `commonCode` and `moduleCode` (instead of only `moduleCode`).

### Results

A parser I am working on benefited significantly from the new table generation function:

- **before:** **173kb** _(generated)_, **155kb** _(minified)_, **36kb** _(gzipped)_
- **optimization 1:** **138kb** _(generated)_, **112kb** _(minified)_, **36kb** _(gzipped)_
- **optimizations 1 and 2:** **110kb** _(generated)_, **90kb** _(minified)_, **20kb** _(gzipped)_

The decrease from 36kb to 20kb is a **44% reduction**.