From bf286a54bc2ac5eeb78c3d5c5ae66e9af24d74d4 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 11 Dec 2022 15:16:59 +0300 Subject: refactor(tvix/eval): add a LightSpan type for lighter span tracking This type carries the information required for calculating a span (i.e. the chunk and offset), instead of the span itself. The span is then only calculated in cases where it is required (when throwing errors). This reduces the eval time for `builtins.length (builtins.attrNames (import {}))` by *one third*! The data structure in chunks that carries span information reduces in-memory size by trading off the speed of retrieving span information. This is because the span information is only actually required when throwing errors (or emitting warnings). However, somewhere along the way we grew a dependency on carrying span information in thunks (for correctly reporting error chains). Hitting the code paths for span retrieval was expensive, and carrying the spans in a different way would still be less cache-efficient. This change is the best tradeoff I could come up with. Refs: b/229. Change-Id: I27d4c4b5c5f9be90ac47f2db61941e123a78a77b Reviewed-on: https://cl.tvl.fyi/c/depot/+/7558 Reviewed-by: grfn Tested-by: BuildkiteCI --- tvix/eval/src/spans.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'tvix/eval/src/spans.rs') diff --git a/tvix/eval/src/spans.rs b/tvix/eval/src/spans.rs index 9998e438b220..d2a8e9badf86 100644 --- a/tvix/eval/src/spans.rs +++ b/tvix/eval/src/spans.rs @@ -1,9 +1,46 @@ //! Utilities for dealing with span tracking in the compiler and in //! error reporting. +use crate::opcode::CodeIdx; +use crate::value::Lambda; use codemap::{File, Span}; use rnix::ast; use rowan::ast::AstNode; +use std::rc::Rc; + +/// Helper struct to carry information required for making a span, but +/// without actually performing the (expensive) span lookup. +/// +/// This is used for tracking spans across thunk boundaries, as they +/// are frequently instantiated but spans are only used in error or +/// warning cases. +#[derive(Clone, Debug)] +pub enum LightSpan { + /// The span has already been computed and can just be used right + /// away. + Actual { span: Span }, + + /// The span needs to be computed from the provided data, but only + /// when it is required. + Delayed { lambda: Rc, offset: CodeIdx }, +} + +impl LightSpan { + pub fn new_delayed(lambda: Rc, offset: CodeIdx) -> Self { + Self::Delayed { lambda, offset } + } + + pub fn new_actual(span: Span) -> Self { + Self::Actual { span } + } + + pub fn span(&self) -> Span { + match self { + LightSpan::Actual { span } => *span, + LightSpan::Delayed { lambda, offset } => lambda.chunk.get_span(*offset), + } + } +} /// Trait implemented by all types from which we can retrieve a span. pub trait ToSpan { -- cgit 1.4.1