Uploaded image for project: 'FreeSWITCH'
  1. FreeSWITCH
  2. FS-10496

Speedup JavaScript. Enabling Code Caching in mod_v8


    • Type: New Feature
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.8
    • Fix Version/s: 1.8.0
    • Component/s: mod_v8
    • Labels:
    • CPU Architecture:
    • Kernel:
    • Userland:
    • Distribution:
    • Distribution Version:
      Debian 8 jessie
    • Compiler:
    • FreeSWITCH GIT Revision:
    • GIT Master Revision hash::


      "V8 uses just-in-time compilation (JIT) to execute Javascript code. This means that immediately prior to running a script, it has to be parsed and compiled - which can cause considerable overhead. Code caching is a technique that lessens this overhead. When a script is compiled for the first time, cache data is produced and stored. The next time V8 needs to compile the same script, even in a different V8 instance, it can use the cache data to recreate the compilation result instead of compiling from scratch. As a result the script is executed much sooner."

      Since we switched to a newer version of libv8, which is currently 6.1.298 in master (5.6.326 on Windows), it is time to enable the Code Caching in mod_v8.

      This is an experimental feature disabled by default.

      There are two new parameters added to the v8.conf.xml:
          <!-- <param name="script-caching" value="enabled"/> -->
          <!-- <param name="cache-expires-sec" value="3600"/> -->

      Uncomment the lines to enable the Code Caching.
      If you set cache-expires-sec = 0 or leave it commented, a script's cache will never expire.

      A file's cache will expire in cache-expires-sec seconds. But you have to keep in mind, that once a script's cache is produced, it is stored in memory. V8 engine does NOT have smart technics to detect changes in your js files. The only way to let mod_v8 drop the cache earlier is the file length difference. So if you change your file and a cache was previously produced for it, the cache will NOT be replaced the file has the same lenght as before the change! Go change the length (put spaces) and it's cache will be rejected on next load.
      The Code Caching will NOT work for inline scripts in mod_v8, but for files only. Javascript files that you require/include are cached also.

      There is also a usefull "jsmon" API added to the mod_v8.
      The performance monitor is disabled by default to not be an overkill for your logs.
      You can enable it by simple "jsmon on" to see what is happening with your scripts, how the cache is produced/expired/rejected, what time it takes to load and run a script.

      What about the performance?

      Here is a small test I made. I took a 10mb long.js (heavy to parse doing almost nothing script)

      It took:
      3101ms to compile for the first time.
      4329ms to execute

      309ms to compile/load next time (using cache)
      1542ms to execute

      312ms to compile again (using cache)
      1518ms to execute

      Script was changed (a space added): v8 rejects the cache
      2363ms to compile rejecting the cache
      3571ms to execute

      3088ms to compule producing cache
      4322ms to execute

      313ms to compile using fresh cache
      1532ms to execute

      10 times faster on compiling and more than 2 times faster on execution!

      Here is an example of how to use the Performance Monitor (jsmon API):
      freeswitch@Andrey-PC> jsrun test.js


      2017-07-13 02:10:53.290497 [INFO] jsmain.cpp:2 === [JS TEST] ===
      2017-07-13 02:10:53.290497 [INFO] jsmain.cpp:3 Google V8 version: 5.6.326
      2017-07-13 02:10:53.290497 [INFO] jsmain.cpp:4 scriptPath: V:/FS/scripts

      freeswitch@Andrey-PC> jsmon

      USAGE: jsmon on|off

      freeswitch@Andrey-PC> jsmon on

      Performance monitor has been enabled.

      freeswitch@Andrey-PC> jsrun include.js


      2017-07-13 02:11:14.109688 [INFO] mod_v8.cpp:553 Javascript ['V:/FS/scripts/include.js'] cache was produced.
      2017-07-13 02:11:14.109688 [INFO] mod_v8.cpp:579 Javascript ['V:/FS/scripts/include.js'] loaded in 1000 microseconds.

      freeswitch@Andrey-PC> 2017-07-13 02:11:17.219866 [INFO] mod_v8.cpp:553 Javascript ['V:/FS/scripts/long.js'] cache was produced.
      2017-07-13 02:11:17.219866 [INFO] mod_v8.cpp:579 Javascript ['V:/FS/scripts/long.js'] loaded in 3076176 microseconds.
      2017-07-13 02:11:18.519940 [INFO] jsmain.cpp:1197847 TADA
      2017-07-13 02:11:18.519940 [INFO] mod_v8.cpp:812 Javascript execution time: 4413253 microseconds

      freeswitch@Andrey-PC> jsrun include.js


      2017-07-13 02:11:19.970023 [INFO] mod_v8.cpp:568 Javascript ['V:/FS/scripts/include.js'] execution using cache.
      2017-07-13 02:11:19.970023 [INFO] mod_v8.cpp:579 Javascript ['V:/FS/scripts/include.js'] loaded in 1000 microseconds.

      freeswitch@Andrey-PC> 2017-07-13 02:11:20.310043 [INFO] mod_v8.cpp:568 Javascript ['V:/FS/scripts/long.js'] execution using cache.
      2017-07-13 02:11:20.310043 [INFO] mod_v8.cpp:579 Javascript ['V:/FS/scripts/long.js'] loaded in 311018 microseconds.
      2017-07-13 02:11:21.560114 [INFO] jsmain.cpp:1197847 TADA
      2017-07-13 02:11:21.560114 [INFO] mod_v8.cpp:812 Javascript execution time: 1589091 microseconds

      freeswitch@Andrey-PC> jsrun include.js


      2017-07-13 02:11:47.569602 [INFO] mod_v8.cpp:526 Javascript ['V:/FS/scripts/include.js'] cache expired.
      2017-07-13 02:11:47.579603 [INFO] mod_v8.cpp:553 Javascript ['V:/FS/scripts/include.js'] cache was produced.
      2017-07-13 02:11:47.579603 [INFO] mod_v8.cpp:579 Javascript ['V:/FS/scripts/include.js'] loaded in 2000 microseconds.

      freeswitch@Andrey-PC> 2017-07-13 02:11:47.919622 [INFO] mod_v8.cpp:568 Javascript ['V:/FS/scripts/long.js'] execution using cache.
      2017-07-13 02:11:47.919622 [INFO] mod_v8.cpp:579 Javascript ['V:/FS/scripts/long.js'] loaded in 307017 microseconds.
      2017-07-13 02:11:49.139692 [INFO] jsmain.cpp:1197847 TADA
      2017-07-13 02:11:49.139692 [INFO] mod_v8.cpp:812 Javascript execution time: 1567089 microseconds

      freeswitch@Andrey-PC> jsmon off

      Performance monitor has been disabled.

      freeswitch@Andrey-PC> jsrun include.js


      freeswitch@Andrey-PC> 2017-07-13 02:12:03.500513 [INFO] jsmain.cpp:1197847 TADA




            • Assignee:
              mikej Mike Jerris
              andywolk Andrey Volk
            • Votes:
              0 Vote for this issue
              3 Start watching this issue


              • Created: