mapbox / mapbox/variant

sizeof(variant)

Open
#19 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
384
Forks
96
PR merge metrics
No merged PRs in 30d

Description

My understanding is that calling `sizeof` on a variant should produce the size of the largest type the variant includes. And my assumption is that `sizeof` for a given type is not going to be consistent/portable across arches and platforms. However, as we should strive to have the variant use at little memory as possible I wanted to surface this ticket for discussion.

Dumb questions that I assume the answer is `duh, no`, but want to know for sure:
- Would it make sense to try to add tests of `sizeof(variant_instance)` by adapting to how `sizeof(std::string)` and other types might be different per platform? Might catch regressions if we ever made a mistake that increased the variant memory footprint.
- Are there any optimizations to be had from learning/applying ideas from http://www.catb.org/esr/structure-packing/?
- For custom types should we add some kind of MAX_VARIANT_SIZEOF flag to allow clamping the size - this would then be able to catch a situation were a poorly aligned custom type is larger than other built-in types used in the variant

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the questions about sizeof(variant_instance), platform-dependent sizeof(std::string), structure packing, and custom types. Determine whether portable size-regression tests, packing changes, or a MAX_VARIANT_SIZEOF option are in scope; done requires a maintainer decision and corresponding tests or implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.