libjoint Design Limitations

This document describes design limitations discovered during comprehensive testing. Some limitations have been addressed in subsequent versions.

Summary of Current Status (v1.2.2)

LimitationStatusVersion Fixed
TI_MASK Limit (~2048 intervals)FIXEDv1.2.0
SPLITS_WHO_MASK Limit (256 entities)FIXEDv1.2.0
Extreme Timestamps (overflow risk)FIXEDv1.2.0
UINT32_MAX Entity ID (sentinel conflict)FIXEDv1.2.0
File Persistence (corm bugs)FIXEDv1.2.1
MV Index Write Efficiency (W3 regression)FIXEDv1.2.2
Zero-Duration Intervals (start == stop)NOT FIXEDBy Design

See CHANGELOG.md for v1.2.2 implementation details.


Fixed Limitations (as of v1.2.2)

The following limitations have been addressed:

1. TI_MASK Limit: Maximum ~2048 Intervals β†’ FIXED βœ…

Status: FIXED in v1.2.0 (increased to 65,536 intervals)

Original Problem (v1.1.0)

Description

The TI_MASK constant in src/libjoint.c:28 is defined as 0x7FF (2047), which limits the corm database to a maximum of approximately 2048 intervals.

Source Code

#define TI_MASK 0x7FF  // src/libjoint.c:28

This mask is used when opening the corm databases:

dbs->ti = corm_open(fname, "ti", qm_ti, qm_ti, TI_MASK, flags);  // line 169
dbs->max = corm_open(NULL, NULL, qm_time, qm_ti, TI_MASK, CM_SORTED);  // line 170
dbs->id = corm_open(NULL, NULL, qm_id, qm_ti, TI_MASK, CM_SORTED);  // line 171

Impact

Evidence

Test case demonstrating the limit:

// Test with 10,000 intervals
for (int i = 0; i < 10000; i++) {
    joint_start(jd, 1000 + i * 100, i + 1);
    joint_stop(jd, 1000 + i * 100 + 50, i + 1);
}

// Query all intervals
joint_cur_t cur = joint_iter(jd, 0, 2000000);
int found = 0;
while (joint_next(&min, &max, &count, &who, &cur)) {
    found++;
}
// Result: found = 2048 (expected 10000)

Query results by range:

Workaround

`c #define TI_MASK 0xFFFF // 65535 max intervals `

Related Tests

Fix (v1.2.0)

Changed: TI_MASK from 0x7FF to 0xFFFF (src/libjoint.c:28)

Test coverage (v1.2.0):


2. SPLITS_WHO_MASK Limit: Maximum 256 Entities Per Split β†’ FIXED βœ…

Status: FIXED in v1.2.0 (increased to 4,096 entities)

Original Problem (v1.1.0)

Description

The SPLITS_WHO_MASK constant in src/libjoint.c:27 is defined as 0xFF (255), which limits each split interval to a maximum of 256 entity IDs.

Source Code

#define SPLITS_WHO_MASK 0xFF  // src/libjoint.c:27

This mask is used when creating the temporary corm for entity IDs during split computation:

uint32_t who_hd = corm_open(NULL, NULL, CM_HNDL, CM_HNDL, SPLITS_WHO_MASK, 0);  // line 430

Impact

Evidence

Test case demonstrating the limit:

// Create 1000 overlapping intervals (all at same time)
for (int i = 0; i < 1000; i++) {
    joint_start(jd, 5000, i + 1);  // All start at 5000
    joint_stop(jd, 10000, i + 1);  // All end at 10000
}

// Query the overlapping region
joint_cur_t cur = joint_iter(jd, 6000, 8000);
int found = 0;
while (joint_next(&min, &max, &count, &who, &cur)) {
    found++;
}
// Result: found = 256 (expected 1000)

Workaround

`c #define SPLITS_WHO_MASK 0xFFF // 4095 max entities per split ` Note: Larger masks increase memory usage for the temporary corm

Related Tests

Fix (v1.2.0)

Changed: SPLITS_WHO_MASK from 0xFF to 0xFFF (src/libjoint.c:27)

Test coverage (v1.2.0):


3. Extreme Timestamps: INT64_MAX Overflow β†’ FIXED βœ…

Status: FIXED in v1.2.0 (input validation added)

Original Problem (v1.1.0)

Description

Timestamps near INT64_MAX (9,223,372,036,854,775,807) may cause overflow or undefined behavior in libjoint's internal calculations.

Source Code

The issue stems from timestamp arithmetic in libjoint and corm, particularly when:

Relevant constants in src/libjoint.c:

#ifdef __OpenBSD__
#define TS_MIN LLONG_MIN
#define TS_MAX LLONG_MAX
#else
#define TS_MIN LONG_MIN
#define TS_MAX LONG_MAX
#endif

const time_t mtinf = (time_t) TS_MIN;  // minus infinite
const time_t tinf = (time_t) TS_MAX;   // infinite

Impact

Evidence

Test case demonstrating the issue:

