Wednesday 22 May 2013

Memoization using jHighFun

Memoization in computer science is about memorizing the method calls along with input parameter(s) and result object in order to avoid the next invocation and returning the result from cache for given input parameter(s).

It helps achieve
  • Faster method execution response
  • Avoid the use of system resource(s) such as CPU cycles, external system calls if any etc. which in general method requires to complete execution
Any kind of software application written down into smaller re-usable modules would be able to benefit from memoization as probability of invocation of such modules and services they provide are higher.





Monday 11 March 2013

Multithreaded Higher Order Function Library for Java

jHighFun

If someone asked to switch back to Java after the experience in functional programming language such as Scala, the obvious answer would be "why? I am feeling quite ok here!"

I agree too. Functional programming languages are on boom because of the flexibility and many more  in built feature which if you want to achieve in Java will eventually lead to lots of boiler plate code and it  looks messier !

Out of the many features available, I found Higher Order Functions  amiable as they facilitate modular way of coding when it comes to define a logic on any sort of collections. Every time i code in Java, i miss them so much, so i made my mind to create a simple  and small library for Higher Order Function which will also have in built concurrent execution capabilities.

Use of Higher Order Functions have following advantages

•  Parameterizable parallelism
•  Improved program modularity
•  Scalable programming
•  Recursion
•  Zero run-time cost in most cases
•  Expressiveness of a visual syntax

So lets get started.

You can download library from here.

This library lets you use following functions.

Just do static import org.highfun.util.FunctionUtil into your java file start using them, be careful while using the overloaded methods which accepts "noOfThreads" as last argument and tries to execute function concurrently.

If you want to use your own custom thread pool, then mention it as value of Java system property "org.highfun.threadpool". The value of this property would be JNDI look up reference for Thread Pool(which implements ExecutorService) which can be managed outside this library. If not mentioned a default unbounded thread pool will be used.

The Github repo:-

https://github.com/corporatepiyush/jHighFun


Saturday 9 February 2013

Rich yet Simple business task framework for Java

RichBiz

This framework focuses on writing any kind of business logic to make it more maintainable, testable, reusable and real time. It essentially focuses on following points and promotes developer to adopt the same while writing the business logic of any kind of use case, preferably which executes at server side. 
  • Task Oriented Programming
  • Fork and Join
  • Asynchronous  Execution
  • Memoization
  • Intelligent Statistics

This framework will greatly simplify business logic code by java's custom annotation feature. I have applied all the standard practices which i have used in my experience as java developer 

Everyone is talking about web application performance, scalability, exposing responsive services and mighty tech talks on clustering and its adoption to achieve these things. But most of us could not observe the fact that “Scaling the application” is different than “Scaling the Code” and these two aspects have to work in association to achieve true scalability.

One could think of “Scaling the Application” by having multiple instances on same as well as different nodes. This will help you facilitate the execution of number of requests from different users. What about execution of single request? Does it get scaled when we add more nodes or CPU’s? No, it does not!  big NO !

When we add more nodes so to our cluster, we are making it respond to more number of request from user but then due to large network infrastructure and increasing complexity several unwanted delays get added to total response time for processing the end user request. One should expect that the execution time of a particular use case on cluster environment to be greater than execution time on development environment where almost everything is on local machine. This is due to old linear programming practices which assume single threaded execution of given code.

If someone really wants to scale the code, then he/she better focus on how can the underlying CPU cores could be utilized to produce maximum throughput. This is where concurrent programming practices such as fork-join comes into practice which suggests division of business use case into smaller meaningful independent task units and executing them concurrently. More the number of processor more is the concurrent execution of task which ultimately results in optimum use of production system hardware and better response time to end user.

Task Oriented Programming
Like in OOP it is recommended practice to have single responsibility to a class, in TOP it is recommended that a single Task should represent a meaningful undividable execution unit if it does not represent a Job itself.
Job or complex task in TOP is collection of tasks or other Jobs along with the precedence execution rule.
TOP focuses on breaking down big fat business logic into smaller, reusable Tasks and arranging them to construct a business logic flow which in turn could be reusable as a Job.

Fork and Join
This particular algorithm from concurrent programming practices helps execute two or more independent Tasks concurrently to reduce the execution time which in general is addition of execution time of each Task.

Asynchronous Execution
Not every Task or unit of code is necessary to execute immediately to produce meaningful outcome; hence execution of such Task can be delayed which drastically reduces the total execution time of Job.

Memoization
Memoization is an technique of memorizing the function calls with the given arguments so that memorized result will be returned  on next function invocation with same arguments instead of executing a function again which is computationally  or system resource intensive act.
Memory cache or Disk cache can be used according to the requirement. This technique acts a steroid for any sort of application when performance is key factor.

Intelligent Statistics
To promote continuous re-factoring of code base for optimal performance, it is good practice to record a code execution statistics. Execution statistics calculation and information should be saved asynchronously which ensures that there is no non-functional logic execution overhead.
Statistics can be used to produce Management Information Reports which is essential to business. Reports may reflect certain meaningful information such as the consumption of various business services during the specific time period.