Skip to content

Commit

Permalink
serialize.h: use machine native byte swapping if available, fall-back…
Browse files Browse the repository at this point in the history
… to previous generic method if not (supported for GCC using endian.h, detection done in cmake) write/readARGB8() - just write 32-bit color in one op, instead of 4 1-byte ops cleanup: removed unneeded buffer init for some serialize-out functions use a #define for the fixed point factor in read/writeF1000()

nodemetadata.cpp, nodetimer.cpp
	optimzation: simpler deserialize node position method

staticobject.cpp:
	cleanup: use util/serialize.h inlines instead of its own de/serialization

serialize.cpp:
	minor optimization/cleanup: avoid generation of unneeded string temporary

CMakeLists.txt, cmake_config.h.in: detection of endian.h

config.h: added HAVE_ENDIAN_H

Commits due to feedback squashed

Signed-off-by: Craig Robbins <kde.psych@gmail.com>
  • Loading branch information
rafael7 authored and Zeno- committed Nov 21, 2014
1 parent d406ac9 commit f7d6509
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 85 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Expand Up @@ -2,6 +2,7 @@ project(minetest)
cmake_minimum_required( VERSION 2.6 )

INCLUDE(CheckCSourceRuns)
INCLUDE(CheckIncludeFiles)

# Set some random things default to not being visible in the GUI
mark_as_advanced(EXECUTABLE_OUTPUT_PATH LIBRARY_OUTPUT_PATH)
Expand Down Expand Up @@ -313,6 +314,8 @@ if(ENABLE_REDIS)
endif(REDIS_LIBRARY AND REDIS_INCLUDE_DIR)
endif(ENABLE_REDIS)

CHECK_INCLUDE_FILES(endian.h HAVE_ENDIAN_H)

