[jira] [Updated] (OFBIZ-10343) Refactoring Variable Scope

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Updated] (OFBIZ-10343) Refactoring Variable Scope

Nicolas Malin (Jira)

     [ https://issues.apache.org/jira/browse/OFBIZ-10343?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Pradhan Yash Sharma updated OFBIZ-10343:
----------------------------------------
    Description:
Here is the detailed information about the things I am working on for performance optimization in our OFBiz code.
  
 *1.) Downsize Accessibility Scope*

I've tried to downsize accessibility scope of classes, interfaces, abstract class, declared member variables, enumerations, methods, and constructors to as minimum as possible as per OFBIz current implementation, still there is a lot of scope for improvement but it would require changes at the granular level. I've used [this|https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html] as my reference point. example:
  
{code:java}
public void noteKeyRemoval(UtilCache<K, V> cache, K key, V oldValue);{code}
{code:java}
void noteKeyRemoval(UtilCache<K, V> cache, K key, V oldValue); {code}
 
 *2.) Using Lambda Expressions*

Then tried to use lambda expressions on simple functional work to leverage implicit type of coding an example: 

 
{code:java}
Map<String, String> initParameters = new LinkedHashMap<>();
for (Element e : initParamList) {
    initParameters.put(e.getAttribute("name"), e.getAttribute("value"));
}{code}
{code:java}
Map<String, String> initParameters = initParamList.stream().collect(Collectors.toMap(e -> e.getAttribute("name"), e -> e.getAttribute("value"), (a, b) -> b, LinkedHashMap::new));{code}
Some of the key benefits of using lambdas will introduce [Functional style over Imperative style|https://stackoverflow.com/questions/2078978/functional-programming-vs-object-oriented-programming], we can use [method referencing|https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html], usage of aggregate operations, and it will help developers to write memory efficient code. 
  
 *3.) Using Type Inference*

Java uses type inference so to make code lightweight I've updated code constructs as shown in the example for more on this refer this [article|https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.htmlv].
  
{code:java}
Map<String, ? extends Object> systemProps = UtilGenerics.<String, Object> checkMap(System.getProperties());{code}
{code:java}
Map<String, ?> systemProps = UtilGenerics.checkMap(System.getProperties());{code}
 
 *4.) Use of single quote for character*

There is a significant usage of <"Single Character"> in the codebase for example:-
  
{code:java}
throw new GenericConfigException("Error opening file at location [" + fileUrl.toExternalForm() + "]", e);{code}
{code:java}
throw new GenericConfigException("Error opening file at location [" + fileUrl.toExternalForm() + ']', e);  "]" {code}
 

is comparatively slower than ']' Java internally uses Flyweight Design pattern to create String literals so for every call it will not create a new Object and used an existing one this will improve performance to some extent a study can be seen on [this|https://stackoverflow.com/questions/24859500/concatenate-char-literal-x-vs-single-char-string-literal-x] page.  
  
 *5.) Updated Variable Declaration*

Lastly some of the variable declarations is updated this doesn't create a huge difference but helps JVM at the from implicit conversion. 
  
{code:java}
private long cumulativeEvents = 0;{code}
 
{code:java}
private long cumulativeEvents = 0L;{code}
 

Based on above findings, I have done some code improvement and provided following patches.

*And need community help for reviewing these changes.*

Kindly provide any improvements or suggestion you have in mind :)

 

P.S. Apart from this I am also working on performance matrix and will share it soon.

  was:
Here is the detailed information about the things I am working on for performance optimization in our OFBiz code.
 
 *1.) Downsize Accessibility Scope*

I've tried to downsize accessibility scope of classes, interfaces, abstract class, declared member variables, enumerations, methods, and constructors to as minimum as possible as per OFBIz current implementation, still there is a lot of scope for improvement but it would require changes at the granular level. I've used [this|https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html] as my reference point. example:
 

 
{code:java}
public void noteKeyRemoval(UtilCache<K, V> cache, K key, V oldValue);{code}
 
{code:java}
void noteKeyRemoval(UtilCache<K, V> cache, K key, V oldValue); {code}
 
 *2.) Using Lambda Expressions*

Then tried to use lambda expressions on simple functional work to leverage implicit type of coding an example: 

 
{code:java}
Map<String, String> initParameters = new LinkedHashMap<>();
for (Element e : initParamList) {
initParameters.put(e.getAttribute("name"), e.getAttribute("value"));
}{code}
 
{code:java}
Map<String, String> initParameters = initParamList.stream().collect(Collectors.toMap(e -> e.getAttribute("name"), e -> e.getAttribute("value"), (a, b) -> b, LinkedHashMap::new));{code}
Some of the key benefits of using lambdas will introduce [Functional style over Imperative style|https://stackoverflow.com/questions/2078978/functional-programming-vs-object-oriented-programming], we can use [method referencing|https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html], usage of aggregate operations, and it will help developers to write memory efficient code. 
 
 *3.) Using Type Inference*

Java uses type inference so to make code lightweight I've updated code constructs as shown in the example for more on this refer this [article|https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.htmlv].
 
