PistonDevelopers / meta

A DSL parsing library for human readable text documents
MIT License
89 stars 5 forks source link

Idea for optimization #316

Open bvssvni opened 7 years ago

bvssvni commented 7 years ago

Currently, when a rule matches on a string, it allocates memory. In many formats the parent rule might fail and drop the allocated string.

An idea to optimize this is to push conversion operations instead of meta data, and then convert to meta data at the end of parsing.

bvssvni commented 7 years ago

Tried the following structure:

/// Represents meta data.
#[derive(PartialEq, Clone, Debug)]
pub enum FastMetaData {
    /// Starts node.
    StartNode(Arc<String>),
    /// Ends node.
    EndNode(Arc<String>),
    /// Sets bool property.
    Bool(Arc<String>, bool),
    /// Sets f64 property.
    F64(Arc<String>, NumberSettings, DebugId),
    /// Sets string property.
    String(Arc<String>, StringFormat, DebugId),
}

There is no point in using slices, because meta data are wrapped in Range<MetaData>. Arc<String> is cloned from meta rules.

This design seems to be a bit worse than the current one:

Before:
test tests::bench_deep_string ... bench:   3,667,948 ns/iter (+/- 1,281,565)
test tests::bench_hello       ... bench:      54,625 ns/iter (+/- 25,679)

After:
test tests::bench_deep_string ... bench:   3,735,350 ns/iter (+/- 478,368)
test tests::bench_hello       ... bench:      58,666 ns/iter (+/- 13,847)