Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ jobs:
-DCMAKE_CXX_STANDARD=${{matrix.cpp_standard}} \
-DUSE_DATE_POLYFILL=${{matrix.date-polyfill}} \
-DBUILD_TESTS=ON \
-DTRACK_COPIES=ON \
-DENABLE_INTEGRATION_TEST=${{matrix.shared}} \
-DBUILD_EXAMPLES=ON \
-DCREATE_JSON_READER_TARGET=ON \
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/osx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jobs:
-DCMAKE_BUILD_TYPE:STRING=${{matrix.config.name}} \
-DCMAKE_INSTALL_PREFIX=$CONDA_PREFIX \
-DBUILD_TESTS=ON \
-DTRACK_COPIES=ON \
-DENABLE_INTEGRATION_TEST=ON \
-DBUILD_EXAMPLES=ON \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
Expand Down Expand Up @@ -112,4 +113,4 @@ jobs:

- name: Install
working-directory: build
run: cmake --install . --config ${{matrix.config.name}}
run: cmake --install . --config ${{matrix.config.name}}
1 change: 1 addition & 0 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ jobs:
-DCMAKE_PREFIX_PATH=$SPARROW_DEPS_PREFIX \
-DCMAKE_INSTALL_PREFIX=$SPARROW_INSTALL_PREFIX \
-DBUILD_TESTS=ON \
-DTRACK_COPIES=ON \
-DENABLE_INTEGRATION_TEST=${{matrix.toolchain.shared}} \
-DBUILD_EXAMPLES=ON \
-DCREATE_JSON_READER_TARGET=ON \
Expand Down
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ MESSAGE(STATUS "🔧 Enable integration tests: ${ENABLE_INTEGRATION_TEST}")
OPTION(CREATE_JSON_READER_TARGET "Create json_reader target, usualy used for integration tests" OFF)
OPTION(ENABLE_INTEGRATION_TEST "Creates json_reader target and enable integration tests" OFF)
OPTION(CREATE_JSON_READER_TARGET "Create json_reader target, automatically set when ENABLE_INTEGRATION_TEST is ON" OFF)
OPTION(TRACK_COPIES, "Track copies in tests" OFF)

if(ENABLE_INTEGRATION_TEST)
set(CREATE_JSON_READER_TARGET ON)
Expand Down Expand Up @@ -182,6 +183,11 @@ if(SPARROW_CONTRACTS_THROW_ON_FAILURE)
list(APPEND SPARROW_COMPILE_DEFINITIONS SPARROW_CONTRACTS_THROW_ON_FAILURE=1)
endif()

if(TRACK_COPIES)
message(STATUS "Tracking copies in tests")
list(APPEND SPARROW_COMPILE_DEFINITIONS SPARROW_TRACK_COPIES)
endif()

