Skip to content

perf: faster loading of native models and meshes - #1419

Open
BotellaA wants to merge 8 commits into
nextfrom
perf/faster-loading
Open

BotellaA wants to merge 8 commits into
nextfrom
perf/faster-loading

Conversation

@BotellaA

Copy link
Copy Markdown
Member

Faster loading of native models and meshes. On a 298 MB structural model (39 blocks, 4.9 M tets, 899 k unique vertices), load_structural_model goes from 1.48 s to 0.36 s on the existing file, and to 0.19 s once the file is re-saved. Goes with the OpenGeode-Geosciences PR on the same branch name (perf/faster-loading).

Benchmarks (idle machine, Release, clang, median of 7 runs)

Whole load of 3_meshed_300m.og_strm, through load_structural_model:

time max RSS anonymous RSS
next 1.479 s 485 MB 461 MB
this branch, same file (298 MB) 0.360 s 561 MB 481 MB
this branch, file re-saved (232 MB) 0.192 s 472 MB 442 MB

The extra max RSS on the old file is memory-mapped archive pages: clean, file-backed and reclaimable.

Gain of each commit, against the previous commit in the same session. The model is loaded as a BRep from the same archive.

commit measured on before after
CMake: minizip-ng with zlib-ng (fast CRC32) full load 1.575 s 1.054 s
no random uuid while deserializing full load / unique vertices 1.018 / 0.551 s 0.854 / 0.382 s
bitsery from memory buffers full load / unique vertices / 88 MB .og_tso3d 0.854 / 0.382 / 0.116 s 0.793 / 0.283 / 0.102 s
VertexIdentifier lists rebuilt on load full load of a re-saved file 0.745–0.816 s 0.571–0.580 s, max RSS 550 → 478 MB, file −55 MB
archives read in place full load (new / old format), 88 MB .og_tso3d 0.548 / 0.781 / 0.100 s 0.247 / 0.416 / 0.062 s

Changes

  • CMake.
    • minizip-ng is built with MZ_ZLIB on the zlib-ng we already build, which gives a hardware CRC32 and support for DEFLATE archives.
    • zlib and minizip symbols are no longer exported from libOpenGeode_basic (--exclude-libs with GNU ld/lld), so a system libz (e.g. in Python) cannot take their place.
  • Serialization.
    • Objects about to be deserialized no longer generate a random uuid. This adds uuid( BITSERY ), bitsery::Access::create specializations and (BITSERY) constructors on Identifier, VertexSet and the meshes. Loading this model generates 5 k uuids instead of 1.77 M.
    • geode::Deserializer now reads a geode::ByteSpan through bitsery::InputBufferAdapter. Mesh, raster and grid readers and writers open UTF-8 paths.
  • VertexIdentifier, serialization version 5.
    • The component mesh vertex lists are no longer saved. repair_component_vertices rebuilds them from each mesh's unique vertex attribute, in a fixed order: corners, lines, surfaces, blocks, each in model order, then increasing vertex index.
    • Older versions still load.
    • Files saved by this version cannot be loaded by earlier releases.
  • Archives read in place.
    • detail::InputFiles has a directory implementation and an archive implementation. The archive is memory-mapped: STORED entries are read without copying, DEFLATE entries are inflated, and every entry's CRC is checked by the thread that loads it. If the archive cannot be mapped, entries are read with positional reads.
    • Model loaders take const detail::InputFiles&. The public Builder::load_*( directory ) methods remain as wrappers.
    • Native inputs gained Input::read_bytes for loading from memory. An input that cannot read from memory gets a temporary file.
    • Nothing is extracted to the temp directory any more.

Notes for review

  • Detail API changes. The OpenGeode-Geosciences PR is updated accordingly.
    • load_brep_files, load_section_files and ComponentsStorage::load_components take InputFiles.
    • The Deserializer type changed, so code that builds it on a stream must switch to detail::deserialize_bytes, or to a ByteSpan from detail::map_file_content.
  • Component vertex order. After a load, component_mesh_vertices( unique_vertex ) follows the order above, which can differ from the order before saving. Code that takes front() gets a vertex at the same position but possibly from another component, so reference outputs downstream may change.
  • Windows. The memory-mapping code (CreateFileMappingW / MapViewOfFile) has not been compiled locally; this is its first CI run. Symbol hiding only applies to GNU ld/lld.
  • Known limits. Implicit Geosciences models still write one temporary file for their nested horizons stack. Prepended (self-extracting) zips are not supported.

