Skip to content

Truly independent web browser

LibJS: Make source maps smaller by storing what they know

Andreas Klingccb9ca198f21
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/emitter.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/emitter.rs
index 12892c78ffe..348deab349b 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/emitter.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/emitter.rs
@@ -9,14 +9,14 @@
use crate::bytecode::executable_data::ExceptionHandler;
use crate::bytecode::operand::Operand;
use crate::bytecode::operand::OperandType;
-use crate::bytecode::source_map::SourceMapEntry;
+use crate::bytecode::source_map::SourceOffsetEntry;
use crate::layout::buffer::BlockSlice;
use crate::u32_from_usize;
/// The result of assembling bytecode.
pub struct AssembledBytecode {
pub bytecode: BlockSlice<u8>,
- pub source_map: BlockSlice<SourceMapEntry>,
+ pub source_map: BlockSlice<SourceOffsetEntry>,
pub exception_handlers: BlockSlice<ExceptionHandler>,
pub number_of_registers: u32,
/// One past the highest `Operand::argument` index referenced by any
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
index 35f2ce2b606..ebe16c51b29 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/scope.rs
@@ -2009,7 +2009,7 @@ impl Generator<'_> {
constants,
shared_function_data,
class_blueprints: link_block.slice(std::mem::take(&mut self.class_blueprints).into_iter()),
- position_source: Some(script.positions.clone()),
+ position_source: script.positions.clone(),
tables: block.finish(),
link_tables: link_block.finish(),
};
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
index cf3b95b8ece..c77e266106d 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/stream.rs
@@ -32,8 +32,8 @@ use crate::bytecode::instruction::specialize_instruction_sequence;
use crate::bytecode::instruction::typed as op;
use crate::bytecode::instruction::typed::TypedInstruction;
use crate::bytecode::operand::*;
-use crate::bytecode::source_map::SourceMapEntry;
use crate::bytecode::source_map::SourceOffset;
+use crate::bytecode::source_map::SourceOffsetEntry;
use crate::slice_block::SliceBlock;
use crate::slice_block::SliceBlockBuilder;
use crate::u32_from_usize;
@@ -3648,7 +3648,7 @@ impl Generator<'_> {
let source_entry_count = head.source_entries.len() + segment.source_entries.len();
let mut block = SliceBlock::builder(
SliceBlock::room_for::<u8>(head.bytes.len() + segment.bytes.len())
- + SliceBlock::room_for::<SourceMapEntry>(source_entry_count)
+ + SliceBlock::room_for::<SourceOffsetEntry>(source_entry_count)
+ SliceBlock::room_for::<ExceptionHandler>(exception_handlers.len())
+ other_tables_size,
);
@@ -3681,10 +3681,9 @@ impl Generator<'_> {
source_entry_count,
head_entries
.chain(entries)
- .map(|(bytecode_offset, source_offset)| SourceMapEntry {
+ .map(|(bytecode_offset, source_offset)| SourceOffsetEntry {
bytecode_offset,
- line: u32::MAX,
- column: source_offset.0,
+ source_offset,
}),
);
let assembled = AssembledBytecode {
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index 7b5e85adfed..ade22adab02 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -20,7 +20,7 @@ use crate::bytecode::encoding::{IdentifierTableIndex, PropertyKeyTableIndex, Str
use crate::bytecode::executable_data::{ConstantValue, ExceptionHandler, MAXIMUM_INTERNED_CONSTANT_LENGTH};
use crate::bytecode::executable_data::{ExecutableData, LocalVariableMetadata};
use crate::bytecode::feedback::{ExecutableFeedback, FeedbackSlotCounts};
-use crate::bytecode::source_map::SourceMapEntry;
+use crate::bytecode::source_map::{SourceMapEntry, SourceOffset, SourceOffsetEntry};
use crate::gc::class::{Finalize, GcCell, define_cell};
use crate::gc::defer_gc::DeferGc;
use crate::gc::root::MarkedVec;
@@ -1524,13 +1524,11 @@ impl Executable {
return None;
}
let entry = &source_map[entries_at_or_before_offset - 1];
- let (line, column) = match &self.code.position_source {
- Some(positions) if entry.column != crate::bytecode::source_map::SourceOffset::NONE.0 => {
- let position = positions.lines.position(entry.column);
- (position.line, position.column)
- }
- Some(_) => (0, 0),
- None => (entry.line, entry.column),
+ let (line, column) = if entry.source_offset == SourceOffset::NONE {
+ (0, 0)
+ } else {
+ let position = self.code.position_source.lines.position(entry.source_offset.0);
+ (position.line, position.column)
};
Some(SourceRange {
code: self
@@ -1544,14 +1542,16 @@ impl Executable {
/// The source around the instruction at `offset`, and where in that it is.
fn source_around(&self, offset: u32) -> Option<(Vec<u16>, usize)> {
const LONGEST_TEXT: usize = 1024;
- let positions = self.code.position_source.as_ref()?;
+ let positions = &self.code.position_source;
let source_map = self.source_map();
let entries_at_or_before_offset = source_map.partition_point(|entry| entry.bytecode_offset <= offset);
- let source_offset = source_map.get(entries_at_or_before_offset.checked_sub(1)?)?.column;
- if source_offset == crate::bytecode::source_map::SourceOffset::NONE.0 {
+ let source_offset = source_map
+ .get(entries_at_or_before_offset.checked_sub(1)?)?
+ .source_offset;
+ if source_offset == SourceOffset::NONE {
return None;
}
- let source_offset = source_offset as usize;
+ let source_offset = source_offset.0 as usize;
let view = crate::utf16::Utf16View::of_string(&positions.source);
let start = source_offset.saturating_sub(LONGEST_TEXT);
let end = (source_offset + LONGEST_TEXT).min(view.length_in_code_units());
@@ -1621,7 +1621,7 @@ impl Executable {
self.code.source_positions()
}
- pub fn source_map(&self) -> &[SourceMapEntry] {
+ pub fn source_map(&self) -> &[SourceOffsetEntry] {
&self.code.source_map
}
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable_data.rs b/Libraries/LibJS/Rust/src/bytecode/executable_data.rs
index 346f973d205..e950d4cf00f 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable_data.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable_data.rs
@@ -13,7 +13,7 @@ use ak::Utf16String;
use super::constant::AbstractOperationKind;
use super::constant::WellKnownSymbolKind;
use super::operand::PropertyKeyTableIndex;
-use super::source_map::SourceMapEntry;
+use super::source_map::SourceOffsetEntry;
use crate::ast::FunctionId;
use crate::compile::SharedFunctionDescription;
use crate::compile::utf16_string_memory_size;
@@ -181,7 +181,7 @@ pub struct ExecutableData {
/// Sorted by start offset, and not overlapping.
pub exception_handlers: BlockSlice<ExceptionHandler>,
/// Sorted by bytecode offset: where in the source code the instructions from each offset on came from.
- pub source_map: BlockSlice<SourceMapEntry>,
+ pub source_map: BlockSlice<SourceOffsetEntry>,
pub number_of_registers: u32,
pub number_of_arguments: u32,
pub is_strict: bool,
@@ -199,7 +199,7 @@ pub struct ExecutableData {
pub shared_function_data: BlockSlice<PendingSharedFunctionData>,
pub class_blueprints: BlockSlice<PendingClassBlueprint>,
/// What turns the source offsets of a source map that has them into lines and columns.
- pub position_source: Option<Arc<crate::parser::SourcePositions>>,
+ pub position_source: Arc<crate::parser::SourcePositions>,
/// The memory of everything above that running the code reads, which gets dropped before it.
pub tables: SliceBlock,
/// The memory of what only linking reads.
@@ -220,26 +220,25 @@ fn constant_memory_size(constant: &ConstantValue) -> usize {
unsafe impl crate::gc::lint_markers::HoldsNoCells for ExecutableData {}
impl ExecutableData {
- /// The entries of the source map, with the lines and columns that they stand for. Code that goes by offsets of the
- /// source leaves working those out until something asks.
+ /// The entries of the source map, with the lines and columns that they stand for, which get worked out when something
+ /// asks.
pub fn source_positions(&self) -> impl Iterator<Item = crate::bytecode::source_map::SourceMapEntry> + '_ {
use crate::bytecode::source_map::SourceMapEntry;
use crate::bytecode::source_map::SourceOffset;
- self.source_map.iter().map(|entry| match &self.position_source {
- Some(_) if entry.column == SourceOffset::NONE.0 => SourceMapEntry {
- bytecode_offset: entry.bytecode_offset,
- line: 0,
- column: 0,
- },
- Some(positions) => {
- let position = positions.lines.position(entry.column);
- SourceMapEntry {
+ self.source_map.iter().map(|entry| {
+ if entry.source_offset == SourceOffset::NONE {
+ return SourceMapEntry {
bytecode_offset: entry.bytecode_offset,
- line: position.line,
- column: position.column,
- }
+ line: 0,
+ column: 0,
+ };
+ }
+ let position = self.position_source.lines.position(entry.source_offset.0);
+ SourceMapEntry {
+ bytecode_offset: entry.bytecode_offset,
+ line: position.line,
+ column: position.column,
}
- None => *entry,
})
}
diff --git a/Libraries/LibJS/Rust/src/bytecode/source_map.rs b/Libraries/LibJS/Rust/src/bytecode/source_map.rs
index 35d46382d48..e8383c76a5b 100644
--- a/Libraries/LibJS/Rust/src/bytecode/source_map.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/source_map.rs
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-/// A source map entry mapping a bytecode offset to a source position.
+/// A bytecode offset with the line and column that the source map says it came from.
#[derive(Debug, Clone, Copy)]
pub struct SourceMapEntry {
pub bytecode_offset: u32,
@@ -12,6 +12,13 @@ pub struct SourceMapEntry {
pub column: u32,
}
+/// An entry of the source map of an executable: where in the source the instructions from a bytecode offset on came from.
+#[derive(Debug, Clone, Copy)]
+pub struct SourceOffsetEntry {
+ pub bytecode_offset: u32,
+ pub source_offset: SourceOffset,
+}
+
/// Where an instruction came from: an offset into the source, which only turns into a line and a column for what ends
/// up in the source map.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
diff --git a/Libraries/LibJS/Rust/src/compile.rs b/Libraries/LibJS/Rust/src/compile.rs
index f69d12942ab..31a337a17ce 100644
--- a/Libraries/LibJS/Rust/src/compile.rs
+++ b/Libraries/LibJS/Rust/src/compile.rs
@@ -1739,9 +1739,8 @@ mod tests {
.ok()
.expect("the source parses");
let script = Arc::downgrade(&parsed.tree.script);
- let compiled = compile_eval(parsed);
+ let _compiled = compile_eval(parsed);
assert!(script.upgrade().is_none());
- assert!(compiled.executable.position_source.is_some());
}
#[test]