Skip to content

vertex unique_name storage & lookup #63

Open

Description

@yakra

Vertex unique_name labels are stored:

  • with waypoints
  • with vertices
  • as keys of unique_waypoints dict/map
  • as keys of vertices dict/map

How much memory do these use up?

Potential things to do:

  1. Convert vertices map to use Waypoint*s as keys rather than unique_name
    A. Reduced RAM footprint
    B. Most likely faster lookup times
  2. unique_waypoints map, we can keep: it's essentially used as a set to store unique point names (which we need), with associated colocation lists.
    A. The only lookup we use is in inserting unique point names, like a set. Otherwise, after it's created we just iterate thru & pass the mapped values to the vertex ctor. No name->point list lookup. Thus no CPU time hit on that front.
    B. What we can do is delete the singleton waypoint lists when finished constructing vertices.
    C. Better yet, we don't even need lists. Just map a Waypoint* instead, and pass that to the vertex ctor.
  3. For vertex objects, we can use a method rather than store unique_name, as per Jim's note.
    A. We can store a pointer to the string in the unique_waypoints (though I don't know how the equivalent might be done in Python) This lets us...
  4. Delete unique names from Waypoints when finished constructing vertices, worst case.

Do we: move unique_name creation into vertex construction, avoiding storing them in waypoints entirely?

  • Doing so would require already having a set of unique coords/waypoints from which to construct vertices.
  • Creating two sets (waypoints, then unique labels) may end up taking more time.
  • Per-waypoint unique_name creation is skipped anyway for points already visited, so no big CPU hit here. May still be faster than creating two unordered_sets.
  • So, it may not make sense to reinvent the wheel...
  • We can erase them from waypoints when done anyway, avoiding the problem of permanent RAM usage. The biggest downside here is having leftover empty string containers for each waypoint.
  • To that end, maybe use a bool to track whether a unique name has been created & skip, and store unique names as unique_waypoints keys only.

See also: #8

Activity

  1. yakra commented on Feb 1, 2019

    @yakra
    OwnerAuthor

    How much memory do these use up?

    11863199 bytes for unique names stored in waypoints, with null terminators
    7359944 bytes for unique names stored in vertices, with null terminators

    3 * 7,359,944 + 11,863,199 = 33,943,031.
    I can save 26,583,087 B by going down to just one unique set.
    Not counting whatever the sizeof overhead of std::string is.
    Not small potatoes, but not wicked big spuds eithah.

  2. yakra commented on Feb 1, 2019

    @yakra
    OwnerAuthor

    sizeof std::string is 32B.
    So, 3 * 24647560 + 37420959 = 111363639, with potential to save 86716079 B.

  3. yakra commented on Feb 6, 2019

    @yakra
    OwnerAuthor

    Do we: move unique_name creation into vertex construction, avoiding storing them in waypoints entirely?

    Rethinking this...

    Doing so would require already having a set of unique coords/waypoints from which to construct vertices.

    We already have this, in the form of colocation lists. Construct vertices only for singleton points or those at the front of their colocation lists.

    Creating two sets (waypoints, then unique labels) may end up taking more time.

    No need to create two sets. It may take a small amount more time to iterate thru all waypoints and check whether !w->colocated || w == w->colocated.front() (C++) or w.colocated is none or w == w.colocated[0] (Python) than to just iterate thru a known set, though this may be about as complicated as, or even a bit less then, creating a new set.
    Factor in dict/map lookups by Waypoint memory address rather than unique_name string, and we're most certainly winning.
    RAM will be a definite win, of course.

    The path of less resistance, in terms of adapting the existing code, may be to keep unique name generation where it is in the HighwayGraph ctor, and pass a constructed name to the HighwayGraphVertexInfo ctor, even if it's one more variable to pass (beyond the waypoint itself).

    OTOH, it may be nice to clean up & combine the canonical name & unique name routines into one. The HighwayGraphVertexInfo ctor seems the most sensible place to do this.

    Maybe take the easy path as a temporary solution, then work out the cleaner option long-term.

  4. yakra commented on Feb 6, 2019

    @yakra
    OwnerAuthor

    Vertex unique_name labels are stored, in chronological order:

    • as keys of unique_waypoints dict/map
      • Eliminate dict/map: we can determine unique waypoints via colocation lists. Just keep a set of unique names instead.
    • within waypoints
      • Eliminate. Create upon vertex construction; store in within vertices.
    • within vertices themselves
      • C++: Store a pointer to the item within the set.
      • Python: I don't see an easy way around storing in both the set and the vertex object itself.
    • as keys of vertices dict/map
      • Eliminate. Use Waypoint* as key instead.

    Potential things to do:

    1. Convert vertices map to use Waypoint*s as keys rather than unique_name
      A. Reduced RAM footprint
      B. Most likely faster lookup times
    2. Lose unique_waypoints map.
      A. Iterate thru waypoints. Process singleton points, and points at the front of their colocation lists.
      B. Store unique point names in a set.
      C. Have HighwayGraphVertexInfo store, and its ctor receive, a single Waypoint rather than a (in the case of singleton points, ad hoc) colocation list.
    3. C++: Vertex ctor gets a Waypoint* & a pointer to its unique name within the set.
      Python: Vertex ctor gets a Waypoint, and still stores the full unique name string.
    4. No unique names in waypoints. Compute once & pass to vertex ctor only.
  5. yakra commented on Feb 7, 2019

    @yakra
    OwnerAuthor

    Potential things to do:

    Implemented in C++.
    I ended up doing the "pointer into the set values" thing, probably out of still being a bit hung up on the idea of unique_waypoints as a map, and needing to keep its values around. I could have just copied the string values themselves into the vertex objects, then deleted the set once done constructing vertices. It would be more intuitive, at least. (Speed, though? Lots of copy constructors would be involved... Maybe check it out in the future...)
    Luckily though, I can do this when I translate back to Python: Clear the set when done, and store unique names in vertex objects only. I still end up storing names in one place, not four, or even two.

  6. yakra commented on Feb 7, 2019

    @yakra
    OwnerAuthor

    Python implementation: TravelMapping#190

    Leaving this open to remind myself:

    I could have just copied the string values themselves into the vertex objects, then deleted the set once done constructing vertices. It would be more intuitive, at least. (Speed, though? Lots of copy constructors would be involved... Maybe check it out in the future...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions