[
https://issues.apache.org/jira/browse/KAFKA-3768?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15305523#comment-15305523
]
ASF GitHub Bot commented on KAFKA-3768:
---------------------------------------
GitHub user satendrakumar06 opened a pull request:
https://github.com/apache/kafka/pull/1444
Replace all pattern match on boolean value by if/else block.
Replaced all pattern match on boolean value by if/else block.
[KAFKA-3768](https://issues.apache.org/jira/browse/KAFKA-3768)
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/satendrakumar06/kafka KAFKA-3768
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/kafka/pull/1444.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #1444
----
commit 212327cba2387dbc0549e1a2e0c5387f430a7fc2
Author: Satendra kumar <[email protected]>
Date: 2016-05-28T18:03:58Z
Replace all pattern match on boolean value by if/elase block.
KAFKA-3768
----
> Replace all pattern match on boolean value by if/elase block.
> -------------------------------------------------------------
>
> Key: KAFKA-3768
> URL: https://issues.apache.org/jira/browse/KAFKA-3768
> Project: Kafka
> Issue Type: Improvement
> Reporter: Satendra Kumar
> Priority: Minor
> Original Estimate: 1h
> Remaining Estimate: 1h
>
> Scala recommend use if/else block instead of pattern match on boolean
> values.
> For example:
> {code:title=Comparasion.scala|borderStyle=solid}
> class Comparasion {
> def method1(flag: Boolean): String = {
> flag match {
> case true => "TRUE"
> case false => "FALSE"
> }
> }
> def method2(flag: Boolean): String = {
> if(flag) {
> "TRUE"
> }else {
> "FALSE"
> }
> }
> }
> {code}
> Byte code comparison between method1 and method2:
> scala>javap -cp Comparasion
> {code:title=Comparasion.class|borderStyle=solid}
> Compiled from "<console>"
> public class Comparasion {
> public java.lang.String method1(boolean);
> Code:
> 0: iload_1
> 1: istore_2
> 2: iconst_1
> 3: iload_2
> 4: if_icmpne 13
> 7: ldc #9 // String TRUE
> 9: astore_3
> 10: goto 21
> 13: iconst_0
> 14: iload_2
> 15: if_icmpne 23
> 18: ldc #11 // String FALSE
> 20: astore_3
> 21: aload_3
> 22: areturn
> 23: new #13 // class scala/MatchError
> 26: dup
> 27: iload_2
> 28: invokestatic #19 // Method
> scala/runtime/BoxesRunTime.boxToBoolean:(Z)Ljava/lang/Boolean;
> 31: invokespecial #23 // Method
> scala/MatchError."<init>":(Ljava/lang/Object;)V
> 34: athrow
> public java.lang.String method2(boolean);
> Code:
> 0: iload_1
> 1: ifeq 9
> 4: ldc #9 // String TRUE
> 6: goto 11
> 9: ldc #11 // String FALSE
> 11: areturn
> public Comparasion();
> Code:
> 0: aload_0
> 1: invokespecial #33 // Method
> java/lang/Object."<init>":()V
> 4: return
> }
> {code}
> method1 have 23 line of byte code and method2 have only 6 line byte code.
> Pattern match are more expensive comparison to if/else block.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)