Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Heapsize calculations #31

Merged
merged 9 commits into from
Aug 20, 2018
31 changes: 28 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
language: rust
cache: cargo

rust:
- stable
- beta
- nightly
- stable
- beta
- nightly
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could we use consistent indentation here (sorry 😅 )


env:
- CARGO_FEATURES=""
REPORTING_CARGO_FEATURES=""
- CARGO_FEATURES="memory_usage"
REPORTING_CARGO_FEATURES="memory_usage"
- CARGO_FEATURES="serialization"
REPORTING_CARGO_FEATURES=""

matrix:
allow_failures:
- rust: nightly
fast_finish: true

script:
- set -ex
# We manually build/test each crate and pass in any necessary feature flags
# to work around rust-lang/cargo#4942

# codespan
- cargo build --manifest-path=codespan/Cargo.toml --verbose --no-default-features --features="$CARGO_FEATURES"
- cargo test --manifest-path=codespan/Cargo.toml --verbose --no-default-features --features="$CARGO_FEATURES"
# codespan-lsp
- cargo build --manifest-path=codespan-lsp/Cargo.toml --verbose
- cargo test --manifest-path=codespan-lsp/Cargo.toml --verbose
# codespan-reporting
- cargo build --manifest-path=codespan-reporting/Cargo.toml --verbose --no-default-features --features="$REPORTING_CARGO_FEATURES"
- cargo test --manifest-path=codespan-reporting/Cargo.toml --verbose --no-default-features --features="$REPORTING_CARGO_FEATURES"
5 changes: 5 additions & 0 deletions codespan-reporting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ exclude = ["assets/**"]
[dependencies]
codespan = { path = "../codespan", version = "0.1.1" }
termcolor = "0.3.4"
heapsize = { version = "0.4", optional = true }
heapsize_derive = { version = "0.1", optional = true }

[dev-dependencies]
structopt = "0.2.7"

[features]
memory_usage = ["heapsize_derive", "heapsize", "codespan/memory_usage"]
3 changes: 3 additions & 0 deletions codespan-reporting/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use Severity;