time_t huge = 9223372036854775000LL;  // Near INT64_MAX
joint_start(jd, huge - 1000, 1);
joint_stop(jd, huge, 1);

// Query for the interval
joint_cur_t cur = joint_iter(jd, huge - 2000, huge + 1000);
int found = 0;
while (joint_next(&min, &max, &count, &who, &cur)) {
    if (who == 1) found++;
}
// Result: found = 0 (expected 1)

Negative timestamps work correctly:

joint_start(jd, -1000000, 2);
joint_stop(jd, -999000, 2);

joint_cur_t cur = joint_iter(jd, -1001000, -998000);
// Result: Found correctly

Workaround

Platform Considerations

Related Tests

Fix (v1.2.0)

Added: Input validation to joint_start() and joint_stop()

API changes:

int joint_start(unsigned jd, time_t ts, unsigned id);
int joint_stop(unsigned jd, time_t ts, unsigned id);
// Return values:
//   0 = success
//   1 = duplicate start / no open interval
//  -1 = validation error (errno = ERANGE for timestamp overflow)

Test coverage (v1.2.0):


4. UINT32_MAX Entity ID: IDM_MISS Sentinel Conflict β†’ FIXED βœ…

Status: FIXED in v1.2.0 (input validation added)

Original Problem (v1.1.0)

Description

The entity ID value UINT32_MAX (0xFFFFFFFF or 4,294,967,295) conflicts with the IDM_MISS sentinel value used by the corm IDM (ID Manager) module.

Source Code

From corm/include/ttypt/idm.h:26:

#define IDM_MISS ((uint32_t)-1)  // = 0xFFFFFFFF = UINT32_MAX

The ids_pop() function returns IDM_MISS when no more IDs are available:

uint32_t ids_pop(ids_t *list);  // Returns IDM_MISS if empty

In src/libjoint.c:594, libjoint uses this to detect end of iteration:

while ((*who = ids_pop(&internal->next->ids)) == (uint32_t) -1) {
    internal->next = TAILQ_NEXT(internal->next, entry);
    if (!internal->next)
        return 0;
}

Impact

Evidence

Test case demonstrating the conflict:

// Insert with UINT32_MAX entity ID
joint_start(jd, 3000, (unsigned)-1);  // UINT32_MAX
joint_stop(jd, 4000, (unsigned)-1);

// Query for the interval
joint_cur_t cur = joint_iter(jd, 2500, 4500);
int found_max = 0;
while (joint_next(&min, &max, &count, &who, &cur)) {
    if (who == (unsigned)-1) found_max = 1;
}
// Result: found_max = 0 (UINT32_MAX is filtered out)

Entity ID 0 works correctly:

joint_start(jd, 1000, 0);
joint_stop(jd, 2000, 0);
// Result: Found correctly, ID=0 is valid

Workaround

Alternative Solution

Change the sentinel value in IDM to use a different value (requires modifying corm):

#define IDM_MISS 0  // Would conflict with entity ID 0 instead

This is not recommended as it would break existing corm APIs.

Related Tests

Fix (v1.2.0)

Added: Input validation to joint_start() and joint_stop()

API changes:

int joint_start(unsigned jd, time_t ts, unsigned id);
int joint_stop(unsigned jd, time_t ts, unsigned id);
// Return values:
//   0 = success
//   1 = duplicate start / no open interval
//  -1 = validation error (errno = EINVAL for UINT32_MAX entity ID)

Valid entity ID range (v1.2.0): 0 to 4,294,967,294 (0x00000000 to 0xFFFFFFFE)

Test coverage (v1.2.0):


5. File Persistence: corm CM_MIRROR Bug β†’ FIXED βœ…

Status: FIXED in v1.2.1 (removed CM_MIRROR requirement)

Original Problem (v1.1.0 - v1.2.0)

Description

File persistence using joint_init("/path/to/file.db") caused segmentation faults when closing and reopening the database. This was due to bugs in the corm library's file persistence implementation.

Root Cause

The corm library (prior to v0.7.0) required the CM_MIRROR flag for file persistence. However, corm had multiple bugs related to CM_MIRROR:

  1. Multiple databases per file failed to persist
  2. Process exit crashes with custom types and CM_MIRROR
  3. Segmentation faults when reopening file-backed databases

Source Code

Original code in src/libjoint.c:166:

uint32_t flags = fname ? CM_MIRROR : 0;  /* CM_MIRROR required for file persistence */

Impact

Evidence

Test results showing the failure:

=== Category 7: Persistence ===
Segmentation fault (core dumped)

Fix Applied (v1.2.1)

corm v0.7.0+ (commit df5a7ac) changed file persistence to work WITHOUT CM_MIRROR:

Changed src/libjoint.c:166:

uint32_t flags = 0;  /* CM_MIRROR optional in corm v0.7.0+, not needed for persistence */

Test Coverage (v1.2.1)

All 5 persistence tests now pass:


