Skip to content

Commit

Permalink
Merge pull request CleverRaven#54399 from akrieger/more_json_cleanup
Browse files Browse the repository at this point in the history
Eliminate ~80% of JsonObject copies
  • Loading branch information
kevingranade authored Jan 14, 2022
2 parents 335aa0e + 72b8270 commit e7a8ea3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 65 deletions.
114 changes: 57 additions & 57 deletions src/assign.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ bool assign( const JsonObject &jo, const std::string &name, T &val, bool strict
double scalar;

// Object via which to report errors which differs for proportional/relative values
JsonObject err = jo;
err.allow_omitted_members();
const JsonObject *err = &jo;
JsonObject relative = jo.get_object( "relative" );
relative.allow_omitted_members();
JsonObject proportional = jo.get_object( "proportional" );
Expand All @@ -68,14 +67,14 @@ bool assign( const JsonObject &jo, const std::string &name, T &val, bool strict
// Do not require strict parsing for relative and proportional values as rules
// such as +10% are well-formed independent of whether they affect base value
if( relative.read( name, out ) ) {
err = relative;
err = &relative;
strict = false;
out += val;

} else if( proportional.read( name, scalar ) ) {
err = proportional;
err = &proportional;
if( scalar <= 0 || scalar == 1 ) {
err.throw_error( "multiplier must be a positive number other than 1", name );
err->throw_error( "multiplier must be a positive number other than 1", name );
}
strict = false;
out = val * scalar;
Expand All @@ -85,11 +84,12 @@ bool assign( const JsonObject &jo, const std::string &name, T &val, bool strict
}

if( out < lo || out > hi ) {
err.throw_error( "value outside supported range", name );
err->throw_error( "value outside supported range", name );
}

if( strict && out == val ) {
report_strict_violation( err, "cannot assign explicit value the same as default or inherited value",
report_strict_violation( *err,
"cannot assign explicit value the same as default or inherited value",
name );
}

Expand Down Expand Up @@ -265,8 +265,7 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::volume
units::volume out;

// Object via which to report errors which differs for proportional/relative values
JsonObject err = jo;
err.allow_omitted_members();
const JsonObject *err = &jo;
JsonObject relative = jo.get_object( "relative" );
relative.allow_omitted_members();
JsonObject proportional = jo.get_object( "proportional" );
Expand All @@ -276,18 +275,18 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::volume
// such as +10% are well-formed independent of whether they affect base value
if( relative.has_member( name ) ) {
units::volume tmp;
err = relative;
if( !parse( err, tmp ) ) {
err.throw_error( "invalid relative value specified", name );
err = &relative;
if( !parse( *err, tmp ) ) {
err->throw_error( "invalid relative value specified", name );
}
strict = false;
out = val + tmp;

} else if( proportional.has_member( name ) ) {
double scalar;
err = proportional;
if( !err.read( name, scalar ) || scalar <= 0 || scalar == 1 ) {
err.throw_error( "multiplier must be a positive number other than 1", name );
err = &proportional;
if( !err->read( name, scalar ) || scalar <= 0 || scalar == 1 ) {
err->throw_error( "multiplier must be a positive number other than 1", name );
}
strict = false;
out = val * scalar;
Expand All @@ -297,11 +296,12 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::volume
}

if( out < lo || out > hi ) {
err.throw_error( "value outside supported range", name );
err->throw_error( "value outside supported range", name );
}

if( strict && out == val ) {
report_strict_violation( err, "cannot assign explicit value the same as default or inherited value",
report_strict_violation( *err,
"cannot assign explicit value the same as default or inherited value",
name );
}

Expand Down Expand Up @@ -331,8 +331,7 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::mass &
units::mass out;

// Object via which to report errors which differs for proportional/relative values
JsonObject err = jo;
err.allow_omitted_members();
const JsonObject *err = &jo;
JsonObject relative = jo.get_object( "relative" );
relative.allow_omitted_members();
JsonObject proportional = jo.get_object( "proportional" );
Expand All @@ -342,18 +341,18 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::mass &
// such as +10% are well-formed independent of whether they affect base value
if( relative.has_member( name ) ) {
units::mass tmp;
err = relative;
if( !parse( err, tmp ) ) {
err.throw_error( "invalid relative value specified", name );
err = &relative;
if( !parse( *err, tmp ) ) {
err->throw_error( "invalid relative value specified", name );
}
strict = false;
out = val + tmp;

} else if( proportional.has_member( name ) ) {
double scalar;
err = proportional;
if( !err.read( name, scalar ) || scalar <= 0 || scalar == 1 ) {
err.throw_error( "multiplier must be a positive number other than 1", name );
err = &proportional;
if( !err->read( name, scalar ) || scalar <= 0 || scalar == 1 ) {
err->throw_error( "multiplier must be a positive number other than 1", name );
}
strict = false;
out = val * scalar;
Expand All @@ -363,11 +362,12 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::mass &
}

if( out < lo || out > hi ) {
err.throw_error( "value outside supported range", name );
err->throw_error( "value outside supported range", name );
}

if( strict && out == val ) {
report_strict_violation( err, "cannot assign explicit value the same as default or inherited value",
report_strict_violation( *err,
"cannot assign explicit value the same as default or inherited value",
name );
}

Expand Down Expand Up @@ -397,8 +397,7 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::length
units::length out;

// Object via which to report errors which differs for proportional/relative values
JsonObject err = jo;
err.allow_omitted_members();
const JsonObject *err = &jo;
JsonObject relative = jo.get_object( "relative" );
relative.allow_omitted_members();
JsonObject proportional = jo.get_object( "proportional" );
Expand All @@ -408,18 +407,18 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::length
// such as +10% are well-formed independent of whether they affect base value
if( relative.has_member( name ) ) {
units::length tmp;
err = relative;
if( !parse( err, tmp ) ) {
err.throw_error( "invalid relative value specified", name );
err = &relative;
if( !parse( *err, tmp ) ) {
err->throw_error( "invalid relative value specified", name );
}
strict = false;
out = val + tmp;

} else if( proportional.has_member( name ) ) {
double scalar;
err = proportional;
if( !err.read( name, scalar ) || scalar <= 0 || scalar == 1 ) {
err.throw_error( "multiplier must be a positive number other than 1", name );
err = &proportional;
if( !err->read( name, scalar ) || scalar <= 0 || scalar == 1 ) {
err->throw_error( "multiplier must be a positive number other than 1", name );
}
strict = false;
out = val * scalar;
Expand All @@ -429,11 +428,12 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::length
}

if( out < lo || out > hi ) {
err.throw_error( "value outside supported range", name );
err->throw_error( "value outside supported range", name );
}

if( strict && out == val ) {
report_strict_violation( err, "cannot assign explicit value the same as default or inherited value",
report_strict_violation( *err,
"cannot assign explicit value the same as default or inherited value",
name );
}

Expand Down Expand Up @@ -463,8 +463,7 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::money
units::money out;

// Object via which to report errors which differs for proportional/relative values
JsonObject err = jo;
err.allow_omitted_members();
const JsonObject *err = &jo;
JsonObject relative = jo.get_object( "relative" );
relative.allow_omitted_members();
JsonObject proportional = jo.get_object( "proportional" );
Expand All @@ -474,18 +473,18 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::money
// such as +10% are well-formed independent of whether they affect base value
if( relative.has_member( name ) ) {
units::money tmp;
err = relative;
if( !parse( err, tmp ) ) {
err.throw_error( "invalid relative value specified", name );
err = &relative;
if( !parse( *err, tmp ) ) {
err->throw_error( "invalid relative value specified", name );
}
strict = false;
out = val + tmp;

} else if( proportional.has_member( name ) ) {
double scalar;
err = proportional;
if( !err.read( name, scalar ) || scalar <= 0 || scalar == 1 ) {
err.throw_error( "multiplier must be a positive number other than 1", name );
err = &proportional;
if( !err->read( name, scalar ) || scalar <= 0 || scalar == 1 ) {
err->throw_error( "multiplier must be a positive number other than 1", name );
}
strict = false;
out = val * scalar;
Expand All @@ -495,11 +494,12 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::money
}

if( out < lo || out > hi ) {
err.throw_error( "value outside supported range", name );
err->throw_error( "value outside supported range", name );
}

if( strict && out == val ) {
report_strict_violation( err, "cannot assign explicit value the same as default or inherited value",
report_strict_violation( *err,
"cannot assign explicit value the same as default or inherited value",
name );
}

Expand Down Expand Up @@ -534,8 +534,7 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::energy
units::energy out;

// Object via which to report errors which differs for proportional/relative values
JsonObject err = jo;
err.allow_omitted_members();
const JsonObject *err = &jo;
JsonObject relative = jo.get_object( "relative" );
relative.allow_omitted_members();
JsonObject proportional = jo.get_object( "proportional" );
Expand All @@ -545,18 +544,18 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::energy
// such as +10% are well-formed independent of whether they affect base value
if( relative.has_member( name ) ) {
units::energy tmp;
err = relative;
if( !parse( err, tmp ) ) {
err.throw_error( "invalid relative value specified", name );
err = &relative;
if( !parse( *err, tmp ) ) {
err->throw_error( "invalid relative value specified", name );
}
strict = false;
out = val + tmp;

} else if( proportional.has_member( name ) ) {
double scalar;
err = proportional;
if( !err.read( name, scalar ) || scalar <= 0 || scalar == 1 ) {
err.throw_error( "multiplier must be a positive number other than 1", name );
err = &proportional;
if( !err->read( name, scalar ) || scalar <= 0 || scalar == 1 ) {
err->throw_error( "multiplier must be a positive number other than 1", name );
}
strict = false;
out = val * scalar;
Expand All @@ -566,11 +565,12 @@ inline bool assign( const JsonObject &jo, const std::string &name, units::energy
}

if( out < lo || out > hi ) {
err.throw_error( "value outside supported range", name );
err->throw_error( "value outside supported range", name );
}

if( strict && out == val ) {
report_strict_violation( err, "cannot assign explicit value the same as default or inherited value",
report_strict_violation( *err,
"cannot assign explicit value the same as default or inherited value",
name );
}

Expand Down
12 changes: 4 additions & 8 deletions src/npc_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,19 @@ static distribution load_distribution( const JsonObject &jo )

if( jo.has_array( "sum" ) ) {
JsonArray jarr = jo.get_array( "sum" );
JsonObject obj = jarr.next_object();
distribution ret = load_distribution( obj );
distribution ret = load_distribution( jarr.next_object() );
while( jarr.has_more() ) {
obj = jarr.next_object();
ret = ret + load_distribution( obj );
ret = ret + load_distribution( jarr.next_object() );
}

return ret;
}

if( jo.has_array( "mul" ) ) {
JsonArray jarr = jo.get_array( "mul" );
JsonObject obj = jarr.next_object();
distribution ret = load_distribution( obj );
distribution ret = load_distribution( jarr.next_object() );
while( jarr.has_more() ) {
obj = jarr.next_object();
ret = ret * load_distribution( obj );
ret = ret * load_distribution( jarr.next_object() );
}

return ret;
Expand Down

0 comments on commit e7a8ea3

Please sign in to comment.