/// A style for the label
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub enum LabelStyle {
/// The main focus of the diagnostic
Primary,
Expand All @@ -15,6 +16,7 @@ pub enum LabelStyle {

/// A label describing an underlined region of code associated with a diagnostic
#[derive(Clone, Debug)]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct Label {
/// The span we are going to include in the final snippet.
pub span: ByteSpan,
Expand Down Expand Up @@ -49,6 +51,7 @@ impl Label {

/// Represents a diagnostic message and associated child messages.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct Diagnostic {
/// The overall severity of the diagnostic
pub severity: Severity,
Expand Down
6 changes: 6 additions & 0 deletions codespan-reporting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ use std::cmp::Ordering;
use std::fmt;
use std::str::FromStr;
use termcolor::{Color, ColorChoice};
#[cfg(feature = "memory_usage")]
extern crate heapsize;
#[cfg(feature = "memory_usage")]
#[macro_use]
extern crate heapsize_derive;

mod diagnostic;
mod emitter;
Expand All @@ -25,6 +30,7 @@ pub use self::emitter::emit;
/// assert!(Severity::Note > Severity::Help);
/// ```
#[derive(Copy, Clone, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub enum Severity {
/// An unexpected bug.
Bug,
Expand Down
4 changes: 3 additions & 1 deletion codespan/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ failure = "0.1.1"
serde_derive = { version = "1", optional = true }
serde = { version = "1", optional = true }
itertools = "0.7"
heapsize = { version = "0.4", optional = true }
heapsize_derive = { version = "0.1", optional = true }

[dev-dependencies]
pretty_assertions = "0.5.0"


[features]
serialization = ["serde", "serde/rc", "serde_derive"]
memory_usage = ["heapsize_derive", "heapsize"]
1 change: 1 addition & 0 deletions codespan/src/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use index::{ByteIndex, ByteOffset, RawIndex};

#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct CodeMap {
files: Vec<Arc<FileMap>>,
}
Expand Down
25 changes: 25 additions & 0 deletions codespan/src/filemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::{fmt, io};

#[cfg(feature = "memory_usage")]
use heapsize::{self, HeapSizeOf};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be cfg-ed out with a feature flag!

use index::{ByteIndex, ByteOffset, ColumnIndex, LineIndex, LineOffset, RawIndex, RawOffset};
use span::ByteSpan;

Expand Down Expand Up @@ -59,6 +61,28 @@ impl fmt::Display for FileName {
}
}

#[cfg(feature = "memory_usage")]
impl HeapSizeOf for FileName {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too!

fn heap_size_of_children(&self) -> usize {
match *self {
FileName::Virtual(ref s) => s.heap_size_of_children(),
FileName::Real(ref path) => {
// Reliably finding the amount of memory used by a `PathBuf` is
// annoying due to its os-specific nature. We use an
// approximation by converting to a string and getting the
// raw heap size for the string's buffer, falling back to 0
// otherwise. Ideally this should be in the `heapsize` crate.
//
// This *should* be safe because a `PathBuf` will allocate its
// buffer using jemalloc.
path.to_str()
.map(|s| unsafe { heapsize::heap_size_of(s.as_ptr()) })
.unwrap_or(0)
}
}
}
}

#[derive(Debug, Fail, PartialEq)]
pub enum LineIndexError {
#[fail(display = "Line out of bounds - given: {:?}, max: {:?}", given, max)]
Expand Down Expand Up @@ -92,6 +116,7 @@ pub enum SpanError {

#[derive(Debug)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
/// Some source code
pub struct FileMap<S = String> {
/// The name of the file that the source came from
Expand Down
8 changes: 8 additions & 0 deletions codespan/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub type RawOffset = i64;
/// A zero-indexed line offset into a source file
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct LineIndex(pub RawIndex);

impl LineIndex {
Expand Down Expand Up @@ -51,6 +52,7 @@ impl fmt::Debug for LineIndex {
/// A 1-indexed line number. Useful for pretty printing source locations.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct LineNumber(RawIndex);

impl fmt::Debug for LineNumber {
Expand All @@ -70,6 +72,7 @@ impl fmt::Display for LineNumber {
/// A line offset in a source file
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct LineOffset(pub RawOffset);

impl Default for LineOffset {
Expand All @@ -95,6 +98,7 @@ impl fmt::Display for LineOffset {
/// A zero-indexed column offset into a source file
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct ColumnIndex(pub RawIndex);

impl ColumnIndex {
Expand Down Expand Up @@ -133,6 +137,7 @@ impl fmt::Debug for ColumnIndex {
/// A 1-indexed column number. Useful for pretty printing source locations.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct ColumnNumber(RawIndex);

impl fmt::Debug for ColumnNumber {
Expand All @@ -152,6 +157,7 @@ impl fmt::Display for ColumnNumber {
/// A column offset in a source file
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct ColumnOffset(pub RawOffset);

impl Default for ColumnOffset {
Expand All @@ -177,6 +183,7 @@ impl fmt::Display for ColumnOffset {
/// A byte position in a source file
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct ByteIndex(pub RawIndex);

impl ByteIndex {
Expand Down Expand Up @@ -214,6 +221,7 @@ impl fmt::Display for ByteIndex {
/// A byte offset in a source file
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct ByteOffset(pub RawOffset);

impl ByteOffset {
Expand Down
8 changes: 8 additions & 0 deletions codespan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
//!
//! - **serialization** - Adds `Serialize` and `Deserialize` implementations
//! for use with `serde`
//! - **memory_usage** - Adds `HeapSizeOf` implementations for use with the
//! `heapsize` crate

#[macro_use]
extern crate failure;
Expand All @@ -22,6 +24,12 @@ extern crate serde;
#[macro_use]
extern crate serde_derive;

#[cfg(feature = "memory_usage")]
extern crate heapsize;
#[cfg(feature = "memory_usage")]
#[macro_use]
extern crate heapsize_derive;

mod codemap;
mod filemap;
mod index;
Expand Down
1 change: 1 addition & 0 deletions codespan/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use index::{ByteIndex, Index};
/// A region of code in a source file
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Ord, PartialOrd)]
#[cfg_attr(feature = "serialization", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "memory_usage", derive(HeapSizeOf))]
pub struct Span<I> {
start: I,
end: I,
Expand Down