3 Some tips when using the FAWE API
stefvanschie edited this page 2018-11-05 20:29:35 +01:00

Performance tips when using the FAWE API:

1. Use the EditSessionBuilder and don't enable what you won't need.

See here.

2. Use the in built iterators whenever possible:

// Naive approach:
for (Vector pt : region) {
    BaseBlock block = editSession.getBlock(pt);
	// Do something
}
// Good approach:
// - Performs chunk preloading
for (Vector pt : new FastIterator(region, editSession)) {
    BaseBlock block = editSession.getBlock(pt);
	// Do something
}

The following classes will perform chunk preloading:

  • FastIterator
  • Fast2DIterator
  • FastChunkIterator
  • BreadFirstSearch
  • RegionVisitor

3. Use existing functions/operations instead of setting single blocks:

// Naive approach: single block changes
for (int x = 0; x <= 100; x++) {
    for (int z = 0; z <= 100; z++) {
        editSession.setBlock(x, 64, z, block);   
    }
}
// Good approach: 
//  - Does chunk preloading
//  - Performs on entire chunks rather than single blocks
//  - Does stuff in parallel
Region region = new CuboidRegion(new Vector(0, 64, 0), new Vector(100, 64, 100));
editSession.setBlocks(region, block);

4. Use the Sets/Collections that come with FAWE

// Naive approach: Storing individual positions
Set<Vector> positions = new HashSet<>(); // Then store some block positions here

// Good approach:
// - Faster and currently uses up to 800x less memory
positions = new BlockVectorSet(); 

FAWE has not optimized these at all to be used async. Consider using the TaskBuilder to break it up on the main thread instead.