T (final @NotNull IndexId<?, ?> indexId) { if (depth > MAX_LOOKUP_DEPTH) { logOrThrowMisuse(); //Even if !THROW_ON_INCORRECT_USAGE -> still do not allow to unwind traces deeper than MAX_LOOKUP_DEPTH, as // this is a risk of OoM. Instead, just overwrite current trace fields with new values (compromises stats // correctness, yes) } else if (depth >= 0 /* && depth <= MAX_LOOKUP_DEPTH */) { //Since MAX_DEPTH is limited, we could pre-allocate MAX_DEPTH trace instances in advance, and keep them // in array, thus avoiding allocation altogether. But I didn't go this route because re-entrant lookups are // considered a suspicious case: it is either misuse of index API, or a valid but unexpected use, and // optimize for either of them seems to be premature final T childTrace = this.clone(); currentTraceHolder.set(childTrace); childTrace.setupTraceBeforeStart(indexId, /* parent = */ typeSafeThis()); return childTrace; } // if (depth < 0) setupTraceBeforeStart(indexId, /* parent = */ null); return typeSafeThis(); }