Tests

  • New tests:
    • test-input-files: directory and archive, CRC, corrupted DEFLATE, truncated entries, temp-file errors.
    • test-mesh-input-bytes
    • test-model-archive-input: every test model loaded from the archive and from the extracted directory; nothing written to TMPDIR; a corrupted entry is named in the error.
    • test-model-deflate-archive
    • test-hidden-zlib-symbols
  • Legacy files: backward_io/v20_2 (BRep and Section, VertexIdentifier v4).
  • Extended tests: UTF-8 path round trips, truncated files, non-nil ids after loading old meshes and models, round trips of the component vertex order.
  • Results: 221/221 with clang Release (Python included); the touched libraries and tests also build and pass with GCC, assertions on.

🤖 Generated with Claude Code

BotellaA and others added 5 commits November 23, 2026 19:07
minizip-ng was built without zlib (MZ_ZLIB OFF), so every zip read and write
computed the CRC32 with its byte-wise software table, most of the unzip time.
It now links the zlib-ng of the superbuild (PCLMUL CRC32), without fetching
its own libraries. OpenGeode finds zlib-ng before minizip-ng and makes the
find_dependency(ZLIB) of minizip-ng use config mode, in the build and in the
exported package, so that no system zlib is picked.

DEFLATE-compressed archives (e.g. a model re-zipped with a standard tool) are
now readable. OpenGeode still writes stored entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Bitsery default-constructs every element it deserializes: each
ComponentMeshVertex, uuid map key, mesh and attribute identifier generated
a random UUIDv7 immediately overwritten by the file content (1.77 M
generations when loading a 900 k unique vertices model).

Add non-generating uuid(BITSERY), ComponentID(BITSERY), Identifier(BITSERY)
and VertexSet/PointSet(BITSERY) constructors, specialize
bitsery::Access::create for uuid and ComponentID, and use them where the
object is built only to be deserialized. Old file versions that do not store
an identifier still get a generated one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
geode::Deserializer read through bitsery::InputStreamAdapter: every value
was a virtual std::streambuf call. It now reads a geode::ByteSpan (read-only
bytes, owned elsewhere) with bitsery::InputBufferAdapter, so that both a file
read in memory and a slice of an archive in memory can be deserialized.

Add detail::read_file_content (whole file in one read, UTF-8 path) and
detail::deserialize_bytes (contexts of every registered library and the
usual completion check), and use them at every native read site: identifier,
relationships, unique vertices, physical properties, components, meshes,
light regular grids, raster images and external time series. Native mesh,
light regular grid and raster image files are now read and written with
UTF-8 paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d of saving them

The vertices file duplicated the "unique vertices" attribute of each
component mesh: version 5 only stores the number of unique vertices and
the attribute id. repair_component_vertices rebuilds the component
vertices from the mesh attributes (components in registration order, i.e.
the model order when loading, then increasing vertex indices) and removes
links to unknown unique vertices with a warning. Versions 1 to 4 read the
same bytes into a temporary vertex set and keep the previous repair.
VertexIdentifier no longer owns a vertex set and attribute manager.

Compatibility: files saved by this version cannot be loaded by earlier
releases (they do not know the version 5 of the vertices file); files
saved by earlier releases still load.

Tests: component vertices are compared as sets after a reload, since the
order after load is the rebuild order (checked separately, and identical
for two loads); the standalone save/load test loads the components and
repairs; legacy v20_2 BRep/Section files cover the old format, and the
missing attribute restoration uses the legacy BRep file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Loading a BRep or a Section extracted every entry of the archive to the
temporary directory, then read the files back. The archive is now read in
place through geode::detail::InputFiles: the archive is memory-mapped
(positional reads if it cannot be mapped), stored entries are zero-copy
slices of the mapping, deflated entries are inflated with zlib-ng, and the
CRC-32 of each entry is checked when it is read (a mismatch is a data error
naming the entry). The pages of an entry leave the process memory once it is
deserialized.

Native mesh, light regular grid and raster inputs read from bytes
(Input::read_bytes); other inputs of an archive entry get a temporary file.
Plain files (meshes loaded by path, model directories) are memory-mapped
instead of copied into a buffer. The load_* builder methods taking a
directory are kept as wrappers of the directory input files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@BotellaA
BotellaA requested a review from BenPinet November 23, 2026 18:24
BotellaA and others added 3 commits November 23, 2026 18:25
minizip-ng is only exported by static OpenGeode builds, so the tests
configured against a shared install could not find MINIZIP::minizip-ng.
DEFLATE archives are now pre-generated data files (Python zipfile,
ZIP_DEFLATED); stored archives are written by the tests themselves or by
geode::ZipFile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…type

Input::read_bytes fills a vector given by reference and returns false when
the input can only read files, instead of returning std::optional of the
objects: MSVC instantiates the inline virtual body with the class, which
needed the complete mesh type in headers that only forward declare it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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