Skip to content

Commit

Permalink
IMPROVE: reuse previous allocated memory when reallocating for slight…
Browse files Browse the repository at this point in the history
… speedup. #15
  • Loading branch information
spillerrec committed Mar 19, 2014
1 parent 14bbd69 commit c31873f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/MeshModel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,39 @@ abstract class Drawable3D{
void draw_lines( Float32List vertices, int amount );
}

List<Float32List> unused = new List<Float32List>();
class DynamicFloat32List{
Float32List create( int wanted ){
for( int i=0; i<unused.length; i++ ){
if( unused[i].length >= wanted )
return unused.removeAt(i);
}
return new Float32List( wanted );
}

Float32List list;
int length = 0;
void addAll( Float32List add, Matrix4 offset ){
if( list == null ){ //List doesn't exist
list = new Float32List.fromList( add );
list = create( add.length );
for( int i=0; i<add.length; i++ )
list[i] = add[i];
}
else{
if( length + add.length < list.length ){
int needed = length + add.length;
if( needed < list.length ){
//List large enough to contain both
for( int i=0; i<add.length; i++ )
list[i+length] = add[i];
}
else{
//List not large enough, need to expand
Float32List new_list = new Float32List( (length+add.length)*2 );
Float32List new_list = create( needed*2 );
for( int i=0; i<length; i++ )
new_list[i] = list[i];
for( int i=0; i<add.length; i++ )
new_list[i+length] = add[i];
unused.add(list);
list = new_list;
}
}
Expand Down
54 changes: 54 additions & 0 deletions tool/mesh_efficiency.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'dart:io';
import 'dart:async';
import 'dart:typed_data';

import 'package:webldraw/ldrawlib.dart';

class LDrawFileLib extends LDrawLib{
Future<String> load( String url ) => new File( url ).readAsString();
}

class Debug extends Progress{

@override
void updated() {
if( current == total ){
MeshModel model = new MeshModel();


Stopwatch watch = new Stopwatch();
watch.start();
loader.file.to_mesh( model, new LDrawContext() );
print( "to_mesh took: ${watch.elapsed}" );
watch.reset();
new Float32List( 3244032*10 );
print( "create took: ${watch.elapsed}" );

int amount = 0, amount_real = 0;

print( "lines:" );
model.lines.values.forEach( (f){
print( f.vertices.length );
amount += f.vertices.length;
amount_real += f.vertices.list.length;
});
print( "\ntriangles:" );
model.triangles.values.forEach( (f){
print( "${f.vertices.length} \t\t\t ${f.vertices.list.length}" );
amount += f.vertices.length;
amount_real += f.vertices.list.length;
});

print( "Needed memory: ${ amount * 4 / 1024 / 1024 } MiB" );
print( "Used memory: ${ amount_real * 4 / 1024 / 1024 } MiB" );
print( "unused size: ${unused.length}" );
unused.forEach((f){
print( f.length );
});
}
}
}
LDrawLoader loader;
void main(){
loader = new LDrawLoader( new LDrawFileLib(), "Keywriter.mpd", new Debug() );
}

0 comments on commit c31873f

Please sign in to comment.