Skip to content

Commit 8ad8376

Browse files
sapiersapier
sapier
authored and
sapier
committed
Remove emerge and speedup addArea by using memcopy instead of one by one assignment
1 parent d5de0cb commit 8ad8376

File tree

4 files changed

+83
-112
lines changed

4 files changed

+83
-112
lines changed

src/map.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3551,12 +3551,12 @@ void MapVoxelManipulator::emerge(VoxelArea a, s32 caller_id)
35513551
flags |= VMANIP_BLOCK_DATA_INEXIST;
35523552

35533553
VoxelArea a(p*MAP_BLOCKSIZE, (p+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
3554-
// Fill with VOXELFLAG_INEXISTENT
3554+
// Fill with VOXELFLAG_NO_DATA
35553555
for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
35563556
for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
35573557
{
35583558
s32 i = m_area.index(a.MinEdge.X,y,z);
3559-
memset(&m_flags[i], VOXELFLAG_INEXISTENT, MAP_BLOCKSIZE);
3559+
memset(&m_flags[i], VOXELFLAG_NO_DATA, MAP_BLOCKSIZE);
35603560
}
35613561
}
35623562
/*else if (block->getNode(0, 0, 0).getContent() == CONTENT_IGNORE)
@@ -3601,7 +3601,7 @@ void MapVoxelManipulator::blitBack
36013601
v3s16 p(x,y,z);
36023602

36033603
u8 f = m_flags[m_area.index(p)];
3604-
if(f & (VOXELFLAG_NOT_LOADED|VOXELFLAG_INEXISTENT))
3604+
if(f & (VOXELFLAG_NO_DATA))
36053605
continue;
36063606

36073607
MapNode &n = m_data[m_area.index(p)];
@@ -3655,7 +3655,7 @@ ManualMapVoxelManipulator::~ManualMapVoxelManipulator()
36553655
void ManualMapVoxelManipulator::emerge(VoxelArea a, s32 caller_id)
36563656
{
36573657
// Just create the area so that it can be pointed to
3658-
VoxelManipulator::emerge(a, caller_id);
3658+
VoxelManipulator::addArea(a);
36593659
}
36603660

36613661
void ManualMapVoxelManipulator::initialEmerge(v3s16 blockpos_min,
@@ -3726,12 +3726,12 @@ void ManualMapVoxelManipulator::initialEmerge(v3s16 blockpos_min,
37263726
Mark area inexistent
37273727
*/
37283728
VoxelArea a(p*MAP_BLOCKSIZE, (p+1)*MAP_BLOCKSIZE-v3s16(1,1,1));
3729-
// Fill with VOXELFLAG_INEXISTENT
3729+
// Fill with VOXELFLAG_NO_DATA
37303730
for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
37313731
for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
37323732
{
37333733
s32 i = m_area.index(a.MinEdge.X,y,z);
3734-
memset(&m_flags[i], VOXELFLAG_INEXISTENT, MAP_BLOCKSIZE);
3734+
memset(&m_flags[i], VOXELFLAG_NO_DATA, MAP_BLOCKSIZE);
37353735
}
37363736
}
37373737
}

src/voxel.cpp

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void VoxelManipulator::print(std::ostream &o, INodeDefManager *ndef,
7171
v3s16 of = m_area.MinEdge;
7272
o<<"size: "<<em.X<<"x"<<em.Y<<"x"<<em.Z
7373
<<" offset: ("<<of.X<<","<<of.Y<<","<<of.Z<<")"<<std::endl;
74-
74+
7575
for(s32 y=m_area.MaxEdge.Y; y>=m_area.MinEdge.Y; y--)
7676
{
7777
if(em.X >= 3 && em.Y >= 3)
@@ -88,10 +88,8 @@ void VoxelManipulator::print(std::ostream &o, INodeDefManager *ndef,
8888
{
8989
u8 f = m_flags[m_area.index(x,y,z)];
9090
char c;
91-
if(f & VOXELFLAG_NOT_LOADED)
91+
if(f & VOXELFLAG_NO_DATA)
9292
c = 'N';
93-
else if(f & VOXELFLAG_INEXISTENT)
94-
c = 'I';
9593
else
9694
{
9795
c = 'X';
@@ -149,11 +147,11 @@ void VoxelManipulator::addArea(VoxelArea area)
149147
// Cancel if requested area has zero volume
150148
if(area.getExtent() == v3s16(0,0,0))
151149
return;
152-
150+
153151
// Cancel if m_area already contains the requested area
154152
if(m_area.contains(area))
155153
return;
156-
154+
157155
TimeTaker timer("addArea", &addarea_time);
158156

159157
// Calculate new area
@@ -186,28 +184,26 @@ void VoxelManipulator::addArea(VoxelArea area)
186184
assert(new_data);
187185
u8 *new_flags = new u8[new_size];
188186
assert(new_flags);
189-
memset(new_flags, VOXELFLAG_NOT_LOADED, new_size);
190-
187+
memset(new_flags, VOXELFLAG_NO_DATA, new_size);
188+
191189
// Copy old data
192-
190+
s32 old_x_width = m_area.MaxEdge.X - m_area.MinEdge.X + 1;
193191
for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
194192
for(s32 y=m_area.MinEdge.Y; y<=m_area.MaxEdge.Y; y++)
195-
for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
196193
{
197-
unsigned int old_index = m_area.index(x,y,z);
198-
// If loaded, copy data and flags
199-
if((m_flags[old_index] & VOXELFLAG_NOT_LOADED) == false)
200-
{
201-
unsigned int new_index = new_area.index(x,y,z);
202-
new_data[new_index] = m_data[old_index];
203-
new_flags[new_index] = m_flags[old_index];
204-
}
194+
unsigned int old_index = m_area.index(m_area.MinEdge.X,y,z);
195+
unsigned int new_index = new_area.index(m_area.MinEdge.X,y,z);
196+
197+
memcpy(&new_data[new_index], &m_data[old_index],
198+
old_x_width * sizeof(MapNode));
199+
memcpy(&new_flags[new_index], &m_flags[old_index],
200+
old_x_width * sizeof(u8));
205201
}
206202

207203
// Replace area, data and flags
208-
204+
209205
m_area = new_area;
210-
206+
211207
MapNode *old_data = m_data;
212208
u8 *old_flags = m_flags;
213209

@@ -216,7 +212,7 @@ void VoxelManipulator::addArea(VoxelArea area)
216212

217213
m_data = new_data;
218214
m_flags = new_flags;
219-
215+
220216
if(old_data)
221217
delete[] old_data;
222218
if(old_flags)
@@ -225,7 +221,7 @@ void VoxelManipulator::addArea(VoxelArea area)
225221
//dstream<<"addArea done"<<std::endl;
226222
}
227223

228-
void VoxelManipulator::copyFrom(MapNode *src, VoxelArea src_area,
224+
void VoxelManipulator::copyFrom(MapNode *src, const VoxelArea& src_area,
229225
v3s16 from_pos, v3s16 to_pos, v3s16 size)
230226
{
231227
for(s16 z=0; z<size.Z; z++)
@@ -238,7 +234,7 @@ void VoxelManipulator::copyFrom(MapNode *src, VoxelArea src_area,
238234
}
239235
}
240236

241-
void VoxelManipulator::copyTo(MapNode *dst, VoxelArea dst_area,
237+
void VoxelManipulator::copyTo(MapNode *dst, const VoxelArea& dst_area,
242238
v3s16 dst_pos, v3s16 from_pos, v3s16 size)
243239
{
244240
for(s16 z=0; z<size.Z; z++)
@@ -252,7 +248,6 @@ void VoxelManipulator::copyTo(MapNode *dst, VoxelArea dst_area,
252248
i_dst++;
253249
i_local++;
254250
}
255-
//memcpy(&dst[i_dst], &m_data[i_local], size.X*sizeof(MapNode));
256251
}
257252
}
258253

@@ -314,22 +309,22 @@ void VoxelManipulator::unspreadLight(enum LightBank bank, v3s16 p, u8 oldlight,
314309
v3s16(0,-1,0), // bottom
315310
v3s16(-1,0,0), // left
316311
};
317-
318-
emerge(VoxelArea(p - v3s16(1,1,1), p + v3s16(1,1,1)));
312+
313+
addArea(VoxelArea(p - v3s16(1,1,1), p + v3s16(1,1,1)));
319314

320315
// Loop through 6 neighbors
321316
for(u16 i=0; i<6; i++)
322317
{
323318
// Get the position of the neighbor node
324319
v3s16 n2pos = p + dirs[i];
325-
320+
326321
u32 n2i = m_area.index(n2pos);
327322

328-
if(m_flags[n2i] & VOXELFLAG_INEXISTENT)
323+
if(m_flags[n2i] & VOXELFLAG_NO_DATA)
329324
continue;
330325

331326
MapNode &n2 = m_data[n2i];
332-
327+
333328
/*
334329
If the neighbor is dimmer than what was specified
335330
as oldlight (the light of the previous node)
@@ -347,9 +342,9 @@ void VoxelManipulator::unspreadLight(enum LightBank bank, v3s16 p, u8 oldlight,
347342
*/
348343

349344
n2.setLight(bank, 0, nodemgr);
350-
345+
351346
unspreadLight(bank, n2pos, light2, light_sources, nodemgr);
352-
347+
353348
/*
354349
Remove from light_sources if it is there
355350
NOTE: This doesn't happen nearly at all
@@ -391,7 +386,7 @@ void VoxelManipulator::unspreadLight(enum LightBank bank,
391386
{
392387
if(from_nodes.size() == 0)
393388
return;
394-
389+
395390
for(std::map<v3s16, u8>::iterator j = from_nodes.begin();
396391
j != from_nodes.end(); ++j)
397392
{
@@ -430,37 +425,37 @@ void VoxelManipulator::unspreadLight(enum LightBank bank,
430425
v3s16(0,-1,0), // bottom
431426
v3s16(-1,0,0), // left
432427
};
433-
428+
434429
if(from_nodes.size() == 0)
435430
return;
436-
431+
437432
core::map<v3s16, u8> unlighted_nodes;
438433
core::map<v3s16, u8>::Iterator j;
439434
j = from_nodes.getIterator();
440435

441436
for(; j.atEnd() == false; j++)
442437
{
443438
v3s16 pos = j.getNode()->getKey();
444-
445-
emerge(VoxelArea(pos - v3s16(1,1,1), pos + v3s16(1,1,1)));
439+
440+
addArea(VoxelArea(pos - v3s16(1,1,1), pos + v3s16(1,1,1)));
446441

447442
//MapNode &n = m_data[m_area.index(pos)];
448-
443+
449444
u8 oldlight = j.getNode()->getValue();
450-
445+
451446
// Loop through 6 neighbors
452447
for(u16 i=0; i<6; i++)
453448
{
454449
// Get the position of the neighbor node
455450
v3s16 n2pos = pos + dirs[i];
456-
451+
457452
u32 n2i = m_area.index(n2pos);
458453

459-
if(m_flags[n2i] & VOXELFLAG_INEXISTENT)
454+
if(m_flags[n2i] & VOXELFLAG_NO_DATA)
460455
continue;
461456

462457
MapNode &n2 = m_data[n2i];
463-
458+
464459
/*
465460
If the neighbor is dimmer than what was specified
466461
as oldlight (the light of the previous node)
@@ -480,7 +475,7 @@ void VoxelManipulator::unspreadLight(enum LightBank bank,
480475
n2.setLight(bank, 0);
481476

482477
unlighted_nodes.insert(n2pos, current_light);
483-
478+
484479
/*
485480
Remove from light_sources if it is there
486481
NOTE: This doesn't happen nearly at all
@@ -502,7 +497,7 @@ void VoxelManipulator::unspreadLight(enum LightBank bank,
502497
<<blockchangecount<<" times"
503498
<<" for "<<from_nodes.size()<<" nodes"
504499
<<std::endl;*/
505-
500+
506501
if(unlighted_nodes.size() > 0)
507502
unspreadLight(bank, unlighted_nodes, light_sources);
508503
}
@@ -520,11 +515,11 @@ void VoxelManipulator::spreadLight(enum LightBank bank, v3s16 p,
520515
v3s16(-1,0,0), // left
521516
};
522517

523-
emerge(VoxelArea(p - v3s16(1,1,1), p + v3s16(1,1,1)));
518+
addArea(VoxelArea(p - v3s16(1,1,1), p + v3s16(1,1,1)));
524519

525520
u32 i = m_area.index(p);
526-
527-
if(m_flags[i] & VOXELFLAG_INEXISTENT)
521+
522+
if(m_flags[i] & VOXELFLAG_NO_DATA)
528523
return;
529524

530525
MapNode &n = m_data[i];
@@ -537,16 +532,16 @@ void VoxelManipulator::spreadLight(enum LightBank bank, v3s16 p,
537532
{
538533
// Get the position of the neighbor node
539534
v3s16 n2pos = p + dirs[i];
540-
535+
541536
u32 n2i = m_area.index(n2pos);
542537

543-
if(m_flags[n2i] & VOXELFLAG_INEXISTENT)
538+
if(m_flags[n2i] & VOXELFLAG_NO_DATA)
544539
continue;
545540

546541
MapNode &n2 = m_data[n2i];
547542

548543
u8 light2 = n2.getLight(bank, nodemgr);
549-
544+
550545
/*
551546
If the neighbor is brighter than the current node,
552547
add to list (it will light up this node on its turn)
@@ -583,7 +578,7 @@ void VoxelManipulator::spreadLight(enum LightBank bank,
583578
{
584579
if(from_nodes.size() == 0)
585580
return;
586-
581+
587582
core::map<v3s16, bool> lighted_nodes;
588583
core::map<v3s16, bool>::Iterator j;
589584
j = from_nodes.getIterator();
@@ -616,19 +611,19 @@ void VoxelManipulator::spreadLight(enum LightBank bank,
616611

617612
if(from_nodes.size() == 0)
618613
return;
619-
614+
620615
std::set<v3s16> lighted_nodes;
621616

622617
for(std::set<v3s16>::iterator j = from_nodes.begin();
623618
j != from_nodes.end(); ++j)
624619
{
625620
v3s16 pos = *j;
626621

627-
emerge(VoxelArea(pos - v3s16(1,1,1), pos + v3s16(1,1,1)));
622+
addArea(VoxelArea(pos - v3s16(1,1,1), pos + v3s16(1,1,1)));
628623

629624
u32 i = m_area.index(pos);
630-
631-
if(m_flags[i] & VOXELFLAG_INEXISTENT)
625+
626+
if(m_flags[i] & VOXELFLAG_NO_DATA)
632627
continue;
633628

634629
MapNode &n = m_data[i];
@@ -641,18 +636,18 @@ void VoxelManipulator::spreadLight(enum LightBank bank,
641636
{
642637
// Get the position of the neighbor node
643638
v3s16 n2pos = pos + dirs[i];
644-
639+
645640
try
646641
{
647642
u32 n2i = m_area.index(n2pos);
648643

649-
if(m_flags[n2i] & VOXELFLAG_INEXISTENT)
644+
if(m_flags[n2i] & VOXELFLAG_NO_DATA)
650645
continue;
651646

652647
MapNode &n2 = m_data[n2i];
653648

654649
u8 light2 = n2.getLight(bank, nodemgr);
655-
650+
656651
/*
657652
If the neighbor is brighter than the current node,
658653
add to list (it will light up this node on its turn)
@@ -685,7 +680,7 @@ void VoxelManipulator::spreadLight(enum LightBank bank,
685680
<<blockchangecount<<" times"
686681
<<" for "<<from_nodes.size()<<" nodes"
687682
<<std::endl;*/
688-
683+
689684
if(lighted_nodes.size() > 0)
690685
spreadLight(bank, lighted_nodes, nodemgr);
691686
}

0 commit comments

Comments
 (0)