Skip to content

No singletons, nav/world: nav, world, physics and the SDF library keep no process state [4.0, breaking] - #1184

Closed
transfix wants to merge 1 commit into
p0a/no-singletons-ariadne-4from
p0a/no-singletons-nav-world
Closed

transfix wants to merge 1 commit into
p0a/no-singletons-ariadne-4from
p0a/no-singletons-nav-world

Conversation

@transfix

Copy link
Copy Markdown
Owner

No singletons, nav/world: nav, world, physics and the SDF library keep no process state. Stacked on #1176 (Ariadne part 3); a sibling of #1182 and #1183. Breaking (4.0).

What moved, and the bugs it fixes

  • The world generators: the terrain and city generator registries are each app's own (an app.slot), and every app starts with the built-ins only. world_builder looks its generators up when it is made and keeps them, not the app. An unknown name still throws at the first step.
  • surface_registry's three ontologies are constexpr tables, with the same hash.
  • Road grading: graded_terrain::sample / graded_at keep their candidate lists on the stack, not in thread-locals.
  • sim_world: the CVC_NAV_PROFILE flag and the per-phase timing sums are each world's. The flag is read when the world is made, and policy_key(0)'s empty string is a member.
  • The plant ledger's item tables are constexpr arrays.
  • grid_nav's scratch buffers are no longer thread-locals:
    • build_sdf and astar allocate their own;
    • an astar_batch call's queries share a pool;
    • sense_batch takes a caller-owned sense_scratch, which sim_world keeps across ticks.
  • world_binding: each binding tracks the threads writing its own keys, and whether a pull read a key through a link is a member under its mutex. Bug fixed: one binding's writes were hidden from another binding's watch on the same thread.
  • Jolt: the heightfield rotation is built where it is used.
  • The vendored SDF library: its legacy globals and init_all_vars were dead duplicates of SDFContext's members, and are gone. Every computation's state is its own SDFContext's. INF is constexpr, and main.cpp's unused thread-local context is gone.

Public API changes

  • These take the app first:
    • register_terrain_generator, terrain_generator_names, make_terrain;
    • register_city_generator, city_generator_names;
    • to_world_params, generate_world, world_builder's constructor;
    • ariadne::make_height_field.
  • surface_registry::ontology() returns const char *, and the class has class_count.
  • plant_ledger_vehicle() / plant_ledger_episode() return std::span<const plant_ledger_item>.
  • New nav::sense_scratch; sense_batch(..., sense_scratch *scratch = nullptr).
  • pycvc's generate_world uses the given app's generators. Its app was already optional; without one, a temporary app with only the built-ins is made, and nothing is shared.

Callers updated:

  • Ariadne's scene code;
  • cvcGL's world nodes;
  • terrain_lab;
  • a cvcGL test;
  • bundle.cpp;
  • the pycvc README and docs/ARIADNE.md.

Allowlist: -72 lines (cvc 71, cvc_physics_jolt 1)

  • SDF: 26 globals in head.cpp, plus octree.cpp's INF;
  • nav/grid_nav.cpp 6, nav/plant_ledger.cpp 4, nav/sim_world.cpp 14;
  • physics/world_binding.cpp 2;
  • world/generated_world.cpp 4, world/road_grade.cpp 6, world/surface.cpp 8;
  • Jolt's kHeightfieldRotation.

Not removed (please review)

jolt_runtime::state()::s stays: the use count and mutex around Jolt's own process globals (Factory::sInstance, RegisterTypes' registry, the allocator, Trace and AssertFailed hooks). Jolt has one of each per process. Counting per app would let one app's last world tear Jolt down while another app's world still uses it.

Tests

  • world_generated_test:
    • GeneratorsAreEachAppsOwn: two apps in both orders, and a later app starts clean;
    • GeneratorsOfAppsOnThreads;
    • GeneratorsByName, tightened: a new app has exactly the built-ins. The old check allowed extras only because other tests registered process-wide.
  • nav_test ScratchIsTheCallersAndShareable.
  • nav_sim_world_test WorldsOfSeveralAppsAreEachTheirOwn.
  • physics_world_test BindingsOfSeveralAppsAreEachTheirOwn.
  • geometry_test SDFV1OfSeveralAppsAreEachTheirOwn (16³ and 32³ from several apps: alone, both orders, threads, a later app).
  • world_roads_test SamplingOnThreadsAtOnceIsSamplingAlone.
  • multi_app_integration_test gains a nav_world part. Each app:
    • registers and samples its own terrain generator;
    • computes an SDF of its own size;
    • steps a sim_world from its own tree;
    • binds a physics world to its own gravity.

