Skip to content

refactor lexer and parser #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2025
Merged

refactor lexer and parser #19

merged 1 commit into from
Jul 19, 2025

Conversation

Schultzer
Copy link
Member

Closes #13

Most optimization have been made in the lexer, the parser deserves a more thoroughly refactor in the future.

We're now faster then Ecto in every single measurements, regardless of using compiletime optimizations or running the lexer and parser at runtime.

➜  sql git:(refactor-lexer-and-parser) ✗ mix sql.bench
Compiling 3 files (.ex)
Generated sql app
Operating System: macOS
CPU Information: Apple M1 Max
Number of Available Cores: 10
Available memory: 64 GB
Elixir 1.18.3
Erlang 27.3.3
JIT enabled: true

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 2 s
reduction time: 2 s
parallel: 1
inputs: Bigger, Medium, Small
Estimated total run time: 5 min 30 s

Benchmarking comptime ecto with input Bigger ...
Benchmarking comptime ecto with input Medium ...
Benchmarking comptime ecto with input Small ...
Benchmarking comptime inspect with input Bigger ...
Benchmarking comptime inspect with input Medium ...
Benchmarking comptime inspect with input Small ...
Benchmarking comptime to_sql with input Bigger ...
Benchmarking comptime to_sql with input Medium ...
Benchmarking comptime to_sql with input Small ...
Benchmarking comptime to_string with input Bigger ...
Benchmarking comptime to_string with input Medium ...
Benchmarking comptime to_string with input Small ...
Benchmarking lex with input Bigger ...
Benchmarking lex with input Medium ...
Benchmarking lex with input Small ...
Benchmarking parse with input Bigger ...
Benchmarking parse with input Medium ...
Benchmarking parse with input Small ...
Benchmarking runtime ecto with input Bigger ...
Benchmarking runtime ecto with input Medium ...
Benchmarking runtime ecto with input Small ...
Benchmarking runtime inspect with input Bigger ...
Benchmarking runtime inspect with input Medium ...
Benchmarking runtime inspect with input Small ...
Benchmarking runtime to_sql with input Bigger ...
Benchmarking runtime to_sql with input Medium ...
Benchmarking runtime to_sql with input Small ...
Benchmarking runtime to_string with input Bigger ...
Benchmarking runtime to_string with input Medium ...
Benchmarking runtime to_string with input Small ...
Calculating statistics...
Formatting results...

##### With input Bigger #####
Name                         ips        average  deviation         median         99th %
comptime to_sql          22.12 M       45.21 ns ±30771.26%          42 ns          42 ns
comptime to_string       21.66 M       46.17 ns ±39873.39%          42 ns          42 ns
runtime to_string        21.53 M       46.45 ns ±41237.22%          42 ns          42 ns
runtime to_sql           20.13 M       49.67 ns ±52992.84%          42 ns          42 ns
runtime inspect           3.42 M      292.48 ns  ±9976.69%         208 ns         375 ns
comptime inspect          3.28 M      305.33 ns  ±9699.04%         208 ns         375 ns
parse                     1.13 M      885.10 ns  ±1634.29%         833 ns        1292 ns
lex                       0.22 M     4454.64 ns   ±208.43%        4334 ns        5125 ns
comptime ecto             0.21 M     4821.99 ns   ±209.29%        4625 ns        6333 ns
runtime ecto             0.196 M     5110.23 ns   ±164.43%        4958 ns        7125 ns

Comparison: 
comptime to_sql          22.12 M
comptime to_string       21.66 M - 1.02x slower +0.96 ns
runtime to_string        21.53 M - 1.03x slower +1.24 ns
runtime to_sql           20.13 M - 1.10x slower +4.46 ns
runtime inspect           3.42 M - 6.47x slower +247.28 ns
comptime inspect          3.28 M - 6.75x slower +260.13 ns
parse                     1.13 M - 19.58x slower +839.89 ns
lex                       0.22 M - 98.54x slower +4409.43 ns
comptime ecto             0.21 M - 106.67x slower +4776.78 ns
runtime ecto             0.196 M - 113.04x slower +5065.02 ns

