Git Product home page Git Product logo

Comments (11)

jpincas avatar jpincas commented on August 16, 2024

For reference, I've tried converting to bytes like this, but I get a panic: runtime error: slice bounds out of range at some point in the loop:

func stringToBytes(s *string) []byte {
	return *(*[]byte)(unsafe.Pointer(s))
}

from buntdb.

valinurovam avatar valinurovam commented on August 16, 2024

I think only solution is fork or make PR with some changes to work buntdb with slice of bytes than raw strings. Cause internally it use anyway bytes as u can see here https://github.com/tidwall/buntdb/blob/master/buntdb.go#L1218

from buntdb.

jpincas avatar jpincas commented on August 16, 2024

Oh yeah - you're right. I wonder what the motivation behind having string in the api was. Presumably that was for a good reason.

from buntdb.

tidwall avatar tidwall commented on August 16, 2024

I wonder what the motivation behind having string in the api was. Presumably that was for a good reason.

Strings are used instead of bytes because the database is in-memory and strings avoids any chance of accidental mutations to the base values. For example, with []byte values, it could be possible to call val := tx.Get("mykey") and then change the byte at some arbitrary position and break the ordering of the indexing.

This is less of an issue with on-disk databases because Get operations commonly retrieve from disk and load into memory for a short period of time. Changes to the memory would likely not corrupt the value in that persists on disk.


Your stringToBytes is casting types of two different sizes. string is 2 words and []bytes is 3 words. This may work better:

func stringToBytes(s string) []byte {
	return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
		(*reflect.StringHeader)(unsafe.Pointer(&s)).Data,
		len(s), len(s),
	}))
}

from buntdb.

jpincas avatar jpincas commented on August 16, 2024

Thanks so much. Could I be cheeky and ask for the correct function for the inverse operation - this is a little too low level for me!!

from buntdb.

tidwall avatar tidwall commented on August 16, 2024

Just swap out the StringHeader/SliceHeader and string/[]byte. Also a StringHeader takes 2 words which is the Data and Len fields, while the SliceHeader has Data, Len, and Cap.

func bytesToString(b []byte) string {
    return *(*string)(unsafe.Pointer(&reflect.StringHeader{
        (*reflect.SliceHeader)(unsafe.Pointer(&b)).Data, len(b),
    }))
}

from buntdb.

jpincas avatar jpincas commented on August 16, 2024

Superb - looking forward to plugging this improved conversion into the benchmarks. I bet it will improve the iteration speed massively.

from buntdb.

Nicolab avatar Nicolab commented on August 16, 2024

Hello,

@jpincas you have a best result with the stringToBytes?

from buntdb.

jpincas avatar jpincas commented on August 16, 2024

Actually it didn't seem to make a whole lot of difference. I think it might be the case that Go optimises this under the hood.

from buntdb.

Nicolab avatar Nicolab commented on August 16, 2024

Ok interesting, thank you for your reply šŸ‘

from buntdb.

tidwall avatar tidwall commented on August 16, 2024

Iā€™m closing this issue for now. Feel free to reopen if you find a specific issue that needs addressing. Thanks.

from buntdb.

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.