Skip to content

Instantly share code, notes, and snippets.

@isicju
Created July 3, 2022 23:45
Show Gist options
  • Save isicju/826550f27c21027d4ca3066f2d1e8536 to your computer and use it in GitHub Desktop.
Save isicju/826550f27c21027d4ca3066f2d1e8536 to your computer and use it in GitHub Desktop.
package old.keke;
import org.openjdk.jol.info.GraphLayout;
/*
last optimization - using short mask for booleans
*/
public class FootPrint {
static final short IS_MALE = 0, IS_EXISTENT_USER = 1, IS_KNOWN_CUSTOMER = 2, IS_BANNED_USER = 3,IS_PREMIUM_USER = 4,
IS_CACHED_USER = 5, IS_FOREIGN_CLIENT = 6, IS_EXTERNAL_USER = 7, IS_MASTER_CARD = 8;
public static void main(String[] args) {
/* converting all booleans to short using mask
short resultValue = convertToShort(true, false,true,
false,false,true,true,true,false);
boolean isMaleValue = convertToBoolean(resultValue, IS_MALE);
boolean isBannedUserValue = convertToBoolean(resultValue, IS_BANNED_USER);
System.out.println(resultValue);
System.out.println(Integer.toBinaryString(resultValue));
System.out.println(isMaleValue);
System.out.println(isBannedUserValue);*/
User user = new User(System.currentTimeMillis(), (short)13133, (short)10, (short)2331, (short) 229);
System.out.println(GraphLayout.parseInstance(user).totalSize());
}
public static short convertToShort(boolean isMale, boolean isExistentUser, boolean isKnownCustomer, boolean isBannedUser,
boolean isPremiumUser, boolean isCachedUser, boolean isForeignClient, boolean isExternalUser,
boolean isMasterCard){
short isMaleShort = (short) (isMale ? 1 : 0);
short isExistentUserShort = (short) (isExistentUser ? 1 : 0);
short isKnownCustomerShort = (short) (isKnownCustomer ? 1 : 0);
short isBannedUserShort = (short) (isBannedUser ? 1 : 0);
short isPremiumUserShort = (short) (isPremiumUser ? 1 : 0);
short isCachedUserShort = (short) (isCachedUser ? 1 : 0);
short isForeignClientShort = (short) (isForeignClient ? 1 : 0);
short isExternalUserShort = (short) (isExternalUser ? 1 : 0);
short isMasterCardShort = (short) (isMasterCard ? 1 : 0);
return (short) (isMaleShort << IS_MALE | isExistentUserShort << IS_EXISTENT_USER | isKnownCustomerShort << IS_KNOWN_CUSTOMER
| isBannedUserShort << IS_BANNED_USER | isPremiumUserShort << IS_PREMIUM_USER | isCachedUserShort << IS_CACHED_USER
| isForeignClientShort << IS_FOREIGN_CLIENT | isExternalUserShort << IS_EXTERNAL_USER | isMasterCardShort << IS_MASTER_CARD);
}
public static boolean convertToBoolean(short value, short flagPosition){
return (value >> flagPosition & 1) == 1;
}
static class User {
long dateOfBirth;
short fixedSalary;
short bonusPercent;
short cardNumber;
short allFlagsValues;
public User(long dateOfBirth, short fixedSalary, short bonusPercent, short cardNumber, short allFlagsValues) {
this.dateOfBirth = dateOfBirth;
this.fixedSalary = fixedSalary;
this.bonusPercent = bonusPercent;
this.cardNumber = cardNumber;
this.allFlagsValues = allFlagsValues;
}
}
/* Third optimization using narrow types: 40 bytes
static class User {
long dateOfBirth;
boolean isMale, isExistentUser, isKnownCustomer,
isBannedUser, isPremiumUser,
isCachedUser, isForeignUser;
short fixedSalary;
short bonusPercent;
boolean isExternalUser;
boolean isMaterCard;
short cardNumber;
public User(long dateOfBirth, boolean isMale, boolean isExistentUser, boolean isKnownCustomer, boolean isBannedUser, boolean isPremiumUser, boolean isCachedUser, boolean isForeignUser, short fixedSalary, short bonusPercent, boolean isExternalUser, boolean isMaterCard, short cardNumber) {
this.dateOfBirth = dateOfBirth;
this.isMale = isMale;
this.isExistentUser = isExistentUser;
this.isKnownCustomer = isKnownCustomer;
this.isBannedUser = isBannedUser;
this.isPremiumUser = isPremiumUser;
this.isCachedUser = isCachedUser;
this.isForeignUser = isForeignUser;
this.fixedSalary = fixedSalary;
this.bonusPercent = bonusPercent;
this.isExternalUser = isExternalUser;
this.isMaterCard = isMaterCard;
this.cardNumber = cardNumber;
}
}
*/
/* Second optimization - moving all fields in one single class total size - 64 shorts
public static void main(String[] args) {
User user = new User(new Date(System.currentTimeMillis()), false, false, false,
false, false, false, false, 1231, 12, false,
true, 3333);
System.out.println(GraphLayout.parseInstance(user).totalSize());
}
static class User {
java.sql.Date dateOfBirth;
boolean isMale, isExistentUser, isKnownCustomer,
isBannedUser, isPremiumUser,
isCachedUser, isForeignUser;
int fixedSalary;
int bonusPercent;
boolean isExternalUser;
boolean isMaterCard;
int cardNumber;
public User(Date dateOfBirth, boolean isMale, boolean isExistentUser, boolean isKnownCustomer, boolean isBannedUser, boolean isPremiumUser, boolean isCachedUser, boolean isForeignUser, int fixedSalary, int bonusPercent, boolean isExternalUser, boolean isMaterCard, int cardNumber) {
this.dateOfBirth = dateOfBirth;
this.isMale = isMale;
this.isExistentUser = isExistentUser;
this.isKnownCustomer = isKnownCustomer;
this.isBannedUser = isBannedUser;
this.isPremiumUser = isPremiumUser;
this.isCachedUser = isCachedUser;
this.isForeignUser = isForeignUser;
this.fixedSalary = fixedSalary;
this.bonusPercent = bonusPercent;
this.isExternalUser = isExternalUser;
this.isMaterCard = isMaterCard;
this.cardNumber = cardNumber;
}
}
* */
/*
first opimization - from wrappers to primitives total size 112
public static void main(String[] args) {
UserPaymentDetails userPaymentDetails = new UserPaymentDetails(true, 3333);
UserSalary userSalary = new UserSalary(1231, 12, false, userPaymentDetails);
User user = new User(new Date(System.currentTimeMillis()), false, false, false,
false, false, false, false, userSalary);
System.out.println(GraphLayout.parseInstance(userPaymentDetails).totalSize());
System.out.println(GraphLayout.parseInstance(userSalary).totalSize());
System.out.println(GraphLayout.parseInstance(user).totalSize());
}
static class User {
java.sql.Date dateOfBirth;
boolean isMale, isExistentUser, isKnownCustomer,
isBannedUser, isPremiumUser,
isCachedUser, isForeignUser;
UserSalary salary;
public User(Date dateOfBirth, boolean isMale, boolean isExistentUser, boolean isKnownCustomer, boolean isBannedUser, boolean isPremiumUser, boolean isCachedUser, boolean isForeignUser, UserSalary salary) {
this.dateOfBirth = dateOfBirth;
this.isMale = isMale;
this.isExistentUser = isExistentUser;
this.isKnownCustomer = isKnownCustomer;
this.isBannedUser = isBannedUser;
this.isPremiumUser = isPremiumUser;
this.isCachedUser = isCachedUser;
this.isForeignUser = isForeignUser;
this.salary = salary;
}
}
static class UserSalary {
int fixedSalary;
int bonusPercent;
boolean isExternalUser;
UserPaymentDetails userDetails;
public UserSalary(int fixedSalary, int bonusPercent, boolean isExternalUser, UserPaymentDetails userDetails) {
this.fixedSalary = fixedSalary;
this.bonusPercent = bonusPercent;
this.isExternalUser = isExternalUser;
this.userDetails = userDetails;
}
}
static class UserPaymentDetails {
boolean isMaterCard;
int cardNumber;
public UserPaymentDetails(boolean isMaterCard, int cardNumber) {
this.isMaterCard = isMaterCard;
this.cardNumber = cardNumber;
}
}
*/
/* Initial version: total size: 208
package old.keke;
import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.info.GraphLayout;
import org.openjdk.jol.vm.VM;
import java.sql.Date;
public class FootPrint {
public static void main(String[] args) {
UserPaymentDetails userPaymentDetails = new UserPaymentDetails(true, 3333);
UserSalary userSalary = new UserSalary(1231, 12, false, userPaymentDetails);
User user = new User(new Date(System.currentTimeMillis()), false, false, false,
false, false, false, false, userSalary);
System.out.println(GraphLayout.parseInstance(userPaymentDetails).totalSize());
System.out.println(GraphLayout.parseInstance(userSalary).totalSize());
System.out.println(GraphLayout.parseInstance(user).totalSize());
}
static class User {
java.sql.Date dateOfBirth;
Boolean isMale, isExistentUser, isKnownCustomer,
isBannedUser, isPremiumUser, isCachedUser, isForeignUser;
UserSalary salary;
public User(Date dateOfBirth, Boolean isMale, Boolean isExistentUser, Boolean isKnownCustomer, Boolean isBannedUser, Boolean isPremiumUser, Boolean isCachedUser, Boolean isForeignUser, UserSalary salary) {
this.dateOfBirth = dateOfBirth;
this.isMale = isMale;
this.isExistentUser = isExistentUser;
this.isKnownCustomer = isKnownCustomer;
this.isBannedUser = isBannedUser;
this.isPremiumUser = isPremiumUser;
this.isCachedUser = isCachedUser;
this.isForeignUser = isForeignUser;
this.salary = salary;
}
}
static class UserSalary {
Integer fixedSalary;
Integer bonusPercent;
Boolean isExternalUser;
UserPaymentDetails userDetails;
public UserSalary(Integer fixedSalary, Integer bonusPercent, Boolean isExternalUser, UserPaymentDetails userDetails) {
this.fixedSalary = fixedSalary;
this.bonusPercent = bonusPercent;
this.isExternalUser = isExternalUser;
this.userDetails = userDetails;
}
}
static class UserPaymentDetails {
Boolean isMaterCard;
Integer cardNumber;
public UserPaymentDetails(Boolean isMaterCard, Integer cardNumber) {
this.isMaterCard = isMaterCard;
this.cardNumber = cardNumber;
}
}
}
* */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment