PHP's APC analogue for Ruby? - ruby-on-rails

PHP has different opcode caches like APC, Zend Optimizer to cache the code and dramatically speed things up. Does Ruby have something similar?

The default Ruby 1.9.x is based on a bytecode VM, in addition you have ruby implementations based on the Java Virtual Machine (JRuby) and LLVM (Rubinius and MacRuby). These will all do just-in-time compilation and other optimizations you'd expect from a modern VM.

Default production settings in Rails is:
config.cache_classes = true
which mean that code isn't reloaded after requests, therefore it's cached in memory.

As far as MRI is concerned, experimental bytecode caching has been released with Ruby 2.3.
All you need to do to make this feature enabled is just to require
'yomikomu' rubygem and set some environmental variables introduced at
here as you can find two export commands in the example above.
It may look a bit magical why VM-level bytecode cache is enabled only
by requiring 'yomikomu' rubygem. Koichi described about this at his
ticket.
Here is a quick benchmark result of current bytecode cache implementation. I used 'bundle version' command with benchmark-ips on Ubuntu machine. Source
The post also provides some benchmarks for this newly released functionality:
$ ruby measure.rb
Comparison:
yomikomu(fs): 5.0 i/s
yomikomanai: 3.6 i/s - 1.40x slower
Other ruby implementations might be able to take advantage of the platform native optimizations - eg. JRuby benefits from the performance benefits of JVM JIT.

Related

Use Lua sandbox with MediaWiki under Windows

I am running a MediaWiki (1.34.1) on a Windows server. The wiki contains some Lua modules which are executed by the Scribunto extension running Lua 5.1.4.
Up to now I am using the luastandalone but I would like to use the luasandbox engine (should be faster).
With the lastest PHP luasandbox release 4.0.2 PECL provides a Windows DLL (https://pecl.php.net/package/LuaSandbox/4.0.2/windows).
With this DLL is it possible to run luasandbox under Windows?
How can I install/configure the PHP/MediaWiki/Scribunto environment to use this DLL?
Yes, it is possible to use LuaSandbox under Windows with IIS, and is in fact an especially convenient way to do so, You simply install the necessary Lua binary as a PHP extension into your existing PHP interpreter (which you know is working, because MediaWiki is implemented in PHP).
I discuss this at some length in this conversation on the MediaWiki page Extension Talk:Scribunto but I'll provide the essentials here as well:
I did finally get Lua working under IIS on Windows 10, with PHP 8.0. The trick was, I abandoned the luaStandalone binary entirely, and instead downloaded the (just released less than 2 months ago) PHP luaSandbox extension from PECL:
https://pecl.php.net/package/LuaSandbox
Click on "DLL", then choose the build that matches your PHP install (for me it was PHP 8.0, x64, non-thread safe — the details are at the very top of the long, long output of php.exe -i from a command line), and download the provided zip file. After extraction, only two files are important:
php_luasandbox.dll, a PHP extension module that goes wherever the rest of your extensions are. (For me, C:\Program Files\PHP\v8.0\ext\.)
lua5.1.dll, an embeddable Lua interpreter that gets installed in the directory where the php.exe binary lives. (For me that was C:\Program Files\PHP\v8.0\, the parent directory of the extension location).
After that, just edit your php.ini to add:
extension=php_luasandbox.dll
and edit LocalSettings.php to include:
$wgScribuntoDefaultEngine = 'luasandbox';
(making sure to remove or comment out any lines about luaStandalone).
Relaunch IIS, and that should be that. If you have MediaWiki working at all, you've already got PHP running, so using Lua that way, as a PHP extension, just makes eminent amounts of sense.
As I note in the MediaWiki discussion, there's some degree of controversy over this because the Lua developers themselves are sort of down on the notion of a "sandboxed Lua". They do not believe it to be a technically viable method of restricting Lua's access to and consumption of system resources. But on Windows, most of the restrictions they recommend imposing on the standalone binary are not available from the OS anyway, making the situation even more confusing/unclear.

my ubuntu system getting stuck while starting elastic search service

I have a ubuntu 18.04 system with 4 gb ram and 500gb hard disk.I have installed elastic search in my system like "sudo apt-get install elasticsearch"
but while starting elastic search with command : sudo service elasticsearch start
My system getting stuck, I am unable to do any thing. how to fix this
Maybe I can answer this question as I have encountered this problem a while ago and have also resolved it after some R&D.My ROR application is Live and have more than 8000+ images and CPU Intensive as there is lot more happening in my server as every request uses LAT/LNG to serve a geo-calculated response.I was facing this low memory issue similarly like yours when i started and reindexed my data on elasticsearch.
My Learnings-
ElasticSearch is a monster that will eat up your ram.
No Matter how optimized your keywords are OR the data is, that you are pushing to the elasticSearch to get indexed, you must keep a default buffer of backup memory.
What I did...that resolved my issue...
Add a Swap space which acts like a backup memory in case your RAM is eaten up.
I also upgraded to 8GB HD to be on safer side.
Now since i have followed the above mentioned steps, i am not facing any low memory error despite the size of my indexex data becoming huge every month.Furthermore I am still good to go error free and memory related issues while indexing this huge data.
One more thing, which I recently did to improve memory consumption of my Rails app is injecting jmalloc inside my ruby 2.4.1.You can read more here but in simple words, It helps improve memory consumption of ruby apps.A copied explanation of Jmalloc would be like -
Ruby traditionally uses the C language function malloc to dynamically allocate, release, and re-allocate memory when storing objects. Jemalloc is a malloc(3) implementation developed by Jason Evans (hence the “je” initials at the start of malloc), which appears to be more effective at allocating memory compared to other allocators due to its focus on fragmentation avoidance and scalable concurrency support.
Below are my steps that I took to add Jmalloc into my existing Ruby 2.4.1 by reinstalling 2.4.1 on my Production server(after testing on staging/dev) using rvm.
===========CHECK RVM --
rvm info
============ check ruby version
ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
========== if ruby is installed, reinstall with Jemalloc and compile
=============REINSTALL RUBY WITH JAMALLOC
rvm reinstall 2.4.1 -C --with-jemalloc --disable-binary
============VERIFY JMALLOC IN THE BELOW LIST of ruby compiling list
ruby -r rbconfig -e "puts RbConfig::CONFIG['LIBS']"
-lpthread -ljemalloc -lgmp -ldl -lcrypt -lm
The Results of jmalloc being injected in ruby were just outstanding even after testing with 2000 random requests, my overall App memory was only 364MB and remained the same throughout by testing with below mentioned testing gems.
gem "memory_profiler"
gem "derailed_benchmarks"
hope it helps
CPU and (RAM)memory used by Elasticsearch is very high. So you can tell the elastic search that you can only use a specific amount of memory.
First go to /etc/elasticsearch/
Just see the directory structure and you will get a file named jvm.options
Just allocate the memory by adding 2 lines of code in jvm.options file:
-Xms1g -Xmx1g
Just restart your elasticsearch and enjoy it :)

What is the build tool for Ruby codes?

Actually when we write codes in Java, we use Maven for building java code similarly Adv Java uses Gradle and .Net code uses Ms Build. But when we write Ruby codes, how can we built that code.
Is it possible to deploy the code directly to ROR ??
Ruby is a interpreted language.
An interpreted language is a type of programming language for which
most of its implementations execute instructions directly and freely,
without previously compiling a program into machine-language
instructions. The interpreter executes the program directly,
translating each statement into a sequence of one or more subroutines,
and then into another language (often machine code).
While there are quite a few IDE's that make it faster or easier to write ruby code there is no compiler step.
Is it possible to deploy the code directly to ROR ??
The question is totally nonsensical since both Ruby on Rails and your application code are interpreted on the go.
You can run ruby code with the Ruby command:
ruby somefile.rb
Or an interactive prompt with irb.
You run RoR applications with rails server and an interactive console with rails console.
While Capistrano is a build tool it does not actually compile code. Rather it just automates the steps of deploying an application, and is an optional dependency usually used when you are deploying via SSH or FTP. Its better compared to other tools in dynamic languages such as Grunt (javascript) or Apache Ant. Capistrano is seldom used when deploying to SAAS platforms such as Heruko.
You need to built code in Java and .Net as these are compile type languages whereas Ruby is an interpreted language ie it will check the syntax errors or any other message whenever the code/file is being run explicitly.
While you can run the ruby file using
ruby filname.rb
Is it possible to deploy the code directly to ROR ??
To run your code locally using
rails s
And deploy the code using capistrano or other tools to some other server.
Hope it clarifies your question.

How to deploy an Agda library on Travis CI?

I've read the .travis.yml in the agda-stdlib project, while it's very different and complex from a simple library that was written in Agda purely (without those Haskell codes and Shell scripts).
I'm confused with the stdlib's .tarvis.yml. I've installed agda via cabal install, but the stdlib is trying to clone and compile Agda on Travis CI, and there're a lot of commands that seems to be irrealavent to building it.
Also, agda-stdlib seems to be available on Ubuntu's source. This could be the 3rd approach to install it.
Also, the stdlib doesn't have dependencies, but I have. I don't know how to add a dependency either.
Conclusion of my question:
In the 3 choices of installing agda listed above, which one should I choose?
How to add an dependency that let the agda compiler knows I'm actually using it?
The standard library is a bit of a special case: it evolves in lock-step with the development version of Agda. As such it is often the case that it cannot be compiled with a version of Agda readily available in your distribution of choice (e.g. because it uses syntax that was not available beforehand!) and it is forced to pull the latest version from github.
Installing Agda
If your library is compatible with a distributed version then it will be far simpler for you to simply pull it from the repositories via apt-get install agda.
Alternatively Scott Fleischman has a basic example on how to use a docker image to typecheck your development: https://github.com/scott-fleischman/agda-travis
Installing your dependencies
If your project relies on dependencies then you do need to install them. In practice it'll probably mean fetching a bunch of tarballs via wget, and having a ~/.agda/libraries pointing at their library files.
Cf. the manual on library management

Dealing with a large c++ library in a Rails deployment

I have a Rails project that is going to be using OpenCV, and it depends on a certain version of it (2.4.6.1).
I'm looking for deployment advice. The Ubuntu opencv package is an earlier version and therefore not suitable.
I can see a number of possibilities, but I'm trying to think of what will work best.
Just write it up in a README and expect people to follow it: download this, apt-get that, etc...
Add opencv, tagged at the version we need, as a git subtree, and include a Rake task to build it.
Write a script to download and compile the needed code.
Something else ?
None of them seem all that great, to tell the truth.
Can your application be made to work with OpenCV 2.4.2? That is available in Ubuntu 13.04, and you could request it be backported to 12.04. If not, you could update the source package to 2.4.6.1 (which would require learning about debian packaging but might not be too difficult since you would be modifying an existing package instead of starting from scratch), upload it to a PPA, and instruct your users on Ubuntu to install OpenCV from there. You could also package your rails application and put it in the PPA, which would make overall installation even easier.

Resources