Skip to content

Commit b87dc98

Browse files
committed
Performance optimization.
1 parent 17449ac commit b87dc98

File tree

2 files changed

+45
-74
lines changed

2 files changed

+45
-74
lines changed

src/BinarySerializer.test.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,15 @@ describe("array",()=>{
199199

200200
//-------------------------------------------------
201201

202-
export class Vector2
202+
class Vector2
203203
{
204204
@SerializeField(DataType.Float32)
205205
x:number;
206206
@SerializeField(DataType.Float32)
207207
y:number;
208208
}
209209

210-
export class Color
210+
class Color
211211
{
212212
@SerializeField(DataType.Float32)
213213
r:number;
@@ -219,7 +219,7 @@ export class Color
219219
a:number;
220220
}
221221

222-
export class Sprite
222+
class Sprite
223223
{
224224
@SerializeField(DataType.String)
225225
public spriteName:string;
@@ -235,7 +235,7 @@ export class Sprite
235235
public maskBone:string;
236236
}
237237

238-
export class Vector3
238+
class Vector3
239239
{
240240
@SerializeField(DataType.Float32)
241241
x:number;
@@ -263,7 +263,7 @@ class Bone
263263
public children:Array<string>;
264264
}
265265

266-
export class Frame
266+
class Frame
267267
{
268268
@SerializeField(DataType.Float32)
269269
public time:number;
@@ -273,7 +273,7 @@ export class Frame
273273
public str:string;
274274
}
275275

276-
export class Curve
276+
class Curve
277277
{
278278
@SerializeField(DataType.String)
279279
public aim:string;
@@ -285,7 +285,7 @@ export class Curve
285285
private framesDic:{[key:number]:number|string} = {}
286286
}
287287

288-
export class State
288+
class State
289289
{
290290
@SerializeField(DataType.String)
291291
public name:string;
@@ -296,7 +296,7 @@ export class State
296296
}
297297

298298

299-
export class Clip
299+
class Clip
300300
{
301301
@SerializeField(DataType.String)
302302
public name:string;
@@ -307,15 +307,15 @@ export class Clip
307307

308308
}
309309

310-
export class Animation
310+
class Animation
311311
{
312312
@SerializeField(DataType.Object,true,Clip)
313313
public animClips:Array<Clip>;
314314
@SerializeField(DataType.Object,true,State)
315315
public states:Array<State>;
316316
}
317317

318-
export class DataInfo
318+
class DataInfo
319319
{
320320
@SerializeField(DataType.String)
321321
public rootBone:string;
@@ -338,18 +338,19 @@ describe('benchmark',()=>{
338338
console.log('json size: ' + jsonsize +" byte");
339339

340340
let datainfo:DataInfo = <DataInfo> obj;
341-
let serializedData = BinarySerialize(datainfo,DataInfo);
341+
342342
let t2 = performance.now();
343+
let serializedData = BinarySerialize(datainfo,DataInfo);
344+
console.log(`binary serialize: ${performance.now() - t2} ms`);
345+
346+
let t3 = performance.now();
343347
let obj1 = BinaryDeserialize(DataInfo,serializedData);
344-
console.log(`binary deserialize: ${performance.now() -t2} ms`);
348+
console.log(`binary deserialize: ${performance.now() -t3} ms`);
345349

346350
let binarysize = serializedData.byteLength;
347351
console.log('binary size: '+ binarysize +" byte");
348352
console.log('size save: '+ ((jsonsize - binarysize)/ jsonsize));
349-
350353
});
351-
352-
353354
})
354355

355356
//--------------------------------------

src/BinarySerializer.ts

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class BinaryBuffer {
133133
return buffer;
134134
}
135135

136-
public checkBufferExten(appendSize:number = 8){
136+
public checkBufferExpand(appendSize:number = 8){
137137
let cursize = this.m_arrayBufferCurrentSize;
138138
if(this.m_pos + appendSize >= cursize){
139139
let tarsize = cursize + appendSize;
@@ -166,7 +166,9 @@ class BinaryBuffer {
166166
this.writeType(type);
167167
let f:(v:any)=>void =this[BinaryBuffer.WriteFuncMap[type]];
168168
let isobj = type == DataType.Object;
169+
169170
if(!isary){
171+
this.checkBufferExpand(8);
170172
isobj? f.call(this,val,tmc): f.call(this,val);
171173
return;
172174
}
@@ -175,15 +177,11 @@ class BinaryBuffer {
175177
throw new Error(msg);
176178
}
177179
let ary = <Array<any>>val;
178-
179180
let arylen = ary.length;
180-
181-
if(type != DataType.Object && type != DataType.String){
182-
this.checkBufferExten(arylen *8+4);
183-
}
184-
else{
185-
this.checkBufferExten(4);
181+
if(arylen > 65535){
182+
throw new Error('array length exceeded.');
186183
}
184+
this.checkBufferExpand(arylen *8 + 4);
187185
this.writeUint16(arylen);
188186
if(isobj){
189187
for(let i=0;i<arylen;i++){
@@ -252,7 +250,13 @@ class BinaryBuffer {
252250
public writeFloat64(v : number) {
253251
let view = this.m_view;
254252
let p = this.m_pos;
255-
view.setFloat64(p, v);
253+
try{
254+
view.setFloat64(p, v);
255+
}
256+
catch(e){
257+
console.log(p,this.m_arrayBufferCurrentSize);
258+
throw e;
259+
}
256260
this.m_pos += 8;
257261
}
258262
public readFloat64() : number {
@@ -360,7 +364,7 @@ class BinaryBuffer {
360364
if (len >= 65535)
361365
throw new Error('string length exceed!');
362366
this.writeUint16(len);
363-
this.checkBufferExten(len);
367+
this.checkBufferExpand(len);
364368
let buf = this.m_arrayBuffer;
365369
buf.set(ary, this.m_pos);
366370
this.m_pos += len;
@@ -387,77 +391,39 @@ class BinaryBuffer {
387391
}
388392

389393
public writeObject(o:any,tmc:TypeMetaClass){
390-
if(o == null){
391-
this.writeUint32(0);
392-
return;
393-
}
394-
let buffer = BinarySeralizer.serialize(tmc,o);
395-
let len = buffer.byteLength;
396-
this.checkBufferExten(len + 8);
397-
this.writeUint32(len);
398-
this.m_arrayBuffer.set(new Uint8Array(buffer,0,len),this.m_pos);
399-
this.m_pos += len;
394+
this.serialize(tmc,o);
400395
}
401396

402397
public readObject(tmc:TypeMetaClass){
403-
let len = this.readUint32();
404-
if(len ==0) return null;
405-
let pos = this.m_pos;
406-
let arybuffer = this.m_arrayBuffer.buffer.slice(pos,pos + len);
407-
let ret = BinarySeralizer.deserializeWidthMeta(tmc,arybuffer,0,len);
408-
this.m_pos += len;
409-
return ret;
398+
let tar = Object.create(tmc.prototype);
399+
this.deserialize(tar,tmc);
400+
return tar;
410401
}
411-
}
412-
BinaryBuffer.initialize();
413402

414-
415-
class BinarySeralizer {
416-
public static serialize <T> (mc : TypeMetaClass, obj : T):ArrayBuffer{
403+
public serialize<T>(mc:TypeMetaClass,obj:T){
417404
let properties = mc.properties;
418-
419-
let binarybuffer = BinaryBuffer.create();
420405
for(let i=0,len = properties.length;i<len;i++){
421406
let p = properties[i];
422-
binarybuffer.pushProperty(p.datatype,obj[p.key],p.isArray,p.pclass);
407+
this.pushProperty(p.datatype,obj[p.key],p.isArray,p.pclass);
423408
}
424-
return binarybuffer.m_arrayBuffer.buffer.slice(0,binarybuffer.pos);
425409
}
426410

427-
public static deserialize<T>(tar:T,mc:TypeMetaClass,buffer:ArrayBuffer):T | null{
411+
public deserialize<T>(tar:T,mc:TypeMetaClass):T | null{
428412
if(mc == null){
429413
throw new Error('typeMetaClass is null');
430414
}
431415
mc.sortProperty();
432416

433417
let properties = mc.properties;
434-
let binarybuffer = BinaryBuffer.createWithView(buffer,0,buffer.byteLength);
435418
for(let i=0,len= properties.length;i<len;i++){
436419
let p = properties[i];
437-
var val = binarybuffer.readProperty(p.datatype,p.isArray,p.pclass);
438-
tar[p.key] = val;
439-
}
440-
return tar;
441-
}
442-
443-
public static deserializeWidthMeta(mc:TypeMetaClass,buffer:ArrayBuffer,offset:number,size:number): any| null{
444-
if(mc == null){
445-
throw new Error('typeMetaClass is null');
446-
}
447-
mc.sortProperty();
448-
449-
let properties = mc.properties;
450-
let proto = mc.prototype;
451-
let tar = Object.create(proto);
452-
let binarybuffer = BinaryBuffer.createWithView(buffer,offset,size);
453-
for(let i=0,len= properties.length;i<len;i++){
454-
let p = properties[i];
455-
var val = binarybuffer.readProperty(p.datatype,p.isArray,p.pclass);
420+
var val = this.readProperty(p.datatype,p.isArray,p.pclass);
456421
tar[p.key] = val;
457422
}
458423
return tar;
459424
}
460425
}
426+
BinaryBuffer.initialize();
461427

462428
export function BinarySerialize < T > (obj : T,type?:{new():T}):ArrayBuffer{
463429
let p = Object.getPrototypeOf(obj);
@@ -474,7 +440,10 @@ export function BinarySerialize < T > (obj : T,type?:{new():T}):ArrayBuffer{
474440
throw new Error(msg);
475441
}
476442
mc.sortProperty();
477-
return BinarySeralizer.serialize(mc,obj);
443+
444+
let binarybuffer = BinaryBuffer.create();
445+
binarybuffer.serialize(mc,obj);
446+
return binarybuffer.m_arrayBuffer.buffer.slice(0,binarybuffer.pos);
478447
}
479448

480449
export function BinaryDeserialize<T>(type:{new():T},databuffer:ArrayBuffer): T |null{
@@ -483,6 +452,7 @@ export function BinaryDeserialize<T>(type:{new():T},databuffer:ArrayBuffer): T |
483452
let mc = TypeReflector.getMetaClass(p);
484453
if(mc == null) throw new Error(`reflect class ${p} invalid.`);
485454
mc.sortProperty();
486-
return BinarySeralizer.deserialize(obj,mc,databuffer);
487455

456+
let binarybuffer = BinaryBuffer.createWithView(databuffer,0,databuffer.byteLength);
457+
return binarybuffer.deserialize(obj,mc);
488458
}

0 commit comments

Comments
 (0)