{code:java}
Map<String, ? extends Object> systemProps = UtilGenerics.<String, Object> checkMap(System.getProperties());{code}
{code:java}
Map<String, ?> systemProps = UtilGenerics.checkMap(System.getProperties());{code}
 
 *4.) Use of single quote for character*

There is a significant usage of <"Single Character"> in the codebase for example:-
 
{code:java}
throw new GenericConfigException("Error opening file at location [" + fileUrl.toExternalForm() + "]", e);{code}
{code:java}
 throw new GenericConfigException("Error opening file at location [" + fileUrl.toExternalForm() + ']', e);  "]" {code}
is comparatively slower than ']' Java internally uses Flyweight Design pattern to create String literals so for every call it will not create a new Object and used an existing one this will improve performance to some extent a study can be seen on [this|https://stackoverflow.com/questions/24859500/concatenate-char-literal-x-vs-single-char-string-literal-x] page.  
 
 *5.) Updated Variable Declaration*Lastly some of the variable declarations is updated this doesn't create a huge difference but helps JVM at the from implicit conversion.* 
 
 private long cumulativeEvents = 0;
private long cumulativeEvents = 0L;   - Based on above findings, I have done some code improvement and provided following patches. *And need community help for reviewing these changes.* Kindly provide any improvements or suggestion you have in mind :)

P.S. Apart from this I am also working on performance matrix and will share it soon.


> Refactoring Variable Scope  
> ----------------------------
>
>                 Key: OFBIZ-10343
>                 URL: https://issues.apache.org/jira/browse/OFBIZ-10343
>             Project: OFBiz
>          Issue Type: Improvement
>          Components: framework
>    Affects Versions: Trunk
>            Reporter: Pradhan Yash Sharma
>            Priority: Minor
>             Fix For: Trunk
>
>
> Here is the detailed information about the things I am working on for performance optimization in our OFBiz code.
>   
>  *1.) Downsize Accessibility Scope*
> I've tried to downsize accessibility scope of classes, interfaces, abstract class, declared member variables, enumerations, methods, and constructors to as minimum as possible as per OFBIz current implementation, still there is a lot of scope for improvement but it would require changes at the granular level. I've used [this|https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html] as my reference point. example:
>   
> {code:java}
> public void noteKeyRemoval(UtilCache<K, V> cache, K key, V oldValue);{code}
> {code:java}
> void noteKeyRemoval(UtilCache<K, V> cache, K key, V oldValue); {code}
>  
>  *2.) Using Lambda Expressions*
> Then tried to use lambda expressions on simple functional work to leverage implicit type of coding an example: 
>  
> {code:java}
> Map<String, String> initParameters = new LinkedHashMap<>();
> for (Element e : initParamList) {
>     initParameters.put(e.getAttribute("name"), e.getAttribute("value"));
> }{code}
> {code:java}
> Map<String, String> initParameters = initParamList.stream().collect(Collectors.toMap(e -> e.getAttribute("name"), e -> e.getAttribute("value"), (a, b) -> b, LinkedHashMap::new));{code}
> Some of the key benefits of using lambdas will introduce [Functional style over Imperative style|https://stackoverflow.com/questions/2078978/functional-programming-vs-object-oriented-programming], we can use [method referencing|https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html], usage of aggregate operations, and it will help developers to write memory efficient code. 
>   
>  *3.) Using Type Inference*
> Java uses type inference so to make code lightweight I've updated code constructs as shown in the example for more on this refer this [article|https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.htmlv].
>   
> {code:java}
> Map<String, ? extends Object> systemProps = UtilGenerics.<String, Object> checkMap(System.getProperties());{code}
> {code:java}
> Map<String, ?> systemProps = UtilGenerics.checkMap(System.getProperties());{code}
>  
>  *4.) Use of single quote for character*
> There is a significant usage of <"Single Character"> in the codebase for example:-
>   
> {code:java}
> throw new GenericConfigException("Error opening file at location [" + fileUrl.toExternalForm() + "]", e);{code}
> {code:java}
> throw new GenericConfigException("Error opening file at location [" + fileUrl.toExternalForm() + ']', e);  "]" {code}
>  
> is comparatively slower than ']' Java internally uses Flyweight Design pattern to create String literals so for every call it will not create a new Object and used an existing one this will improve performance to some extent a study can be seen on [this|https://stackoverflow.com/questions/24859500/concatenate-char-literal-x-vs-single-char-string-literal-x] page.  
>   
>  *5.) Updated Variable Declaration*
> Lastly some of the variable declarations is updated this doesn't create a huge difference but helps JVM at the from implicit conversion. 
>   
> {code:java}
> private long cumulativeEvents = 0;{code}
>  
> {code:java}
> private long cumulativeEvents = 0L;{code}
>  
> Based on above findings, I have done some code improvement and provided following patches.
> *And need community help for reviewing these changes.*
> Kindly provide any improvements or suggestion you have in mind :)
>  
> P.S. Apart from this I am also working on performance matrix and will share it soon.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)