# Build
# =====
set(BINARY_BUILD_DIR "${CMAKE_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}")
Expand Down Expand Up @@ -228,6 +234,9 @@ set(SPARROW_HEADERS
${SPARROW_INCLUDE_DIR}/sparrow/config/config.hpp
${SPARROW_INCLUDE_DIR}/sparrow/config/sparrow_version.hpp

# debug
${SPARROW_INCLUDE_DIR}/sparrow/debug/copy_tracker.hpp

# detail
${SPARROW_INCLUDE_DIR}/sparrow/details/3rdparty/float16_t.hpp

Expand Down Expand Up @@ -324,6 +333,7 @@ set(SPARROW_SRC
${SPARROW_SOURCE_DIR}/arrow_interface/arrow_array_schema_proxy.cpp
${SPARROW_SOURCE_DIR}/arrow_interface/arrow_schema.cpp
${SPARROW_SOURCE_DIR}/arrow_interface/private_data_ownership.cpp
${SPARROW_SOURCE_DIR}/debug/copy_tracker.cpp
${SPARROW_SOURCE_DIR}/layout/array_factory.cpp
${SPARROW_SOURCE_DIR}/layout/array_helper.cpp
${SPARROW_SOURCE_DIR}/layout/fixed_width_binary_array_utils.cpp
Expand Down
28 changes: 28 additions & 0 deletions include/sparrow/debug/copy_tracker.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <string>
#include <vector>

#include "sparrow/config/config.hpp"

namespace sparrow::copy_tracker
{
template <class T>
std::string key();

constexpr bool is_enabled()
{
#ifdef SPARROW_TRACK_COPIES
return true;
#else
return false;
#endif
}

SPARROW_API void increase(const std::string& key);
SPARROW_API void reset(const std::string& key);
SPARROW_API void reset_all();
SPARROW_API int count(const std::string& key, int disabled_value = 0);
SPARROW_API std::vector<std::string> key_list();

}
105 changes: 105 additions & 0 deletions src/debug/copy_tracker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "sparrow/debug/copy_tracker.hpp"

#include <map>
#include <ranges>
#include <string>

namespace sparrow::copy_tracker
{
#ifdef SPARROW_TRACK_COPIES

class copy_tracker_impl
{
public:

static copy_tracker_impl& instance()
{
static copy_tracker_impl inst;
return inst;
}

void increase(const std::string& key)
{
m_copy_count[key] += 1;
}

void reset(const std::string& key)
{
m_copy_count[key] = 0;
}

void reset_all()
{
m_copy_count.clear();
}

int count(const std::string& key)
{
return m_copy_count[key];
}

auto key_list()
{
return std::views::keys(m_copy_count);
}

private:

copy_tracker_impl() = default;

std::map<std::string, int> m_copy_count = {};
};

void increase(const std::string& key)
{
return copy_tracker_impl::instance().increase(key);
}

void reset(const std::string& key)
{
return copy_tracker_impl::instance().reset(key);
}

void reset_all()
{
return copy_tracker_impl::instance().reset_all();
}

int count(const std::string& key, int /*disabled_value*/)
{
return copy_tracker_impl::instance().count(key);
}

std::vector<std::string> key_list()
{
auto keys = copy_tracker_impl::instance().key_list();
return {keys.begin(), keys.end()};
}

#else

void increase(const std::string&)
{
}

void reset(const std::string&)
{
}

void reset_all()
{
}

int count(const std::string&, int disabled_value)
{
return disabled_value;
}

std::vector<std::string> key_list()
{
return {};
}

#endif

}
11 changes: 11 additions & 0 deletions src/record_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@

#include <unordered_set>

#include "sparrow/debug/copy_tracker.hpp"
#include "sparrow/utils/contracts.hpp"

namespace sparrow
{
namespace copy_tracker
{
template <>
SPARROW_API std::string key<record_batch>()
{
return "record_batch";
}
}

record_batch::record_batch(initializer_type init)
{
m_name_list.reserve(init.size());
Expand Down Expand Up @@ -97,6 +107,7 @@ namespace sparrow
, m_array_list(rhs.m_array_list)
{
update_array_map_cache();
copy_tracker::increase(copy_tracker::key<record_batch>());
}

record_batch& record_batch::operator=(const record_batch& rhs)
Expand Down
19 changes: 19 additions & 0 deletions test/test_record_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <string_view>

#include "sparrow/debug/copy_tracker.hpp"
#include "sparrow/primitive_array.hpp"
#include "sparrow/record_batch.hpp"

Expand Down Expand Up @@ -199,9 +200,16 @@ namespace sparrow

TEST_CASE("copy semantic")
{
#ifdef SPARROW_TRACK_COPIES
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should always track the copies in the tests

auto key = std::string("record_batch"); // copy_tracker::key<record_batch>();
copy_tracker::reset(key);
#endif
auto record1 = make_record_batch(col_size);
auto record2(record1);
CHECK_EQ(record1, record2);
#ifdef SPARROW_TRACK_COPIES
CHECK_EQ(copy_tracker::count(key), 1);
#endif

auto record3 = make_record_batch(col_size + 2u);
CHECK_NE(record1, record3);
Expand Down Expand Up @@ -330,6 +338,17 @@ namespace sparrow
);
CHECK(res);
}
#ifdef SPARROW_TRACK_COPIES
TEST_CASE("emplace_back")
{
std::vector<record_batch> vec(3, make_record_batch(col_size));
std::cout << "capacity: " << vec.capacity() << std::endl;
auto key = std::string("record_batch"); // copy_tracker::key<record_batch>();
copy_tracker::reset(key);
vec.emplace_back(make_record_batch(col_size));
CHECK_EQ(copy_tracker::count(key), 0);
}
#endif

#if defined(__cpp_lib_format)
TEST_CASE("formatter")
Expand Down
Loading