Skip to content

Conversation

@678098
Copy link
Collaborator

@678098 678098 commented Nov 19, 2025

We cannot not directly cast/assign a pointer even for simple types because alignment in a Blob is not guaranteed. Have to call memcpy with hopes that it will be optimized
TODO: check asm outputs

@678098 678098 requested a review from a team as a code owner November 19, 2025 22:27
@678098 678098 changed the title Perf[BMQ]: fast memcpy properties from continuous buffers Perf[BMQ]: fast memcpy properties from continuous buffers Nov 19, 2025
@678098 678098 force-pushed the 251119_opt_MessageProperties branch from f6862dd to 953d214 Compare November 19, 2025 22:42
// Rare path
bmqu::BlobUtil::readNBytes(buf, blob, d_start, numBytes);
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

int BlobUtil::readUpToNBytes(char*               buf,
                             const bdlbb::Blob&  blob,
                             const BlobPosition& start,
                             int                 length)
{
    if (BSLS_PERFORMANCEHINT_PREDICT_UNLIKELY(!isValidPos(blob, start))) {
        BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
        return -1;  // RETURN
    }

    if (BSLS_PERFORMANCEHINT_PREDICT_UNLIKELY(length == 0)) {
        BSLS_PERFORMANCEHINT_UNLIKELY_HINT;
        return 0;  // RETURN
    }

    char*              outPosition = buf;
    const int          numBuffers  = blob.numDataBuffers();
    bmqu::BlobPosition cursor(start);

    while (length != 0 && (cursor.buffer() != numBuffers)) {
        const bdlbb::BlobBuffer& buffer   = blob.buffer(cursor.buffer());
        const int                buffSize = bufferSize(blob, cursor.buffer());

        const int toCopy = bsl::min(buffSize - cursor.byte(), length);
        bsl::memcpy(outPosition, buffer.data() + cursor.byte(), toCopy);

        outPosition += toCopy;
        length -= toCopy;

        cursor.setBuffer(cursor.buffer() + 1);
        cursor.setByte(0);
    }

    return static_cast<int>(outPosition - buf);
}

Short memcpy path should skip all these other operations

@678098 678098 changed the title Perf[BMQ]: fast memcpy properties from continuous buffers [WIP]Perf[BMQ]: fast memcpy properties from continuous buffers Nov 21, 2025
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