This is an automated email from the ASF dual-hosted git repository.

mboehm7 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/systemml.git


The following commit(s) were added to refs/heads/master by this push:
     new 30d5c40  [MINOR] Avoid unnecessary overhead in createvar instructions
30d5c40 is described below

commit 30d5c408b900b1aa4c8ddeb2f1264b830f460a05
Author: Matthias Boehm <[email protected]>
AuthorDate: Thu May 14 18:20:17 2020 +0200

    [MINOR] Avoid unnecessary overhead in createvar instructions
    
    This patch makes a minor performance improvement to the createvar
    instruction execution (which happens for every non-scalar operator). In
    detail, the need for creating unique file names (from one instruction),
    led to unnecessary string concatenation and thus object allocation. We
    now reuse the existing thread-local string builders as used for
    instruction generation.
    
    On a special-case scenario with ~1M loop iterations over tiny data (100
    values), this patch improved the createvar overhead from 22.1s to 5.6s
    (and overall from 49s to 33s).
---
 .../sysds/runtime/instructions/InstructionUtils.java     |  8 ++++++++
 .../runtime/instructions/cp/VariableCPInstruction.java   | 16 ++++++++++------
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git 
a/src/main/java/org/apache/sysds/runtime/instructions/InstructionUtils.java 
b/src/main/java/org/apache/sysds/runtime/instructions/InstructionUtils.java
index a47d6de..f1c8dc6 100644
--- a/src/main/java/org/apache/sysds/runtime/instructions/InstructionUtils.java
+++ b/src/main/java/org/apache/sysds/runtime/instructions/InstructionUtils.java
@@ -1008,4 +1008,12 @@ public class InstructionUtils
                sb.append(inputs[inputs.length-1]);
                return sb.toString();
        }
+       
+       public static String concatStrings(String... inputs) {
+               StringBuilder sb = _strBuilders.get();
+               sb.setLength(0); //reuse allocated space
+               for( int i=0; i<inputs.length; i++ )
+                       sb.append(inputs[i]);
+               return sb.toString();
+       }
 }
diff --git 
a/src/main/java/org/apache/sysds/runtime/instructions/cp/VariableCPInstruction.java
 
b/src/main/java/org/apache/sysds/runtime/instructions/cp/VariableCPInstruction.java
index b550a8a..7c030c5 100644
--- 
a/src/main/java/org/apache/sysds/runtime/instructions/cp/VariableCPInstruction.java
+++ 
b/src/main/java/org/apache/sysds/runtime/instructions/cp/VariableCPInstruction.java
@@ -521,9 +521,8 @@ public class VariableCPInstruction extends CPInstruction 
implements LineageTrace
                                //(existing objects gets cleared through rmvar 
instructions)
                                String fname = getInput2().getName();
                                // check if unique filename needs to be 
generated
-                               if( Boolean.parseBoolean(getInput3().getName()) 
) {
-                                       fname = fname + '_' + 
_uniqueVarID.getNextID();
-                               }
+                               if( Boolean.parseBoolean(getInput3().getName()) 
)
+                                       fname = getUniqueFileName(fname);
                                MatrixObject obj = new 
MatrixObject(getInput1().getValueType(), fname);
                                //clone meta data because it is updated on 
copy-on-write, otherwise there
                                //is potential for hidden side effects between 
variables.
@@ -544,9 +543,8 @@ public class VariableCPInstruction extends CPInstruction 
implements LineageTrace
                                //(existing objects gets cleared through rmvar 
instructions)
                                String fname = getInput2().getName();
                                // check if unique filename needs to be 
generated
-                               if( Boolean.parseBoolean(getInput3().getName()) 
) {
-                                       fname = fname + '_' + 
_uniqueVarID.getNextID();
-                               }
+                               if( Boolean.parseBoolean(getInput3().getName()) 
)
+                                       fname = getUniqueFileName(fname);
                                CacheableData<?> obj = new 
TensorObject(getInput1().getValueType(), fname);
                                //clone meta data because it is updated on 
copy-on-write, otherwise there
                                //is potential for hidden side effects between 
variables.
@@ -560,6 +558,8 @@ public class VariableCPInstruction extends CPInstruction 
implements LineageTrace
                        }
                        else if( getInput1().getDataType() == DataType.FRAME ) {
                                String fname = getInput2().getName();
+                               if( Boolean.parseBoolean(getInput3().getName()) 
)
+                                       fname = getUniqueFileName(fname);
                                FrameObject fobj = new FrameObject(fname);
                                fobj.setMetaData((MetaData)metadata.clone());
                                fobj.setFileFormatProperties(_formatProperties);
@@ -1257,4 +1257,8 @@ public class VariableCPInstruction extends CPInstruction 
implements LineageTrace
                        || opcode == VariableOperationCode.CastAsDoubleVariable
                        || opcode == 
VariableOperationCode.CastAsBooleanVariable;
        }
+       
+       public static String getUniqueFileName(String fname) {
+               return InstructionUtils.concatStrings(fname, "_", 
String.valueOf(_uniqueVarID.getNextID()));
+       }
 }

Reply via email to