7. MV Index Write Efficiency (W3 regression) β†’ FIXED βœ…

Status: FIXED in v1.2.2 (libcorm MV duplicate chains + backshift; libjoint equality reads via corm_get_multi)

Description

Adopting corm's CM_MULTIVALUE subsidiary indexes (W3) made every libjoint write and some reads hit libcorm's lazy-sorted-index design per operation:

(Γ—9–16 inserts).

per-entry MV deletes β†’ quadratic close (hang).

(Γ—1.9 queries).

puts O(m) again after the first fix (libjoint 10k pairs: 21.4 s β†’ 14 ms).

Fix (v1.2.2)

O(N) close) + hole-eliminating backshift delete (all probes early-exit).

CM_MISS guards. ti_intersect GE scans on max unchanged.

Results

All benchmarks at or below the pre-adoption baseline; 10k start+stop pairs 2 499 442 Β΅s β†’ 14 297 Β΅s; query-all 1 833 316 β†’ 872 228 Β΅s; 1000-cycle 100 342 259 β†’ 71 072 518 Β΅s. No behavior change; all 74 tests pass. Requires libcorm with the MV chain + backshift (0.8.0).

Related Tests


Remaining Limitations (Not Fixed)

6. Zero-Duration Intervals Not Supported (BY DESIGN)

Description

Intervals where the start and stop timestamps are identical (zero duration, representing a single point in time) are not properly supported by libjoint's query mechanism.

Behavior

When an interval has start == stop:

Source Code

The intersection check in src/libjoint.c:238:

if (tmp.max >= min && tmp.min < max) {
    // Match found
}

For a query range [A, B) and an interval [T, T] where T is the zero-duration point:

The issue is that intervals use half-open ranges [min, max) where max is exclusive:

Impact

Evidence

Test case demonstrating the issue:

// Create zero-duration interval at time 5000
joint_start(jd, 5000, 50);
joint_stop(jd, 5000, 50);  // start == stop

// Query for the point
joint_cur_t cur = joint_iter(jd, 5000, 5001);  // Range includes 5000
int found = 0;
while (joint_next(&min, &max, &count, &who, &cur)) {
    found++;
}
// Result: found = 0 (zero-duration interval not returned)

Normal intervals work correctly:

joint_start(jd, 5000, 51);
joint_stop(jd, 5001, 51);  // Duration = 1
// Result: Found correctly via queries

Workaround

Use minimal non-zero durations for point events:

// Instead of:
joint_start(jd, timestamp, entity_id);
joint_stop(jd, timestamp, entity_id);  // DON'T DO THIS

// Use:
joint_start(jd, timestamp, entity_id);
joint_stop(jd, timestamp + 1, entity_id);  // Minimum duration of 1

Design Consideration

This limitation is inherent to interval tree semantics where:

To properly support point events, libjoint would need:

  1. Special handling for start == stop as point events
  2. Modified intersection logic: (tmp.max > min || (tmp.max == tmp.min && tmp.min >= min)) && tmp.min < max
  3. Additional flag or data structure to distinguish points from ranges

Related Tests


Testing Methodology

These limitations were discovered through systematic stress testing in Phase 4:

Test Coverage

Test Files

Verification Commands

# Run core tests (59 tests including validation + persistence)
cd /home/quirinpa/libjoint
make
LD_LIBRARY_PATH=./lib ./bin/test

# Run extended tests (15 tests including boundary tests)
LD_LIBRARY_PATH=./lib ./bin/test_extended

Recommendations

For Application Developers (v1.2.0)

  1. Interval Count: Can now use up to 65,000 intervals per joint_init() instance βœ…
  2. Overlapping Entities: Can now use up to 4,000 concurrent overlapping entities βœ…
  3. Timestamp Range: Use timestamps in range [LONG_MIN/2, LONG_MAX/2] (validated) βœ…
  4. Entity IDs: Cannot use UINT32_MAX (returns error with errno=EINVAL) βœ…
  5. Point Events: Use minimum duration of 1 time unit instead of zero-duration (still required)

For Library Maintainers

Completed in v1.2.0: βœ…

  1. ~~Increase TI_MASK to 0xFFFF (65,536 intervals)~~ DONE
  2. ~~Increase SPLITS_WHO_MASK to 0xFFF (4,096 entities)~~ DONE
  3. ~~Add runtime checks with error reporting when limits are exceeded~~ DONE (errno-based)
  4. ~~Add overflow checks for extreme timestamp arithmetic~~ DONE (validation added)
  5. ~~Document entity ID restrictions in API documentation~~ DONE (it.h updated)

Future Enhancements:

  1. Consider special handling for zero-duration intervals (point events)
  2. Add joint_get_limits() API to query current mask values at runtime
  3. Make masks configurable via joint_init() parameters

Compatibility Notes

v1.2.2 (current):

v1.2.1 (legacy):

v1.1.0 (legacy):


References


Version History


This document tracks design limitations across libjoint versions. See CHANGELOG.md for detailed implementation notes.