Skip to content

Commit

Permalink
Merge pull request #164 from alkum/robohash-optimization
Browse files Browse the repository at this point in the history
Minor optimization to RoboHash template calculation
  • Loading branch information
chimp1984 authored Mar 29, 2022
2 parents f5bfe36 + af82b82 commit 3192ea7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ public static Image getImage(ByteArray pubKeyHash, boolean useCache) {
}

private static Image imageForHandle(Handle handle, Configuration configuration) {
long t0 = System.currentTimeMillis();
byte[] bucketValues = handle.bucketValues();
String[] paths = configuration.convertToFacetParts(bucketValues);
log.debug("Generated paths for RoboHash image in {} ms", System.currentTimeMillis() - t0); // typically <1ms
return ImageUtil.composeImage(paths, configuration.width(), configuration.height());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public class Set1Configuration implements Configuration {
};

private final static String[] FACET_PATH_TEMPLATES = new String[]{
"#ROOT#/#COLOR#/01Body/#COLOR#_body-#ITEM#.png",
"#ROOT#/#COLOR#/02Face/#COLOR#_face-#ITEM#.png",
"#ROOT#/#COLOR#/Mouth/#COLOR#_mouth-#ITEM#.png",
"#ROOT#/#COLOR#/Eyes/#COLOR#_eyes-#ITEM#.png",
"#ROOT#/#COLOR#/Accessory/#COLOR#_accessory-#ITEM#.png",
ROOT + "/#COLOR#/01Body/#COLOR#_body-#ITEM#.png",
ROOT + "/#COLOR#/02Face/#COLOR#_face-#ITEM#.png",
ROOT + "/#COLOR#/Mouth/#COLOR#_mouth-#ITEM#.png",
ROOT + "/#COLOR#/Eyes/#COLOR#_eyes-#ITEM#.png",
ROOT + "/#COLOR#/Accessory/#COLOR#_accessory-#ITEM#.png",
};

@Override
Expand All @@ -60,8 +60,7 @@ public String[] convertToFacetParts(byte[] bucketValues) {
}

private String generatePath(String facetPathTemplate, String color, int bucketValue) {
// TODO: Make more efficient
return facetPathTemplate.replace("#ROOT#", ROOT)
return facetPathTemplate
.replaceAll("#COLOR#", color)
.replaceAll("#ITEM#", String.format("%02d", bucketValue + 1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public class Set2Configuration implements Configuration {
};

private final static String[] FACET_PATH_TEMPLATES = new String[]{
"#ROOT#/01FaceColors/final#ITEM#.png",
"#ROOT#/02BodyColors/final#ITEM#.png",
"#ROOT#/03Faces/final#ITEM#.png",
"#ROOT#/04Body/final#ITEM#.png",
"#ROOT#/Eyes/final#ITEM#.png",
"#ROOT#/Mouth/final#ITEM#.png",
"#ROOT#/Nose/final#ITEM#.png",
ROOT + "/01FaceColors/final#ITEM#.png",
ROOT + "/02BodyColors/final#ITEM#.png",
ROOT + "/03Faces/final#ITEM#.png",
ROOT + "/04Body/final#ITEM#.png",
ROOT + "/Eyes/final#ITEM#.png",
ROOT + "/Mouth/final#ITEM#.png",
ROOT + "/Nose/final#ITEM#.png",
};

@Override
Expand All @@ -42,7 +42,6 @@ public String[] convertToFacetParts(byte[] bucketValues) {
int bucketValue = bucketValues[i] + 1;
String facetPathTemplate = FACET_PATH_TEMPLATES[i];
paths[i] = facetPathTemplate
.replace("#ROOT#", ROOT)
.replaceAll("#ITEM#", String.valueOf(bucketValue));
}
return paths;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public class Set3Configuration implements Configuration {
};

private final static String[] FACET_PATH_TEMPLATES = new String[]{
"#ROOT#/01BaseFace/Robot-Design#ITEM#.png",
"#ROOT#/02Wave/wave#ITEM#.png",
"#ROOT#/03Antenna/Robot-Design#ITEM#.png",
"#ROOT#/04Eyes/Robot-Design#ITEM#.png",
"#ROOT#/05Eyebrows/Robot-Design#ITEM#.png",
"#ROOT#/06Nose/Robot-Design#ITEM#.png",
"#ROOT#/07Mouth/Robot-Design#ITEM#.png",
ROOT + "/01BaseFace/Robot-Design#ITEM#.png",
ROOT + "/02Wave/wave#ITEM#.png",
ROOT + "/03Antenna/Robot-Design#ITEM#.png",
ROOT + "/04Eyes/Robot-Design#ITEM#.png",
ROOT + "/05Eyebrows/Robot-Design#ITEM#.png",
ROOT + "/06Nose/Robot-Design#ITEM#.png",
ROOT + "/07Mouth/Robot-Design#ITEM#.png",
};

@Override
Expand All @@ -42,7 +42,6 @@ public String[] convertToFacetParts(byte[] bucketValues) {
int bucketValue = bucketValues[i] + 1;
String facetPathTemplate = FACET_PATH_TEMPLATES[i];
paths[i] = facetPathTemplate
.replace("#ROOT#", ROOT)
.replaceAll("#ITEM#", String.valueOf(bucketValue));
}
return paths;
Expand Down

0 comments on commit 3192ea7

Please sign in to comment.