Skip to content
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

[ntuple] Handling of over-aligned types #16757

Open
hahnjo opened this issue Oct 28, 2024 · 1 comment
Open

[ntuple] Handling of over-aligned types #16757

hahnjo opened this issue Oct 28, 2024 · 1 comment
Assignees

Comments

@hahnjo
Copy link
Member

hahnjo commented Oct 28, 2024

Description

In some type-punned fields, we make assumptions that the alignment is smaller than alignof(std::max_align_t).

Reproducer

This breaks in certain combinations of types, for example a std::vector of an over-aligned type:

#include <ROOT/RNTupleModel.hxx>
#include <ROOT/RNTupleReader.hxx>
#include <ROOT/RNTupleWriter.hxx>

#include <sstream>
#include <vector>

struct alignas(64) CacheLine {
  int i = 0;
};

void ntuple_aligned() {
  auto model = ROOT::Experimental::RNTupleModel::Create();
  using Vector = std::vector<CacheLine>;
  model->MakeField<Vector>("v");

  {
    auto writeModel = model->Clone();
    auto vPtr = writeModel->GetDefaultEntry().GetPtr<Vector>("v");
    auto writer = ROOT::Experimental::RNTupleWriter::Recreate(std::move(writeModel), "ntuple", "ntuple_aligned.root");

    vPtr->push_back({});
    writer->Fill();
  }

  auto readModel = model->Clone();
  auto vPtr = readModel->GetDefaultEntry().GetPtr<Vector>("v");
  auto reader = ROOT::Experimental::RNTupleReader::Open(std::move(readModel), "ntuple", "ntuple_aligned.root");
  reader->LoadEntry(0);

  auto ptr = &vPtr->at(0);
  auto uptr = reinterpret_cast<std::uintptr_t>(ptr);
  // Do a small dance to prevent the compiler from optimizing the alignment check...
  std::stringstream ss;
  ss << uptr;
  ss >> uptr;
  std::cout << ptr << " (" << uptr << ") % 64 = " << (uptr % 64) << std::endl;
}

possible output:

0x48c9230 (76321328) % 64 = 48
@hahnjo
Copy link
Member Author

hahnjo commented Oct 28, 2024

Some other fields try to get this right, for example RRecordField. We should investigate if we can make std::vector<char> allocate over-aligned memory areas. The other option is to forbid over-aligned types (and possibly simplify some other code).

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

No branches or pull requests

2 participants