Git Product home page Git Product logo

Comments (8)

zah avatar zah commented on August 28, 2024

Capturing and carrying around all these string values is a bit expensive btw. I suggest the following optimisation:

We can create a reusable module for capturing "source locations". A good place for it would be std_shims.

type
  SourceLocation* = object
    file*: cstring
    line*: int

proc `$`*(loc: ptr SourceLocation): string =
  result.add loc.file
  result.add ":"
  result.add $loc.line

proc sourceLocationIMPL(file: static string, line: static int): ptr SourceLocation =
  var loc {.global.} = SourceLocation(file: cstring(file), line: line)
  return loc.addr

template getSourceLocation*: ptr SourceLocation =
  sourceLocationIMPL(instantiationInfo(-2).filename, instantiationInfo(-2).line)

It's used like this:

import
  source_locations

type
  Future[T] = object
    createdAt: ptr SourceLocation

proc newFuture(T: type, loc: ptr SourceLocation): Future[T] =
  result.createdAt = loc

template newFuture(T: type): auto =
  newFuture(T, getSourceLocation())

var f = newFuture(int)
echo f.createdAt

The main idea is that every usage of newFuture effectively allocates a single global variable associated with the line of code where newFuture was used. A pointer to this global variable is all that needs to be passed around at run-time to track the origin location. There is no run-time memory allocation, string copying and other more expensive operations.

from nim-chronos.

cheatfate avatar cheatfate commented on August 28, 2024

@zah its very nice idea, but i don't see any reasons to put this small part of code as dependency std_shims.

Also i'm curious how it will work for Future[T] objects which was created using macro code?

from nim-chronos.

zah avatar zah commented on August 28, 2024

The main benefit is that it can be used in other projects as well for similar purposes.

from nim-chronos.

cheatfate avatar cheatfate commented on August 28, 2024

With recent nimble problems i wish to avoid external dependencies, also i dont think 13LOCs deserve its own module name. But i will add your copyright because it nice idea :)

from nim-chronos.

arnetheduck avatar arnetheduck commented on August 28, 2024

could this be a finalizer for the future type? ie assert on future that's released without being actioned?

from nim-chronos.

cheatfate avatar cheatfate commented on August 28, 2024

@arnetheduck you mean with being read()?

from nim-chronos.

arnetheduck avatar arnetheduck commented on August 28, 2024

https://nim-lang.org/docs/system.html#new%2Ctypedesc - new takes a proc that is run whenever the gc gets to deallocating something. we could use that (probably in debug mode) to catch stragglers

from nim-chronos.

dom96 avatar dom96 commented on August 28, 2024

Looks like great minds think alike :)

I implemented this: nim-lang/Nim#11181

from nim-chronos.

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.