Memory usage statistics:

Name                  Memory usage
comptime to_sql               24 B
comptime to_string             0 B - 0.00x memory usage -24 B
runtime to_string              0 B - 0.00x memory usage -24 B
runtime to_sql                24 B - 1.00x memory usage +0 B
runtime inspect              664 B - 27.67x memory usage +640 B
comptime inspect             664 B - 27.67x memory usage +640 B
parse                       2600 B - 108.33x memory usage +2576 B
lex                        12232 B - 509.67x memory usage +12208 B
comptime ecto              18848 B - 785.33x memory usage +18824 B
runtime ecto               21336 B - 889.00x memory usage +21312 B

**All measurements for memory usage were the same**

Reduction count statistics:

Name               Reduction count
comptime to_sql                  2
comptime to_string               7 - 3.50x reduction count +5
runtime to_string                7 - 3.50x reduction count +5
runtime to_sql                   2 - 1.00x reduction count +0
runtime inspect                 44 - 22.00x reduction count +42
comptime inspect                44 - 22.00x reduction count +42
parse                           72 - 36.00x reduction count +70
lex                            178 - 89.00x reduction count +176
comptime ecto                 1132 - 566.00x reduction count +1130
runtime ecto                  1193 - 596.50x reduction count +1191

**All measurements for reduction count were the same**

##### With input Medium #####
Name                         ips        average  deviation         median         99th %
runtime to_string        24.94 M       40.09 ns ±21427.46%          42 ns          42 ns
comptime to_string       24.86 M       40.22 ns ±22164.38%          42 ns          42 ns
runtime to_sql           22.21 M       45.03 ns ±57571.17%          42 ns          42 ns
comptime to_sql          22.12 M       45.20 ns ±57895.72%          42 ns          42 ns
runtime inspect           3.47 M      288.05 ns  ±8359.97%         208 ns         375 ns
comptime inspect          3.39 M      294.83 ns  ±8262.90%         208 ns         375 ns
parse                     1.09 M      917.91 ns  ±2041.73%         833 ns        1292 ns
lex                       0.22 M     4490.99 ns   ±206.57%        4375 ns        6084 ns
comptime ecto             0.21 M     4823.12 ns   ±164.22%        4625 ns        6167 ns
runtime ecto             0.195 M     5134.28 ns   ±166.28%        4958 ns        7250 ns

Comparison: 
runtime to_string        24.94 M
comptime to_string       24.86 M - 1.00x slower +0.126 ns
runtime to_sql           22.21 M - 1.12x slower +4.93 ns
comptime to_sql          22.12 M - 1.13x slower +5.11 ns
runtime inspect           3.47 M - 7.18x slower +247.96 ns
comptime inspect          3.39 M - 7.35x slower +254.74 ns
parse                     1.09 M - 22.89x slower +877.82 ns
lex                       0.22 M - 112.01x slower +4450.89 ns
comptime ecto             0.21 M - 120.29x slower +4783.02 ns
runtime ecto             0.195 M - 128.05x slower +5094.18 ns

Memory usage statistics:

Name                  Memory usage
runtime to_string              0 B
comptime to_string             0 B - 1.00x memory usage +0 B
runtime to_sql                24 B - ∞ x memory usage +24 B
comptime to_sql               24 B - ∞ x memory usage +24 B
runtime inspect              664 B - ∞ x memory usage +664 B
comptime inspect             664 B - ∞ x memory usage +664 B
parse                       2600 B - ∞ x memory usage +2600 B
lex                        12232 B - ∞ x memory usage +12232 B
comptime ecto              18848 B - ∞ x memory usage +18848 B
runtime ecto               21336 B - ∞ x memory usage +21336 B

