Coder Social home page Coder Social logo

Comments (3)

Rahix avatar Rahix commented on July 30, 2024

The problem is that core::fmt uses a lot of memory for its formatting. For any non-trivial cases, it usually surpasses the capabilities of an AVR chip so essentially you just cannot use it at all. That's why all the examples make use of the ufmt crate instead.

However, there is one place where core::fmt sneaks in and cannot be replaced so easily: panic message formatting. The only real solution at the moment is to make sure that all panics do not provide a formatted message and instead simply panic with a static message / no message. For example, you need to replace all Result::unwrap() calls with something like

trait ResultExt {
    type T;
    fn unwrap_avr(self) -> Self::T;
}

impl<T, E> UnwrapExt for Result<T, E> {
    type T = T;

    fn unwrap_avr(self) -> Self::T {
        match self {
            Ok(t) => t,
            Err(_) => panic!(),
        }
    }
}

(Or alternatively follow your suggestion from #463 but that means you don't get any custom panic behavior at all)

from avr-hal.

djdisodo avatar djdisodo commented on July 30, 2024

@Rahix i thought it might be caused by insufficient sram but i didn't thought further because compiling with release profile fixed it
(maybe core::fmt heavily rely on compiler optimizations to reduce ram usage)

about immediate abort, iirc just avoiding print of error message didn't strip out all of the error messages

from avr-hal.

Rahix avatar Rahix commented on July 30, 2024

Relying on optimizations with core::fmt is very hit or miss. Compiling with LTO manages to eliminate a lot of overhead a surprising number of times, but it does not always work. And when it doesn't, there is little you can do, unfortunately.

iirc just avoiding print of error message didn't strip out all of the error messages

The things is that you need to ensure all panicking code paths do not lead to a panic with a message. This is not an easy feat of course. The ResultExt shown above just takes care of the worst offender - Result::unwrap() which usually pulls in the full core::fmt::Debug implementations of the error types. But to get rid of all error message strings, you'll of course need to do a lot more. A list that certainly isn't exhaustive:

  • Option::unwrap()
  • Any uses of ops::Index/ops::IndexMut where the bounds check isn't optimized away - replace with fallible .get()/.get_mut calls and then work around the following Option::unwrap() as before.
  • If overflow checks are enabled, you get panicking code paths from a lot of maths operations. Either disable overflow checks or use the fallible methods on integer types instead (e.g. u32::checked_add())

from avr-hal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.