Dmytro P works with one of those developers who clearly started life in C/C++, and has never quite adapted to languages like Java. How can I tell?
Well, functions like this are a good hint:
private static int roundNextPow2(int x) {
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
return x;
}
This rounds the input up to the next power of two. It's called in this function:
public static int calculateMapSize(int size) {
int newMinSize = (int)((float)size / 0.75f);
newMinSize = roundNextPow2(newMinSize);
return newMinSize;
}
This function kicks the minimum size up a bit, and then rounds the value up to the nearest power of two, and you're probably asking yourself: "Why? Also, why is it called calculateMapSize? Java already resizes collections for you, you don't need to do it yourself. They... they didn't, did they?"
They did, in fact.
protected Map<String, CalendarImpl> ensureMapSize(int addSize) {
// calc size of map
int newMinSize = CollectionUtils.calculateMapSize((null == this.calendars ? 0 : this.calendars.size()) + addSize);
HashMap<String, CalendarImpl> newMap = new HashMap<String, CalendarImpl>(newMinSize);
// handle map
if (null != this.calendars) {
// move existing entries
for (String key : this.calendars.keySet()) {
newMap.put(key, this.calendars.get(key));
}
}
// return
return newMap;
}
This function takes an existing map, increases its size by addSize, does all the weird logic we saw above, and then creates a new empty map of the target size. Then it copies each key/value from the existing map (if there is one) into the new map.
Note also that this function copies from the existing map, but doesn't alter it- it returns a new map. That's important, because it ends up getting used to do things like this:
protected String[] doReadUsersAll(Json json, ITEM[] items) {
// read all users into map
JArray objUsers = (JArray)json.get("UsersAll").get("Users");
// get new map
Map<String, CalendarImpl> newMap = this.ensureMapSize(objUsers.getCount());
// loop and create calendar objects
String[] result = new String[12];
int k=0;
for (int i=0; i<objUsers.getCount(); i++) {
JObject objJson = (JObject)objUsers.get(i);
String id = objJson.get("ID").getStr();
result = ArrayUtil.ensureSize(result, String.class, k+1, 12);
result[k++] = id;
if (newMap.containsKey(id)) {
// add items we do not yet have
CalendarImpl knownCal = newMap.get(id);
UserImpl knownUser = (UserImpl)knownCal.getOwner();
knownUser.addItems(objJson, items);
} else {
// just add (but use the full current user if it's that)
//TODO Should really allow user object to know the exact timezone
UserImpl user = UserImpl.buildInstance(this.api, objJson, items).setNameFormat(this.nameFormat);
CalendarImpl cal = new CalendarImpl(user);
cal.setComparator(this.getComparator());
newMap.put(id, cal);
}
}
// move pointer
this.calendars = newMap;
// clear json to clear memory
json.clear();
// return
return Arrays.copyOfRange(result, 0, k);
}
This takes JSON data, which I'll add, is managed by their own home-brewed JSON library, loads it into a map while also putting every read ID into a string array which they also resize, ignoring the perfectly good auto-resizing Java collections.
This code clearly was written by somebody who wanted to call memcpy and didn't know what to do without it. You can also see the C/C++ influence in the comment // move pointer.
How much do you want to bet that the developer behind this also complained about how slow Java was? When you write code like this, how could it be slow?