**All measurements for memory usage were the same**

Reduction count statistics:

Name               Reduction count
runtime to_string                7
comptime to_string               7 - 1.00x reduction count +0
runtime to_sql                   2 - 0.29x reduction count -5
comptime to_sql                  2 - 0.29x reduction count -5
runtime inspect                 44 - 6.29x reduction count +37
comptime inspect                44 - 6.29x reduction count +37
parse                           72 - 10.29x reduction count +65
lex                            178 - 25.43x reduction count +171
comptime ecto                 1132 - 161.71x reduction count +1125
runtime ecto                  1193 - 170.43x reduction count +1186

**All measurements for reduction count were the same**

##### With input Small #####
Name                         ips        average  deviation         median         99th %
comptime to_string       23.27 M       42.97 ns ±27691.14%          42 ns          42 ns
runtime to_string        23.10 M       43.29 ns ±27604.67%          42 ns          42 ns
comptime to_sql          21.53 M       46.44 ns ±38085.15%          42 ns          42 ns
runtime to_sql           21.21 M       47.14 ns ±39133.96%          42 ns          42 ns
comptime inspect          3.38 M      295.44 ns  ±8230.59%         208 ns         375 ns
runtime inspect           3.36 M      297.49 ns  ±8249.99%         208 ns         375 ns
parse                     1.09 M      918.27 ns  ±2024.90%         833 ns        1292 ns
lex                       0.22 M     4493.37 ns   ±216.96%        4375 ns        6125 ns
comptime ecto             0.21 M     4834.18 ns   ±149.64%        4666 ns        6250 ns
runtime ecto             0.194 M     5147.07 ns   ±167.87%        4958 ns        7792 ns

Comparison: 
comptime to_string       23.27 M
runtime to_string        23.10 M - 1.01x slower +0.32 ns
comptime to_sql          21.53 M - 1.08x slower +3.47 ns
runtime to_sql           21.21 M - 1.10x slower +4.17 ns
comptime inspect          3.38 M - 6.88x slower +252.48 ns
runtime inspect           3.36 M - 6.92x slower +254.52 ns
parse                     1.09 M - 21.37x slower +875.30 ns
lex                       0.22 M - 104.57x slower +4450.40 ns
comptime ecto             0.21 M - 112.50x slower +4791.21 ns
runtime ecto             0.194 M - 119.79x slower +5104.10 ns

Memory usage statistics:

Name                  Memory usage
comptime to_string             0 B
runtime to_string              0 B - 1.00x memory usage +0 B
comptime to_sql               24 B - ∞ x memory usage +24 B
runtime to_sql                24 B - ∞ x memory usage +24 B
comptime inspect             664 B - ∞ x memory usage +664 B
runtime inspect              664 B - ∞ x memory usage +664 B
parse                       2600 B - ∞ x memory usage +2600 B
lex                        12232 B - ∞ x memory usage +12232 B
comptime ecto              18848 B - ∞ x memory usage +18848 B
runtime ecto               21336 B - ∞ x memory usage +21336 B

**All measurements for memory usage were the same**

Reduction count statistics:

Name               Reduction count
comptime to_string               7
runtime to_string                7 - 1.00x reduction count +0
comptime to_sql                  2 - 0.29x reduction count -5
runtime to_sql                   2 - 0.29x reduction count -5
comptime inspect                44 - 6.29x reduction count +37
runtime inspect                 44 - 6.29x reduction count +37
parse                           72 - 10.29x reduction count +65
lex                            278 - 39.71x reduction count +271
comptime ecto                 1200 - 171.43x reduction count +1193
runtime ecto                  1262 - 180.29x reduction count +1255

**All measurements for reduction count were the same**

@Schultzer Schultzer merged commit 8ffe269 into main Jul 19, 2025
4 checks passed
@Schultzer Schultzer deleted the refactor-lexer-and-parser branch July 19, 2025 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistency in identifier tokens
1 participant