From 92cae9b5761c34c32cd74884f81501913d554ce0 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Sun, 7 Jul 2024 20:37:04 -0500 Subject: [PATCH] Reduce size of Block to 32 bytes Using a 32-bit integer to store the line number, as previously discussed. --- src/parser.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 476b8bde7..f12b92107 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -35,6 +35,7 @@ use fish_printf::sprintf; use libc::c_int; use std::cell::{Ref, RefCell, RefMut}; use std::ffi::{CStr, OsStr}; +use std::num::NonZeroU32; use std::os::fd::{AsRawFd, OwnedFd, RawFd}; use std::os::unix::prelude::OsStrExt; use std::rc::Rc; @@ -75,8 +76,8 @@ pub struct Block { /// Name of the file that created this block pub src_filename: Option>, - /// Line number where this block was created. - pub src_lineno: Option, + /// Line number where this block was created, starting from 1. + pub src_lineno: Option, } impl Block { @@ -125,7 +126,7 @@ impl Block { .to_owned(); if let Some(src_lineno) = self.src_lineno { - result.push_utfstr(&sprintf!(" (line %d)", src_lineno)); + result.push_utfstr(&sprintf!(" (line %d)", src_lineno.get())); } if let Some(src_filename) = &self.src_filename { result.push_utfstr(&sprintf!(" (file %ls)", src_filename)); @@ -845,7 +846,9 @@ impl Parser { /// Pushes a new block. Returns a pointer to the block, stored in the parser. pub fn push_block(&self, mut block: Block) -> BlockId { - block.src_lineno = self.get_lineno(); + block.src_lineno = self + .get_lineno() + .and_then(|num| NonZeroU32::new(num.try_into().unwrap_or(u32::MAX))); block.src_filename = self.current_filename(); if block.typ() != BlockType::top { let new_scope = block.typ() == BlockType::function_call { shadows: true }; @@ -1222,7 +1225,7 @@ fn append_block_description_to_stack_trace(parser: &Parser, b: &Block, trace: &m if let Some(file) = b.src_filename.as_ref() { trace.push_utfstr(&sprintf!( "\tcalled on line %d of file %ls\n", - b.src_lineno.unwrap_or(0), + b.src_lineno.map(|n| n.get()).unwrap_or(0), user_presentable_path(file, parser.vars()) )); } else if parser.libdata().within_fish_init {