Giter Club home page Giter Club logo

Comments (3)

erooke avatar erooke commented on August 25, 2024

After #20 I started to play around with making mecha take memory allocations. I agree with parse failures being errors and not attempting to get mecha to manage memory. I have a couple of questions:

  • Should mecha attempt to enforce the use of arena allocator? In my experiments with adding memory allocation it became apparent that it would be basically impossible to clean up the memory by hand as a parse failure halfway through the chain would give you nothing to work with.
  • What is the point of OtherError in the proposed error set?
  • Should all parsers, regardless of if they allocate or not, take an allocator?

In my experiments I thought it would nice/interesting if as a user knew mecha was going to attempt to allocate memory. To that end I defined two types of parsers:

/// The type of all parsers that can work with `mecha`
pub fn Parser(comptime T: type) type {
    return fn (void, []const u8) ParseError!Result(T);
}

/// A parser which works with `mecha` and is allowed to allocate
pub fn ParserAlloc(comptime T: type) type {
    return fn (*mem.Allocator, []const u8) ParseAllocError!Result(T);
}

With the two associated error sets

pub const ParseError = error{ParseFailed};
pub const ParseAllocError = ParseError || mem.Allocator.Error;

I was able to get it working with not too much headache over on branch which no longer exists Not sure if others think that its worth the extra complexity to allow for such behavior however.

from mecha.

Hejsil avatar Hejsil commented on August 25, 2024
* Should mecha attempt to enforce the use of arena allocator? In my experiments with adding memory allocation it became apparent that it would be basically impossible to clean up the memory by hand as a parse failure halfway through the chain would give you nothing to work with.

There are many allocation schemes that are able to clean up the entire "pool", such as ArenaAllocator and FixedBufferAllocator. One could even make an allocator that just keeps track of a list of allocations, so that they can all be free at once if desired. I think it is best to just take an *mem.Allocator and let the user pick the best allocator for their needs.

* What is the point of `OtherError` in the proposed error set?

For conversions where the user of mecha wants to return an error which is not OutOfMemory. They could then look somethink up in their Context to figure out more details about their own specific error.

* Should all parsers, regardless of if they allocate or not, take an allocator?

I think the library should always take an allocator to keep the whole library simple. If the user wonna make sure the parser never allocates, they can pass a FailingAllocator and catch |err| switch (err) { error.OutOfMemory => unreachable }. I don't think this is unreasonable.

from mecha.

Hejsil avatar Hejsil commented on August 25, 2024

Let's leave this open until we have a version of many that collects the results. I have an API in mind that only has one many function that takes a set of options.

from mecha.

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.