Created
April 12, 2013 12:54
-
-
Save jasononeil/5371801 to your computer and use it in GitHub Desktop.
A test of custom serialisation in Haxe when using sys.db.Object objects. Results in file below * Using a custom serialiser for SPOD can reduce speeds by more than half.
* String size is also reduced, which is good if you're sending over HTTP
* Have a build macro append a static array containing the names of the fields to serialise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys.db.Types; | |
using Lambda; | |
/** | |
* What this class shows: | |
* | |
* Using a custom serialiser for SPOD can reduce speeds by more than half. | |
* Have a build macro append a static array containing the names of the fields to serialise. | |
*/ | |
class CustomSerialisation | |
{ | |
static function main() | |
{ | |
setupDatabase(); | |
var obj1 = MyModelWithoutCustomSerialize.manager.get(12); | |
var obj2 = MyModelWithCustomSerialize.manager.get(12); | |
var s1:String = "", s2:String = ""; | |
// var u = haxe.Unserializer.run(s); | |
var start:Float, end:Float; | |
trace ("Serialise without custom"); | |
var start = Sys.time(); | |
for (i in 0...100000) | |
{ | |
s1 = haxe.Serializer.run(obj1); | |
} | |
var end = Sys.time(); | |
trace (' Time taken: ' + (end - start)); | |
trace (' Serialised String: $s1 [${s1.length}]'); | |
trace ("Serialise with custom"); | |
var start = Sys.time(); | |
for (i in 0...100000) | |
{ | |
s2 = haxe.Serializer.run(obj2); | |
} | |
var end = Sys.time(); | |
trace (' Time taken: ' + (end - start)); | |
trace (' Serialised String: $s2 [${s2.length}]'); | |
trace ("Unserialise without custom"); | |
var start = Sys.time(); | |
for (i in 0...100000) | |
{ | |
obj1 = haxe.Unserializer.run(s1); | |
} | |
var end = Sys.time(); | |
trace (' Time taken: ' + (end - start)); | |
trace ("Unserialise with custom"); | |
var start = Sys.time(); | |
for (i in 0...100000) | |
{ | |
obj2 = haxe.Unserializer.run(s2); | |
} | |
var end = Sys.time(); | |
trace (' Time taken: ' + (end - start)); | |
} | |
static function setupDatabase() | |
{ | |
sys.db.Manager.cnx = sys.db.Sqlite.open("CustomSerialisation.db"); | |
if (sys.db.TableCreate.exists(MyModelWithoutCustomSerialize.manager) == false) | |
{ | |
sys.db.TableCreate.create(MyModelWithoutCustomSerialize.manager); | |
// Insert a test object | |
var obj = new MyModelWithoutCustomSerialize(); | |
obj.id = 12; | |
obj.created = Date.now(); | |
obj.modified = Date.now(); | |
obj.insert(); | |
} | |
if (sys.db.TableCreate.exists(MyModelWithCustomSerialize.manager) == false) | |
{ | |
sys.db.TableCreate.create(MyModelWithCustomSerialize.manager); | |
// Insert a test object | |
var obj = new MyModelWithCustomSerialize(); | |
obj.id = 12; | |
obj.created = Date.now(); | |
obj.modified = Date.now(); | |
obj.insert(); | |
} | |
} | |
} | |
class MyModelWithoutCustomSerialize extends sys.db.Object | |
{ | |
public var id:SId; | |
public var created:SDateTime; | |
public var modified:SDateTime; | |
} | |
class MyModelWithCustomSerialize extends sys.db.Object | |
{ | |
public var id:SId; | |
public var created:SDateTime; | |
public var modified:SDateTime; | |
static var hxSerializeFields = ["id","created","modified"]; | |
function hxSerialize( s : haxe.Serializer ) | |
{ | |
s.useEnumIndex = true; | |
s.useCache = true; | |
var fields:Array<String> = untyped Type.getClass(this).hxSerializeFields; | |
for (f in fields) | |
{ | |
s.serialize(Reflect.field(this, f)); | |
} | |
} | |
function hxUnserialize( s : haxe.Unserializer ) | |
{ | |
for (f in hxSerializeFields) | |
{ | |
Reflect.setProperty(this, f, s.unserialize()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CustomSerialisation.hx:26: Serialise without custom | |
CustomSerialisation.hx:33: Time taken: 6.49781084060669 | |
CustomSerialisation.hx:34: Serialised String: cy29:MyModelWithoutCustomSerializey5:_lockty2:idi12y7:createdy25:2013-04-12%2020%3A09%3A13y8:modifiedR4y9:__cache__oR2i12R3R4R5R4gg [131] | |
CustomSerialisation.hx:36: Serialise with custom | |
CustomSerialisation.hx:43: Time taken: 2.81798195838928 | |
CustomSerialisation.hx:44: Serialised String: Cy26:MyModelWithCustomSerializei12y25:2013-04-12%2020%3A09%3A13R1g [66] | |
CustomSerialisation.hx:46: Unserialise without custom | |
CustomSerialisation.hx:53: Time taken: 4.57977795600891 | |
CustomSerialisation.hx:55: Unserialise with custom | |
CustomSerialisation.hx:62: Time taken: 1.75522494316101 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment