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

Added Link type #230

Merged
merged 6 commits into from May 27, 2016

Conversation

@bvssvni
Copy link
Member

commented May 26, 2016

Closes #227

This PR adds a link type to Dyon. It stores bool/f64/str with only 3.2% overhead in memory compared to same-type-lists (when compactly filled), which is only 0.1% above theoretical minimum (2 bit) for a dynamical typed list like this. The link type is 1.5x faster at joining single items than arrays, and uses much less memory than arrays (3.096x).

overhead_link := 1+4/124 // uses 4 u64 to stores types of 124 items per block
bytes_per_variable := 24 // 8 for type + 16 bytes for data
bytes_per_link_item := 8
memory_saved := bytes_per_variable / bytes_per_link_item * overhead_link
println(memory_saved) // prints `3.096774193548387`

How to use a link object

A link {} bracket simply links together types of bool/f64/str in a link object. White-space is optional, since the syntax can tell the difference between a string and a variable.

fn main() {
    title := "My own home page!"
    paragraph := "I love coding in Dyon!"
    a := link {
        "<html>\n"
        "<body>\n"
        "<h1>"title"</h1>\n"
        "<p>"paragraph"</p>"
        "</body>\n"
        "</html>\n"
    }
    _ := unwrap(save(string: to_string(a), file: "/Users/sven/test.html"))
}

You can compute stuff inside the bracket:

a := link { "my answer is: " 7 + 4 }

You can link single items with +=:

a := link {}
a += "hi!"

Link together links with += (puts it at the end) or -= (puts it at the front):

fn main() {
    a := link { 1 2 3 }
    b := link { 4 5 6 }
    a -= b
    println(a) // prints `456123`
}

You can't use + because it wastes memory and is too easy to do. For more information, see #227. Instead, put it inside the link bracket:

foo() = link { 2 }

a := link { 1 foo() }
println(a) // prints `12`

Motivation

The Dyon language has been very focused on vectors and mathematics up to now, but I would like a language that is also good at processing data, and good at generating and parse text, such as its own source. In Piston-Meta meta parsing there are only bool/f64/str, so an efficient structure to store these is perfect.

The original idea evolved around an improvement of all/any loops, but I decided to not go in direction with links because these are very small amount of data, and links seems perfect for doing large amounts of data. I want it to be really good at one thing: Link together data.

There are lots of applications where you link together data:

  • Web servers
  • Code generation
  • Templates

However, the problem with generating text is that the process removes the information that makes the data useful, so you have to parse it again. By preserving this information one can reuse the data while keeping it close to the form of the final output.

Dyon uses the go keyword to turn a function call into a new thread, and links are very nice here since they can be passed between threads and efficiently linked in any order. When joining [thr[link]], you have to use pop(mut) it to create a unique reference, or else Dyon will show an error. This reverses the order of the results. The -= operator puts the links together in the right order.

bvssvni added some commits May 26, 2016

Basic support for `link {}`
- Added “link” module
- Added `Type::Link`
- Added `Runtime::link`
Push `bool`, `f64`, `str` to link with `+=`
- Added “bench/push_link.dyon”
- Added “bench/push_str.dyon”
- Added “bench/push_array.dyon”
Added `link`+`go` bench
- Added “bench/push_link_go.dyon”
- Added “save_string_file” intrinsic
- Added `-=` for links
@bvssvni

This comment has been minimized.

Copy link
Member Author

commented May 26, 2016

Screenshot:

dyon html environment

@bvssvni

This comment has been minimized.

Copy link
Member Author

commented May 27, 2016

Tested. It's awesome for generating web pages.

Merging.

@bvssvni bvssvni merged commit 3cdb449 into PistonDevelopers:master May 27, 2016

@bvssvni bvssvni deleted the bvssvni:link branch May 27, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.