Skip to content

Arena; avoid use of temporary.#10940

Merged
SolidWallOfCode merged 1 commit intoapache:masterfrom
SolidWallOfCode:arena-cleanup-1
Dec 18, 2023
Merged

Arena; avoid use of temporary.#10940
SolidWallOfCode merged 1 commit intoapache:masterfrom
SolidWallOfCode:arena-cleanup-1

Conversation

@SolidWallOfCode
Copy link
Member

I can't see any need for the temporary, when the reference can be used (since it is used only for the debug message). Further, url_string_get prints the URL into the heap also, so either it's already there and it's faster to use it, or it will be generated.

@SolidWallOfCode SolidWallOfCode added this to the 10.0.0 milestone Dec 17, 2023
@SolidWallOfCode SolidWallOfCode self-assigned this Dec 17, 2023
@SolidWallOfCode
Copy link
Member Author

Still parsing the code, I thought this wasn't doing a heap allocation so I closed the PR, but in fact it does.

url_string_get takes a HdrHeap* which, if nullptr, avoids a heap allocation. However, HttpHdr::url_string_get always passes in HttpHdr::m_heap therefore this call always results in the URL being on the heap.

if (info->active) {
if (info->active->last_failure.load() == TS_TIME_ZERO) {
char *url_str = t_state.hdr_info.client_request.url_string_get(&t_state.arena, nullptr);
char *url_str = t_state.hdr_info.client_request.url_string_get_ref(nullptr);
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this reserve space for a copy of the URL every time it is called? Can we be sure it will only be called once?

Should there be a swoc::AbstractMemArena, that MemArena inherits from, and url_string_get() takes a reference to? Then there could be a swoc::LocalMemArena that inherits from swoc::AbstractMemArena, that would have an internal array, small enough so that LocalMemArena could reasonably be a stack variable? That seems like it might be the ticket for this use case.

Copy link
Member Author

Choose a reason for hiding this comment

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

The copy on the heap is a cache and it's not duplicated if already present. If it's already there, url_string_get_ref will return that existing copy. Therefore in the worst case it's the same, and the best case it's very fast because the URL doesn't have to be printed. The original code used Arena as temporary space, but as noted that doesn't avoid the copy to the heap.

This isn't really done as I'd like, but changing things to do the better style is a very heavy lift (e.g., this should be returned TextView not char const *).

MemArena already supports the use of static blocks for memory allocation. Speaking of which, that would be good in another PR I'm working on for which this is a precursor.

Copy link
Contributor

@ywkaras ywkaras left a comment

Choose a reason for hiding this comment

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

No net increase in the scariness of this logic.

@SolidWallOfCode SolidWallOfCode merged commit 5f1d865 into apache:master Dec 18, 2023
phongn pushed a commit to phongn/trafficserver that referenced this pull request Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants