lrama icon indicating copy to clipboard operation
lrama copied to clipboard

Iterate over symbols to get the number to avoid array creation

Open krzysiek1507 opened this issue 2 years ago • 2 comments

A tiny optimisation that reduces the memory usage by eliminating an array that it not used later anyway.

For parsing spec/fixtures/context/basic.y it reduces one object:

Before:

Calculating -------------------------------------
              master   133.668k memsize (     2.481k retained)
                         1.976k objects (     1.000  retained)
                        50.000  strings (     1.000  retained)

After:

Calculating -------------------------------------
              master   133.348k memsize (     2.481k retained)
                         1.975k objects (     1.000  retained)
                        50.000  strings (     1.000  retained)

krzysiek1507 avatar Sep 14 '23 20:09 krzysiek1507

Hey @yui-knk is this PR worth merging?

krzysiek1507 avatar Oct 07 '23 12:10 krzysiek1507

@krzysiek1507 Thanks for your PR. However I think this is not bottleneck of current Lrama. If it's not bottleneck, I want to keep current codes because it's intuitive for me.

By the way, this is how to take profiling for real world use case, generating parser for ruby/ruby.

1. Create parse.tmp.y in ruby/ruby

$ ruby tool/id2token.rb parse.y > parse.tmp.y
$ cp parse.tmp.y dir/lrama/tmp

2. Enable Profiler

diff --git a/exe/lrama b/exe/lrama
index ba5fb06..2497178 100755
--- a/exe/lrama
+++ b/exe/lrama
@@ -3,4 +3,6 @@
 $LOAD_PATH << File.join(__dir__, "../lib")
 require "lrama"

-Lrama::Command.new.run(ARGV.dup)
+Lrama::Report::Profile.report_profile do
+  Lrama::Command.new.run(ARGV.dup)
+end

3. Run Lrama

$ bundle exec exe/lrama -d -o parse.tmp.c -h tmp/parse.tmp.y

4. Generate Flamegraph

$ stackprof --d3-flamegraph tmp/stackprof-cpu-myapp.dump > tmp/flamegraph.html

yui-knk avatar Oct 08 '23 08:10 yui-knk