Artifactspublic-v0

curl

master
ply4 git calls23 ms288 KiB3 cachedprotocol v2
108 lines3.4 KiBRaw

splay

#include "splay.h"

This is an internal module for splay tree management. A splay tree is a binary search tree with the additional property that recently accessed elements are quick to access again. A self-balancing tree.

Nodes are added to the tree, they are accessed and removed from the tree and it automatically rebalances itself in each operation.

libcurl use

libcurl adds fixed timeout expiry timediff_t to the splay tree, and is meant to scale up to holding a huge amount of pending timeouts with decent performance.

The splay tree is used to:

  1. figure out the next timeout expiry value closest in time
  2. iterate over timeouts that already have expired

This splay tree rebalances itself based on the timeout value.

Each node in the splay tree carries a uint32_t id. This is set to the mid, the unique transfer identifier in a multi handle. Each transfer is added only once in the tree. To still allow each transfer to have a large number of timeouts per handle, each handle has a sorted list of pending timeouts. Only the handle's timeout that is closest to expire is the timestamp used for the splay tree node.

When a specific easy handle's timeout expires, the node gets removed from the splay tree and from the handle's list of timeouts. The next timeout for that handle is then first in line and becomes the new timeout value as the node is re-added to the splay.

To convert the timediff_t is the splay to the correct timestamps, the multi handle carries a "base timestamp", set once when created, and the timeout values are calculated relative to that. This works for about 500,000 years of continued operation of a multi handle.

splay

c
struct Curl_tree *splay(timediff_t key, struct Curl_tree *t);

Rearranges the tree t after the provide time key.

splayinsert

c
struct Curl_tree *splayinsert(timediff_t key,
                              struct Curl_tree *t,
                              struct Curl_tree *node,
                              uint32_t id);

This function inserts a new node in the tree, using the given key timeout. The node struct has a field called ->id which is set to the passed id. libcurl sets this to the transfer's mid, the unique identifier in a multi handle.

The splay insert function does not allocate any memory, it assumes the caller has that arranged.

It returns a pointer to the new tree root.

splaygetbest

c
struct Curl_tree *splaygetbest(timediff_t key,
                               struct Curl_tree *tree,
                               struct Curl_tree **removed);

If there is a node in the tree that has a timeout that is less than the provided key, this function removes that node from the tree and provides it in the *removed pointer (or NULL if there was no match).

It returns a pointer to the new tree root.

splayremove

c
int splayremove(struct Curl_tree *tree,
                struct Curl_tree *node,
                struct Curl_tree **newroot);

Removes a given node from a splay tree, and returns the newroot identifying the new tree root.

Note that a clean tree without any nodes present implies a NULL pointer.

Curl_splayget

c
uint32_t Curl_splayget(struct Curl_tree *node);

Get the id from the splay node that was previously set.

1<!--
2Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
4SPDX-License-Identifier: curl
5-->
7# `splay`
9 #include "splay.h"
11This is an internal module for splay tree management. A splay tree is a binary
12search tree with the additional property that recently accessed elements are
13quick to access again. A self-balancing tree.
15Nodes are added to the tree, they are accessed and removed from the tree and
16it automatically rebalances itself in each operation.
18## libcurl use
20libcurl adds fixed timeout expiry `timediff_t` to the splay tree, and is meant
21to scale up to holding a huge amount of pending timeouts with decent
22performance.
24The splay tree is used to:
261. figure out the next timeout expiry value closest in time
272. iterate over timeouts that already have expired
29This splay tree rebalances itself based on the timeout value.
31Each node in the splay tree carries a `uint32_t id`. This is set to the
32`mid`, the unique transfer identifier in a multi handle. Each transfer
33is added only once in the tree. To still allow each transfer
34to have a large number of timeouts per handle, each handle has a sorted list
35of pending timeouts. Only the handle's timeout that is closest to expire
36is the timestamp used for the splay tree node.
38When a specific easy handle's timeout expires, the node gets removed from the
39splay tree and from the handle's list of timeouts. The next timeout for
40that handle is then first in line and becomes the new timeout value as the
41node is re-added to the splay.
43To convert the `timediff_t` is the splay to the correct timestamps, the
44multi handle carries a "base timestamp", set once when created, and the
45timeout values are calculated relative to that. This works for about
46500,000 years of continued operation of a multi handle.
48## `splay`
50~~~c
51struct Curl_tree *splay(timediff_t key, struct Curl_tree *t);
52~~~
54Rearranges the tree `t` after the provide time `key`.
56## `splayinsert`
58~~~c
59struct Curl_tree *splayinsert(timediff_t key,
60 struct Curl_tree *t,
61 struct Curl_tree *node,
62 uint32_t id);
63~~~
65This function inserts a new `node` in the tree, using the given `key`
66timeout. The `node` struct has a field called `->id` which is set to
67the passed `id`. libcurl sets this to the transfer's `mid`, the unique
68identifier in a multi handle.
70The splay insert function does not allocate any memory, it assumes the caller
71has that arranged.
73It returns a pointer to the new tree root.
75## `splaygetbest`
77~~~c
78struct Curl_tree *splaygetbest(timediff_t key,
79 struct Curl_tree *tree,
80 struct Curl_tree **removed);
81~~~
83If there is a node in the `tree` that has a timeout that is less than the
84provided `key`, this function removes that node from the tree and provides it
85in the `*removed` pointer (or NULL if there was no match).
87It returns a pointer to the new tree root.
89## `splayremove`
91~~~c
92int splayremove(struct Curl_tree *tree,
93 struct Curl_tree *node,
94 struct Curl_tree **newroot);
95~~~
97Removes a given `node` from a splay `tree`, and returns the `newroot`
98identifying the new tree root.
100Note that a clean tree without any nodes present implies a NULL pointer.
102## `Curl_splayget`
104~~~c
105uint32_t Curl_splayget(struct Curl_tree *node);
106~~~
108Get the `id` from the splay node that was previously set.
Diagnostics4 git calls · 23 ms
CallStatusSizeTime023 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=18
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch2001.4 KiB · hit18 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=1443
git.response.cache=HIT
git.response.duration_ms=18
git.fetch.wants=cb75374874260530da11a2ab66b3502cb6c5497c
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1443
git.pkt.payload.max=1422
git.sideband.band1.bytes=1421
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e2df182ca72308
Request
8eaa8889…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format