Skip to content

Commit

Permalink
[Performance] Improve serialization performance using Externalizable
Browse files Browse the repository at this point in the history
  • Loading branch information
rh-id committed Nov 17, 2021
1 parent de77fef commit f98d8e1
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 10 deletions.
52 changes: 44 additions & 8 deletions navigator/src/main/java/m/co/rh/id/anavigator/NavRoute.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
package m.co.rh.id.anavigator;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;

import m.co.rh.id.anavigator.component.NavPopCallback;
import m.co.rh.id.anavigator.component.StatefulViewFactory;

@SuppressWarnings("rawtypes")
public class NavRoute implements Serializable {
private final StatefulViewFactory statefulViewFactory;
private final NavPopCallback navPopCallback;
private final RouteOptions routeOptions;
private final StatefulView statefulView;
private final String routeName;
private final Serializable routeArgs;
private final String routeStateKey;
public class NavRoute implements Externalizable {
private StatefulViewFactory statefulViewFactory;
private NavPopCallback navPopCallback;
private RouteOptions routeOptions;
private StatefulView statefulView;
private String routeName;
private Serializable routeArgs;
private String routeStateKey;
private Serializable routeResult;

/**
* Do not use this in production.
* This constructor is meant for serialization purpose only.
*/
public NavRoute() {
// leave blank
}

NavRoute(StatefulViewFactory statefulViewFactory,
NavPopCallback navPopCallback,
RouteOptions routeOptions,
Expand Down Expand Up @@ -78,4 +90,28 @@ public Object getRouteResult() {
void setRouteResult(Serializable routeResult) {
this.routeResult = routeResult;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(statefulViewFactory);
out.writeObject(navPopCallback);
out.writeObject(routeOptions);
out.writeObject(statefulView);
out.writeObject(routeName);
out.writeObject(routeArgs);
out.writeObject(routeStateKey);
out.writeObject(routeResult);
}

@Override
public void readExternal(ObjectInput in) throws ClassNotFoundException, IOException {
statefulViewFactory = (StatefulViewFactory) in.readObject();
navPopCallback = (NavPopCallback) in.readObject();
routeOptions = (RouteOptions) in.readObject();
statefulView = (StatefulView) in.readObject();
routeName = (String) in.readObject();
routeArgs = (Serializable) in.readObject();
routeStateKey = (String) in.readObject();
routeResult = (Serializable) in.readObject();
}
}
53 changes: 51 additions & 2 deletions navigator/src/main/java/m/co/rh/id/anavigator/RouteOptions.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package m.co.rh.id.anavigator;

import java.io.Serializable;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class RouteOptions implements Serializable {
public class RouteOptions implements Externalizable {
/**
* Helper method to setup in and out animation for transition.
*
Expand Down Expand Up @@ -46,4 +49,50 @@ public Integer getPopEnterAnimationResId() {
public Integer getPopExitAnimationResId() {
return popExitAnimationResId;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
int nullInt = -1;
if (enterAnimationResId != null) {
out.writeInt(enterAnimationResId);
} else {
out.writeInt(nullInt);
}
if (exitAnimationResId != null) {
out.writeInt(exitAnimationResId);
} else {
out.writeInt(nullInt);
}
if (popEnterAnimationResId != null) {
out.writeInt(popEnterAnimationResId);
} else {
out.writeInt(nullInt);
}
if (popExitAnimationResId != null) {
out.writeInt(popExitAnimationResId);
} else {
out.writeInt(nullInt);
}
}

@Override
public void readExternal(ObjectInput in) throws IOException {
int nullInt = -1;
int tempIn = in.readInt();
if (tempIn != nullInt) {
enterAnimationResId = tempIn;
}
tempIn = in.readInt();
if (tempIn != nullInt) {
exitAnimationResId = tempIn;
}
tempIn = in.readInt();
if (tempIn != nullInt) {
popEnterAnimationResId = tempIn;
}
tempIn = in.readInt();
if (tempIn != nullInt) {
popExitAnimationResId = tempIn;
}
}
}

0 comments on commit f98d8e1

Please sign in to comment.