configure_file(
"${PROJECT_SOURCE_DIR}/cmake_config.h.in"
"${PROJECT_BINARY_DIR}/cmake_config.h"
Expand Down
1 change: 1 addition & 0 deletions src/cmake_config.h.in
Expand Up @@ -20,6 +20,7 @@
#define CMAKE_VERSION_PATCH @VERSION_PATCH@
#define CMAKE_VERSION_PATCH_ORIG @VERSION_PATCH_ORIG@
#define CMAKE_VERSION_EXTRA_STRING "@VERSION_EXTRA@"
#define CMAKE_HAVE_ENDIAN_H @HAVE_ENDIAN_H@

#ifdef NDEBUG
#define CMAKE_BUILD_TYPE "Release"
Expand Down
4 changes: 4 additions & 0 deletions src/config.h
Expand Up @@ -36,6 +36,8 @@
#define USE_REDIS 0
#endif

#define HAVE_ENDIAN_H 0

#ifdef USE_CMAKE_CONFIG_H
#include "cmake_config.h"
#undef PROJECT_NAME
Expand Down Expand Up @@ -72,6 +74,8 @@
#define PRODUCT_VERSION_STRING CMAKE_PRODUCT_VERSION_STRING
#undef VERSION_EXTRA_STRING
#define VERSION_EXTRA_STRING CMAKE_VERSION_EXTRA_STRING
#undef HAVE_ENDIAN_H
#define HAVE_ENDIAN_H CMAKE_HAVE_ENDIAN_H
#endif

#ifdef __ANDROID__
Expand Down
12 changes: 6 additions & 6 deletions src/nodemetadata.cpp
Expand Up @@ -130,12 +130,12 @@ void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
{
u16 p16 = readU16(is);

v3s16 p(0,0,0);
p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
p.Y += p16 / MAP_BLOCKSIZE;
p16 -= p.Y * MAP_BLOCKSIZE;
p.X += p16;
v3s16 p;
p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
p.Y = p16 / MAP_BLOCKSIZE;
p16 &= MAP_BLOCKSIZE - 1;
p.X = p16;

if(m_data.find(p) != m_data.end())
{
Expand Down
12 changes: 6 additions & 6 deletions src/nodetimer.cpp
Expand Up @@ -96,12 +96,12 @@ void NodeTimerList::deSerialize(std::istream &is, u8 map_format_version)
{
u16 p16 = readU16(is);

v3s16 p(0,0,0);
p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
p.Y += p16 / MAP_BLOCKSIZE;
p16 -= p.Y * MAP_BLOCKSIZE;
p.X += p16;
v3s16 p;
p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
p.Y = p16 / MAP_BLOCKSIZE;
p16 &= MAP_BLOCKSIZE - 1;
p.X = p16;

NodeTimer t;
t.deSerialize(is);
Expand Down
32 changes: 9 additions & 23 deletions src/staticobject.cpp
Expand Up @@ -22,42 +22,31 @@ with this program; if not, write to the Free Software Foundation, Inc.,

void StaticObject::serialize(std::ostream &os)
{
char buf[12];
// type
buf[0] = type;
os.write(buf, 1);
writeU8(os, type);
// pos
writeV3S32((u8*)buf, v3s32(pos.X*1000,pos.Y*1000,pos.Z*1000));
os.write(buf, 12);
writeV3F1000(os, pos);
// data
os<<serializeString(data);
}
void StaticObject::deSerialize(std::istream &is, u8 version)
{
char buf[12];
// type
is.read(buf, 1);
type = buf[0];
type = readU8(is);
// pos
is.read(buf, 12);
v3s32 intp = readV3S32((u8*)buf);
pos.X = (f32)intp.X/1000;
pos.Y = (f32)intp.Y/1000;
pos.Z = (f32)intp.Z/1000;
pos = readV3F1000(is);
// data
data = deSerializeString(is);
}

void StaticObjectList::serialize(std::ostream &os)
{
char buf[12];
// version
buf[0] = 0;
os.write(buf, 1);
u8 version = 0;
writeU8(os, version);
// count
u16 count = m_stored.size() + m_active.size();
writeU16((u8*)buf, count);
os.write(buf, 2);
writeU16(os, count);
for(std::list<StaticObject>::iterator
i = m_stored.begin();
i != m_stored.end(); ++i)
Expand All @@ -75,13 +64,10 @@ void StaticObjectList::serialize(std::ostream &os)
}
void StaticObjectList::deSerialize(std::istream &is)
{
char buf[12];
// version
is.read(buf, 1);
u8 version = buf[0];
u8 version = readU8(is);
// count
is.read(buf, 2);
u16 count = readU16((u8*)buf);
u16 count = readU16(is);
for(u16 i=0; i<count; i++)
{
StaticObject s_obj;
Expand Down
12 changes: 6 additions & 6 deletions src/util/serialize.cpp
Expand Up @@ -68,11 +68,11 @@ std::string deSerializeString(std::istream &is)
if(is.gcount() != 2)
throw SerializationError("deSerializeString: size not read");
u16 s_size = readU16((u8*)buf);
std::string s;
if(s_size == 0)
return "";
return s;
Buffer<char> buf2(s_size);
is.read(&buf2[0], s_size);
std::string s;
s.reserve(s_size);
s.append(&buf2[0], s_size);
return s;
Expand All @@ -86,9 +86,9 @@ std::wstring deSerializeWideString(std::istream &is)
if(is.gcount() != 2)
throw SerializationError("deSerializeString: size not read");
u16 s_size = readU16((u8*)buf);
if(s_size == 0)
return L"";
std::wstring s;
if(s_size == 0)
return s;
s.reserve(s_size);
for(u32 i=0; i<s_size; i++)
{
Expand Down Expand Up @@ -118,11 +118,11 @@ std::string deSerializeLongString(std::istream &is)
if(is.gcount() != 4)
throw SerializationError("deSerializeLongString: size not read");
u32 s_size = readU32((u8*)buf);
std::string s;
if(s_size == 0)
return "";
return s;
Buffer<char> buf2(s_size);
is.read(&buf2[0], s_size);
std::string s;
s.reserve(s_size);
s.append(&buf2[0], s_size);
return s;
Expand Down

0 comments on commit f7d6509

Please sign in to comment.