-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RDY] Ad hoc string identifiers for terrain + generic id caching (refactoring) #15977
Conversation
8ba9f9e
to
dc1dbba
Compare
@@ -7853,9 +7843,9 @@ void map::draw_rough_circle_ter( ter_id type, int x, int y, int rad ) | |||
}, x, y, rad ); | |||
} | |||
|
|||
void map::draw_rough_circle_ter( std::string type, int x, int y, int rad ) | |||
void map::draw_rough_circle_ter( ter_str_id &type, int x, int y, int rad ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make those references const.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've done it already (as a part of 'Other code improvements'), but didn't push the unfinished commit yet.
The whole caching of int id looks like premature optimization to me. I doubt it's worth the effort. Have you measured that this is really a problem? Besides that, it looks good. |
I have and I'm convinced, that it's worth the spent effort. I'll describe my point more in detail in a summary a bit later. Thank you for the review. |
Regarding id caching: another issue would be invalidating those cached ids when loading a new game. Some string ids are statically allocated, this would mean their cached ids are allocated statically as well. They are not reset when a new world is loaded, but loading new world data may change the mapping of string to int ids. The int ids are sequentially (based on the load order), imagine a world 1 with mod A and mod B (loading in that order) and a world 2 with mod B and mod A (in that order). In world 1, the terrain from mod A gets id 100,101, ... and from mod B would get 200,201, ... (example numbers). In world 2, the mod B is loaded first and its terrain would now get ids 100,101, ... |
b289bc9
to
aa14509
Compare
Summary (apart from the stated above): Correct me if I'm wrong, but currently we have two equal copies of each loaded Using cached int is approx 50-70 times faster, than hashmap lookup (~25 times faster if it needs to recheck the index, but it shall only occur when objects are deleted/reloaded). We don't need to worry about performance loss anymore (previous // the first condition checks int range, the second checks matching
if( tid.is_valid() && terlist[tid].id == *this ) { ... Also, I don't see any reason why we should distinguish empty-ids ( if( !ter.open.empty() && ter.open != "t_null" ) { ... Any optional id can be initially set to if( ter.open.is_null() ) { ...
// or even
if( ter.open ) { ... // omit is_null() as BevapDin proposed My next goal is making the template<>
bool string_id<ter_t>::is_valid() const
{
return terrain_factoy.is_valid( *this );
}
template<>
int_id<ter_t> string_id<ter_t>::id() const
{
return terrain_factoy.get_int_id( *this );
} Also, forthcoming PRs will decouple terrain and furniture further. |
915c382
to
aa14509
Compare
Anything else to do before it's mergeable? A couple of my next PRs depend on it. |
78d221c
to
829438f
Compare
Has a merge conflict, I assume due to the astyling |
I'll mark it as a priority to make it stand out despite being "buried" under other PRs. If you have something important depending on it, you can branch it out of this one and note in the PR that you did that. |
@@ -3,6 +3,8 @@ | |||
|
|||
#include <string> | |||
#include <type_traits> | |||
#include <memory> | |||
#include "debug.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are these includes needed here? I can't see anything in this files that uses their content.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well spotted. Thanks. I had added them for debugging purposes, but then forgot to delete.
It looks fined, besides the strange includes. Are they really needed, it compiles and runs fine without them, for me. |
This commit doesn't aim introducing full-blown constructors for structs other than 'ter_t'
…timisation) Why was it made that way? Same for the furniture, but I won't touch it here. Also, if a terrain string id is not found in the map and the id should be converted to represent the right index, then let it be converted only once and put the converted value into the map.
Add the validity check and allow constructing an 'int_id' from a string. It simplifies the syntax and should be mostly harmless.
It's meant to be generic and the 'generic_factory' should take care of the caching. Therefore as soon as it's done, no need to copy-paste the boilerplate code for every class.
829438f
to
9f35019
Compare
Rebased against |
Rationale:
Provide convenient access to terrain objects using the existing tool (
string_id<T>
)Make usage of the
generic_factory
class (or its derivative) possible for handling terrain. The current terrain code looks pretty old (probably it's mostly a bequest from the original game).Get templated loading of ids from JSON files
Get rid of the hard-coded terrain id tables (at least try to)
And the last, but not least. While working on JSONize gates #15945 I realised how convenient it would be to cache int ids inside string ids (see the d22c736 - there's an ugly attempt, but it reveals the intent. dc1dbba088c1f756a62edfe420cda9640a527ec4 - the same code after). Since we don't remove existing prototype objects separately, we're in position to kill two birds with one stone:
Edit: even if we do delete them, there's still a good way to cache ids.
Done:
I addedReverted and made a separate extended PRfilename
andlinenumber
to the displayeddebugmsg()
for testing purposes (will revert the commit later if it's not desirable)the existing map lookup for the time beingcached valueter_t
andfurn_t
now have different identifier types (same plain strings for furniture --> another PR later)partlyencapsulated~~, but there's still some work to do~~.TODO:
terfind()
'stermap
insidemapdata.cpp
and don't export itint_id<T>
cachingloadid