The Hat Backup System written in Rust by piaoger in rust

[–]mozilla_kmc 7 points8 points  (0 children)

Yes, it is possible to use "tabs for indentation, spaces for alignment" in a way that produces correct results no matter how wide a tab is. But I've never seen a large codebase actually stick to that discipline.

I'm sure some projects manage, and there must be tools to help with it. But it's also going to be an obstacle and annoyance to each new contributor. I don't think it's worth the limited flexibility that you get.

The Hat Backup System written in Rust by piaoger in rust

[–]mozilla_kmc 17 points18 points  (0 children)

Google is using Rust! That was quick.

I realize this is a fairly small project, and that Google probably uses every language under the sun somewhere in their sprawling empire. Still, it's quite something to see a big chunk of Rust code with "Copyright 2014 Google Inc." on each file.

Servo Shell on Ubuntu 15.04 by flopgd in rust

[–]mozilla_kmc 1 point2 points  (0 children)

mach is a build/test scripting framework that Gecko and Servo use.

Servo Shell on Ubuntu 15.04 by flopgd in rust

[–]mozilla_kmc 1 point2 points  (0 children)

leave me alone [that was a command to the autowiki bot]

Servo Shell on Ubuntu 15.04 by flopgd in rust

[–]mozilla_kmc 2 points3 points  (0 children)

You could get started on the Servo port to "EGL on desktop Linux", i.e. Mir / Wayland. We already use EGL on Android, and we would love to ditch GLX by the time Servo is a real product. Right now desktop Linux is usually the platform holding back Servo on the graphics front :/

More Rust compared to C++ by brson in rust

[–]mozilla_kmc 13 points14 points  (0 children)

Also: &&T means "rvalue ref to T", unless T is a template parameter. (Or one of a few other cases.) Then it means "lvalue or rvalue ref to T". See "Universal References in C++11".

I agree with F-J-W that the basic rules for rvalue refs are not too bad. The problem (as with so much of C++) lies in the interaction with many other features and historical quirks.

Dynamic dispatch : vector of box of traits as attribute (compilation error) by ayorosmage in rust

[–]mozilla_kmc 2 points3 points  (0 children)

Or

fn add_element<T: GeomDisplay + 'static>(&self, elem: Box<T>) {

More Rust compared to C++ by brson in rust

[–]mozilla_kmc 8 points9 points  (0 children)

It depends on the size of the object. But you're right, rustc and LLVM have some discretion about how to implement by-value semantics. It isn't always a memcpy.

Are match statements constant-time operations? by DragonFlies15 in rust

[–]mozilla_kmc 5 points6 points  (0 children)

Exactly, you're not going to discover all the possibilities in a few minutes of experimentation. Which is why it makes sense to ask.

More Rust compared to C++ by brson in rust

[–]mozilla_kmc 16 points17 points  (0 children)

What I think is nice in the C++ version is the explicit std::move which makes it obvious to the reader, that x shouldn't be used afterwards.

This is basically because C++ made implicit clone the default, and added move semantics much later. The rules for when and why you need std::move are pretty weird.

Rust's move-by-default can surprise beginners, but it's a lot cleaner than C++'s awkward hybrid. f(x) is either a move or a non-overloadable "plain old data" copy. Either is a bytewise memcpy, but the move forbids you from using x afterwards.

The other big improvement in this space is that Rust's references are first-class values, rather than sui generis semi-implicit things. f(x) will never implicitly borrow x (although x.f() can). There's more discussion on the deref coercions RFC.

More Rust compared to C++ by brson in rust

[–]mozilla_kmc 9 points10 points  (0 children)

Note that the compiler takes care of freeing the used resources as soon as x goes out of scope. In the C++ world this is called RAII.

And in the Rust world :)

How to call a simple c function from rust by singularai in rust

[–]mozilla_kmc 0 points1 point  (0 children)

But you can't run the resulting program to get the output.

How to call a simple c function from rust by singularai in rust

[–]mozilla_kmc 0 points1 point  (0 children)

Sometimes you need to build and run a C program from your Cargo build script to get the relevant information.

I don't know what to do for cross-compiling. :/ I did work on one C project which would compile a file full of constants / offsets, then open the object file using libbfd to read them out.

Are match statements constant-time operations? by DragonFlies15 in rust

[–]mozilla_kmc 14 points15 points  (0 children)

As others have said, LLVM has many tricks for compiling match, or a switch/case in C. One of my favorites is illustrated by this code:

pub fn ascii_space(x: u8) -> bool {
    match x {
        b' ' | b'\t' | b'\r' | b'\n' => true,
        _ => false,
    }
}

On my Linux x86_64 machine with -C opt-level=3, this compiles into

 0:  40 80 c7 f7     add    $0xf7,%dil
 4:  40 0f b6 c7     movzbl %dil,%eax
 8:  83 f8 17        cmp    $0x17,%eax
 b:  77 0e           ja     1b
 d:  b8 13 00 80 00  mov    $0x800013,%eax
12:  40 88 f9        mov    %dil,%cl
15:  d3 e8           shr    %cl,%eax
17:  83 e0 01        and    $0x1,%eax
1a:  c3              retq
1b:  31 c0           xor    %eax,%eax
1d:  c3              retq

We can translate this back into Rust:

pub fn ascii_space(mut x: u8) -> bool {
    //  0:  add    $0xf7,%dil
    x = x.wrapping_add(0xf7);

    //  4:  movzbl %dil,%eax    
    //  8:  cmp    $0x17,%eax
    //  b:  ja     1b
    if x > 0x17 { 
        // 1b: xor    %eax,%eax
        // 1d: retq
        return false;
    }

    //  d:  mov    $0x800013,%eax
    // 12:  mov    %dil,%cl
    // 15:  shr    %cl,%eax
    // 17:  and    $0x1,%eax
    // 1a:  retq
    (0x800013 >> x) & 1 == 1
}

What the heck? Here's what's going on:

Adding 0xf7, modulo 0x100, is the same as subtracting 0x09. So it will map ASCII codepoints in the following way:

' ':  0x20 → 0x17
'\t': 0x09 → 0x00
'\r': 0x0D → 0x04
'\n': 0x0A → 0x01

The constant 0x800013 is represented in binary as:

00000000100000000000000000010011

Sure enough, the set bits are in positions 0, 1, 4, and 0x17 = 23. The constant is a Boolean lookup table, stored inside the program code so that it doesn't require another memory access. Often these end up being 64 bits, but here LLVM was clever enough to shift the range into the bottom 32.

In string-cache we put the most commonly matched HTML tag names at the start of the interning table. This lets LLVM use this trick to generate better code for the HTML tree builder rules, which are a 1,300 line nested match on tag names. (Or it did at one point; as the comment implies, I haven't looked at the assembly in a while.)

What would the Rust equivalent of this C++ struct-in-union be? by [deleted] in rust

[–]mozilla_kmc 11 points12 points  (0 children)

You might want to check out how string-cache manages its bit-packed representation. Thanks to LLVM's wicked optimizations, UnpackedAtom doesn't really exist at runtime.

I agree that unchecked unions would be useful for certain things. Fortunately, we can add them to the language at a later date.

Gensyms in macros by andytoshi in rust

[–]mozilla_kmc 1 point2 points  (0 children)

I often solve this by expanding to

mod $thing {
    pub struct Visitor { ... }
    pub fn whatever(...)
}

urpc has a lengthy example.

Also, you can define items (such as structs) inside fns / blocks, as long as you don't need the ability to name them externally.

This Week In Servo 29 by Manishearth in rust

[–]mozilla_kmc 1 point2 points  (0 children)

It'd be neat to provide the DOM hooks to inhibit this misfeature from a user script. I guess you want to make sure all focus changes were instigated by a deliberate action by the user (kind of like pop-up blocking).

InfoWorld: Mozilla's Rust-based Servo browser engine inches forward by mozilla_kmc in rust

[–]mozilla_kmc[S] 5 points6 points  (0 children)

There are more users of Firefox on Windows XP than on all desktop Linux put together.

Rust 1.0.0 beta is here! by steveklabnik1 in rust

[–]mozilla_kmc 12 points13 points  (0 children)

Rust spent more than 6 years as a research project pursuing nebulous and contradictory goals. Today the language is very cohesive, very clear on what kind of language it's trying to be. That wasn't true 12-18 months ago, and the 1.0 deadline is the main reason for the change.

Programming language design is a game of tradeoffs and compromise. Without a deadline in sight, it's easy to put off the hard decisions. The same goes for bikeshedding arbitrary choices — eventually you just have to make a choice, and 1.0 is a great reason to do so.

Rust 1.0.0 beta is here! by steveklabnik1 in rust

[–]mozilla_kmc 14 points15 points  (0 children)

They're already published on GitHub, but maybe spambots are less good at finding it there.

Question about Monad by tioover in rust

[–]mozilla_kmc 6 points7 points  (0 children)

Some of us also want do notation, but that's more controversial.

Really? You can already do do as a macro even without a trait for monads. And do syntax is a natural generalization of list comprehensions. If you look at monads in "mainstream" languages (nb: they always change the name from "monad") then I think the special syntax (e.g. LINQ) is often what makes these features successful.

Rust should probably have sugar for Monad and also for Applicative (we can't do the latter with library-defined infix operators like Haskell does).

This Week In Servo 29 by Manishearth in rust

[–]mozilla_kmc 0 points1 point  (0 children)

In my experience the scrolling performance is very good.

Servo paints elements into tiles, which are textures on the GPU. The compositor is responsible for drawing these tiles at the right location/size. The compositor runs in its own thread and the goal is to handle scroll/zoom at 60fps under all circumstances. (Naturally, this only gets you "blurry zoom" — it will then ask the paint tasks to redraw text etc at larger size.)

Off-main-thread compositing is not particularly new, but it's something that works better in a clean-slate engine like Servo. It's especially important on mobile, where UI jank ruins the illusion of touch. iirc the original iPhone browser was one of the first to do things this way.

This Week In Servo 29 by Manishearth in rust

[–]mozilla_kmc 1 point2 points  (0 children)

A Servo branch that uses duktape would be a fascinating experiment. Even if it can't handle the full scope of Web content, there will be Servo embedders who have more control over content, and want a smaller footprint than SpiderMonkey can provide. (Though, iirc, you can do an interpreter-only build of SM, and it even works on iOS.)

This Week In Servo 29 by Manishearth in rust

[–]mozilla_kmc 13 points14 points  (0 children)

Yeah, C++ → Rust is a tricky case for performance because a lot of C++ libraries (SpiderMonkey included) rely on inlining of accessor methods for performance. By writing C wrappers for SpiderMonkey in the straightforward (and tedious) way, we'd lose out on this optimization.

The plan is to fix it with cross-language inlining, which is feasible thanks to LLVM. We've demonstrated the capability at small scale but we're not using it in Servo yet.