Locally (MSVC Release, -j 4):

  • app_test, multi_app_integration_test;
  • the 31 world_* / nav_* tests run, and all 12 physics_*;
  • geometry*, algorithm_test, mtxlib_test;
  • ariadne_loader_test, ariadne_scene_world_test;
  • the ctests cvcgl_nav_stats_publish, cvcgl_ariadne_realize, pycvc_plant_host, pycvc_nav_agents, pycvc_nav_belief, pycvc_nav_routes, physics_replay_test.

CI

Full run: the push to ci/full/no-singletons-nav-world, https://github.com/transfix/libcvc/actions/runs/43211874618.

…p no process state

Nav, world, physics and the vendored SDF library kept process-wide state:
caches, counters, flags, registries and thread-locals. They are each
app's own now (app.slot), or the object's that does the work, or
constants. Allowlist -72 lines (cvc 261 -> 190, cvc_physics_jolt 2 -> 1).

- world: the terrain and city generators are each app's own (slot
  cvc.world.generators): register_terrain_generator, terrain_generator_names,
  make_terrain, register_city_generator, city_generator_names,
  to_world_params, generate_world and world_builder take the app. Every
  app starts with the built-ins; what one registers, another never sees.
  world_builder looks both generators up as it is made (it keeps them,
  not the app); an unknown name still fails at the first step.
  ariadne's make_height_field takes the app too. pycvc's generate_world
  uses the app's generators, or the built-ins without an app.
- world: surface_registry's three ontologies are constexpr tables built
  at compile time (read-only data); ontology() returns const char *.
  graded_terrain::sample / graded_at keep their candidates on the stack
  (small_vector), not in thread-locals.
- nav: sim_world's CVC_NAV_PROFILE flag and its per-phase sums are the
  world's (read when it is made), and policy_key(0)'s empty string is a
  member. The plant ledger's items are constexpr tables;
  plant_ledger_vehicle / plant_ledger_episode return std::span.
  grid_nav's scratch thread-locals are gone: build_sdf and astar make
  their own, astar_batch's queries share a pool, and sense_batch takes a
  caller-owned sense_scratch (a sim_world keeps one, tick to tick).
- physics: world_binding's thread-locals are the binding's: who is
  writing its own keys (shared with its tree watch) and whether a pull
  read a key through a link. jolt_backend's heightfield rotation is made
  where used.
- geometry/SDF: the legacy API's 26 globals in head.cpp (and
  init_all_vars, which reset them) are gone; every computation's state
  is its SDFContext's. octree.cpp's INF is constexpr; main.cpp's unused
  thread-local context is gone.

Left on purpose: jolt_backend's jolt_runtime::state() use count and mutex.
Jolt's factory, type registry, allocator hooks and trace/assert hooks are
the library's process globals; the count guards their one setup and
teardown across every world in the process, so it cannot be per app.

Tests: world_generated_test adds GeneratorsAreEachAppsOwn (two apps in
both orders, a later app clean) and GeneratorsOfAppsOnThreads, and checks
a new app's generators are the built-ins exactly (the old test allowed
more because other tests registered process-wide). nav_test adds
ScratchIsTheCallersAndShareable; nav_sim_world_test adds
WorldsOfSeveralAppsAreEachTheirOwn; physics_world_test adds
BindingsOfSeveralAppsAreEachTheirOwn; geometry_test adds
SDFV1OfSeveralAppsAreEachTheirOwn; world_roads_test adds
SamplingOnThreadsAtOnceIsSamplingAlone. multi_app_integration_test's
workload adds nav_world: each app registers its own terrain generator,
computes an SDF of its own size, steps a sim_world from its own tree and
binds a physics world to its own gravity.
@transfix

Copy link
Copy Markdown
Owner Author

Replaced by #1203, which lands the whole no-singletons stack as one PR. This branch's head is an ancestor of #1203's branch, so its commits are all there; the branch is kept.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant